The Necklace Problem

Partho Sarathi

04 Oct, 2007 · 2 minutes read

  • Share:

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 necklacecloses 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
  • Share:

The cleanest blogging platform


2024 © Maxotek. All rights reserved.