Jump to content
xisto Community
Sign in to follow this  
aaronpatel

Necklace Problem Number Theory

Recommended Posts

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. The application output should look similar to:

I can't seem to figure it out.

Any help?

I tried doing this, but there is no output. It seems to just freeze my computer.

public class Necklace {	public static void main(String[] args) {		int originalFirstNumber = IBIO.inputInt("Enter the first starting number: ");		int originalSecondNumber = IBIO.inputInt("Enter the second starting number: ");				System.out.println(necklace(originalFirstNumber, originalSecondNumber));			}	private static String necklace(int originalFirstNumber,int originalSecondNumber) 	{		String output;				int firstNumber = originalFirstNumber;		int secondNumber = originalSecondNumber;				do{			output = originalFirstNumber + " " + originalSecondNumber + " ";			int result = firstNumber + secondNumber;			String resultInString = " " + result;			int lastNumberOfResult = resultInString.charAt(resultInString.length() - 1);			firstNumber = secondNumber;			secondNumber = lastNumberOfResult;			output += secondNumber + " ";		}		while(!(firstNumber == originalFirstNumber) && !(secondNumber == originalSecondNumber));				return output;	}}

Thanks.

Share this post


Link to post
Share on other sites

the necklace thing is confusing... im ok at maths but yeah confusing.oh and you might wanna tell us what language that is so we can help you?

Share this post


Link to post
Share on other sites

The line output = originalFirstNumber + " " + originalSecondNumber + " "; should be outside the do-while loop because the first two digits need to be added to the output string only for the very first time.

 

Next, the condition in while should be written as !(firstNumber == originalFirstNumber && secondNumber == originalSecondNumber). Notice that the NOT operator surrounds the entire condition and not individually on both.

 

 

This is the correct code:-

 

public class Necklace {	public static void main(String[] args) {		int originalFirstNumber = IBIO.inputInt("Enter the first starting number: ");		int originalSecondNumber = IBIO.inputInt("Enter the second starting number: ");				System.out.println(necklace(originalFirstNumber, originalSecondNumber));			}	private static String necklace(int originalFirstNumber,int originalSecondNumber) 	{		String output;				int firstNumber = originalFirstNumber;		int secondNumber = originalSecondNumber;				output = originalFirstNumber + " " + originalSecondNumber + " ";		do{			int result = firstNumber + secondNumber;			String resultInString = " " + result;			int lastNumberOfResult = resultInString.charAt(resultInString.length() - 1);			firstNumber = secondNumber;			secondNumber = lastNumberOfResult;			output += secondNumber + " ";		}		while(!(firstNumber == originalFirstNumber && secondNumber == originalSecondNumber));				return output;	}}

P.S. - Instead of converting the result to a string and getting the last character, you can use the modulus operator (%) to get the last digit of any number. The updated code inside the do-while loop block would be as follows:-

 

int result = firstNumber + secondNumber;int lastNumberOfResult = result % 10;firstNumber = secondNumber;secondNumber = lastNumberOfResult;output += secondNumber + " ";

Edited by turbopowerdmaxsteel (see edit history)

Share this post


Link to post
Share on other sites

Earlier I was trying the code in C#. I have managed to trace the cause to the line int lastNumberOfResult = resultInString.charAt(resultInString.length() - 1);

 

Use the replacement code I gave for the body of the do-while loop which gets rid of the need to convert the result into the string. That should do it.

Share this post


Link to post
Share on other sites

oh... i didnt even stop to think about looking at that, lol my bad.i have absolutely no experience what so ever in java so i hope that turbopoweredmaxsteel's code was right, lol

Share this post


Link to post
Share on other sites

So I need to ask, even though if my guess is correct I assume he won't be back on the boards anytime soon... what class was that for? ;)I love how such simple programs can take 100% cpu usage. I mean from a technical viewpoint it makes perfect sense (if nothing else needs the extra cpu cycles they devote it all to something that does need it, even if all the program is doing is adding infinitly to an increment variable) but from a non-technical viewpoint it still amuses me haha.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.