Published · 2 min read

The Necklace Problem

An interesting problem in number theory is sometimes called the Necklace Problem . This problem begins with two single-digit numbers.

An interesting problem in number theory is sometimes called the Necklace Problem. This problem begins with two single-digit numbers. The next number is obtained by adding the first two numbers together and saving only the ones digit. This process is repeated until the necklace closes by returning to the original two numbers. For example, if the starting two numbers are 1 and 8, twelve steps are required to close the necklace: 1 8 9 7 6 3 9 2 1 3 4 7 1 8.

Create a Necklace application that prompts the user for two single-digit integers and then displays the sequence and the number of steps taken.

using System;

namespace Necklace_Problem
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the First Number:");
            int Num1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the Second Number:");
            int Num2 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Output = {0}", Necklace(Num1, Num2));
            Console.Read();
        }

        private static string Necklace(int OrigFirstNum, int OrigSecNum)
        {
            string Output = "";

            int FirstNum = OrigFirstNum;
            int SecondNum = OrigSecNum;

            Output = OrigFirstNum + " " + OrigSecNum + " ";
            do
            {
                int Result = FirstNum + SecondNum;
                int LastDigit = Result % 10;
                FirstNum = SecondNum;
                SecondNum = LastDigit;
                Output += SecondNum + " ";
            } while (!(FirstNum == OrigFirstNum && SecondNum == OrigSecNum));

            return Output;
        }
    }
}
Namespace Necklace_Problem
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Console.Write("Enter the First Number:")
            Dim Num1 As Integer = Convert.ToInt32(Console.ReadLine())

            Console.Write("Enter the Second Number:")
            Dim Num2 As Integer = Convert.ToInt32(Console.ReadLine())

            Console.WriteLine("Output = {0}", Necklace(Num1, Num2))
            Console.Read()
        End Sub

        Private Shared Function Necklace(ByVal OrigFirstNum As Integer, ByVal OrigSecNum As Integer) As String
            Dim Output As String = ""

            Dim FirstNum As Integer = OrigFirstNum
            Dim SecondNum As Integer = OrigSecNum

            Output = OrigFirstNum + " " + OrigSecNum + " "
            Do
                Dim Result As Integer = FirstNum + SecondNum
                Dim LastDigit As Integer = Result Mod 10
                FirstNum = SecondNum
                SecondNum = LastDigit
                Output += SecondNum + " "
            Loop While Not (FirstNum = OrigFirstNum AndAlso SecondNum = OrigSecNum)

            Return Output
        End Function
    End Class
End Namespace
Partho Sarathi

Partho Sarathi

Founder, Maxotek

Programming Basics

Our blog

Related posts

Tutorials, notes and news from Maxotek.

  • Digital logic gates

    Digital logic gates is an electronic component which results perticular ouput after implementing its logic on the input signals.

    Partho Sarathi

    Partho Sarathi

  • 64-Bit Computing

    Around 2003 the term 64-Bit computing was everywhere. Want to install Windows XP? Great! Do you want the 32-Bit disc or 64-Bit disc? Installing WinZip for your shiny new re-installed OS?

    Partho Sarathi

    Partho Sarathi

View all posts

See what Maxotek makes

Monitoring, conversion tools and more, built by the same people who wrote these posts.