Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Necklace Problem, Number Theory
aaronpatel
post Oct 3 2007, 01:54 PM
Post #1


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 4
Joined: 3-October 07
Member No.: 25,295



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.

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;
        
        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.
Go to the top of the page
 
+Quote Post
Sten
post Oct 4 2007, 11:38 AM
Post #2


Oh come on Mrs. B!
Group Icon

Group: Members
Posts: 648
Joined: 6-June 07
From: Tasmania, Australia
Member No.: 22,422



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?


Go to the top of the page
 
+Quote Post
turbopowerdmaxst...
post Oct 4 2007, 12:09 PM
Post #3


Premium Member
Group Icon

Group: [HOSTED]
Posts: 415
Joined: 16-February 06
From: Kolkata, India
Member No.: 11,322
myCENTs:67.18



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:-

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:-

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


This post has been edited by turbopowerdmaxsteel: Oct 4 2007, 12:18 PM
Go to the top of the page
 
+Quote Post
aaronpatel
post Oct 4 2007, 01:54 PM
Post #4


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 4
Joined: 3-October 07
Member No.: 25,295



hey guys....thanks alot...i thought cause it was in java category, it would be self-explanatory...
Go to the top of the page
 
+Quote Post
aaronpatel
post Oct 4 2007, 01:57 PM
Post #5


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 4
Joined: 3-October 07
Member No.: 25,295



for some reason, the programming is not outputing anything...instead...cpu usage goes to a hundred percent...and its running at 2.93Ghz dual core without anything else running?

LOL!
Go to the top of the page
 
+Quote Post
turbopowerdmaxst...
post Oct 4 2007, 03:36 PM
Post #6


Premium Member
Group Icon

Group: [HOSTED]
Posts: 415
Joined: 16-February 06
From: Kolkata, India
Member No.: 11,322
myCENTs:67.18



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.
Go to the top of the page
 
+Quote Post
Sten
post Oct 5 2007, 11:57 AM
Post #7


Oh come on Mrs. B!
Group Icon

Group: Members
Posts: 648
Joined: 6-June 07
From: Tasmania, Australia
Member No.: 22,422



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


Go to the top of the page
 
+Quote Post
aaronpatel
post Oct 6 2007, 05:24 AM
Post #8


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 4
Joined: 3-October 07
Member No.: 25,295



yup got it working...wat was wrong with my original code...it seems to be logically rite?

Thanks for all your help...
Go to the top of the page
 
+Quote Post
Jeigh
post Oct 6 2007, 02:50 PM
Post #9


Whitest Black Mage
Group Icon

Group: [MODERATOR]
Posts: 1,371
Joined: 20-May 05
From: NB, Canada
Member No.: 5,281
myCENTs:65.99



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? wink.gif

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.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Help! Problem With My Flash-Drive(2)
  2. Error 406 - Problem In My Phpbb Forum(8)
  3. Ip Problem(8)
  4. MSN Help(5)
  5. Problem: I Need Urgent Help. Can't Boot.(9)
  6. ATI Video Card Problem! Need Help(6)
  7. Problem Accessing My Cpanel(9)
  8. Decrementing Number Of Digits Ofter Point(15)
  9. Einstein Quiz(30)
  10. Photoshop Cropping Problem(7)
  11. Need To Hack An Admin Account On Xp... No Problem!(61)
  12. Blue Screen - irql_not_less_or_equal(35)
  13. Spam Problem On My Forums(26)
  14. Frustrating Problem With XP On Laptop(20)
  15. Problem With Drag And Drop (or So It Seems).(12)
  1. Win Rar Password Problem(7)
  2. Internet Explorer 7 Problem(8)
  3. Fedora Core 6 Install Problem(6)
  4. Trojan / Virus Problem ,please Help(18)
  5. A Gaiaonline Problem(9)
  6. Administrator Account Problem In Microsoft Xp [solved](20)
  7. Problem With Move_uploaded_file()(5)
  8. Mind Control And Mindsong Inc(3)
  9. Problem With Div's In Ie6 And Lower(4)
  10. Pc Problem(8)
  11. Graphics Driver Out...(2)
  12. Problem Setting Up Wireless Internet & Wireless Nintendo Wii(4)
  13. System Is Crashed - Hardware Problem(3)


 



- Lo-Fi Version Time is now: 5th December 2008 - 01:45 AM