This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes Recursion Functions:  The proper condition to stop it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Recursion Functions:  The proper condition to stop it" Watch "Recursion Functions:  The proper condition to stop it" New topic
Author

Recursion Functions: The proper condition to stop it

Tom Cameron
Ranch Hand

Joined: Apr 30, 2006
Posts: 33
Good evening everyone!

I ran into a strange looking problem the other day. It involves the use of a special recursive function called the necklace problem. Heard it's supposed to be a famous math problem. Anyways. The thing is, I have to input two numbers, the first one to fill in the tens value place, and the second number to fill in the ones value place. To get the next number, I have to add the two previous numbers and only use the number in the ones place value, but it has to stop if you see the same two numbers in the beginning. So only the numbers of the ones place value are used. I am very close to getting it, but the condition to stop my loop doesn't seem right.



I seem to be very close to getting it right. What's the correct boolean condition to stop it? Suggestions/ideas/solutions all welcomed if possible
Michael Valentino
Ranch Hand

Joined: Nov 01, 2005
Posts: 96
I'm not sure I follow your problem description, could you post a sample output of what the functioning program will produce?


SCJP 1.4, SCWCD J2EE 1.4, SCJD J2SE 1.5, SCBCD J2EE 1.3, SCDJWS (In Progress)
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Are you just writing it iteratively to test it or do you need to set it up recursively?
Tom Cameron
Ranch Hand

Joined: Apr 30, 2006
Posts: 33
I'm "trying" to set it up recursively by using the arrays (just seems easier). The problem seems to lie around the for loop condition.

Sorry if my problem doesn't seem to be clear.

Example Input:
1
8

Output
1 8 9 7 6 3 9 2 1 3 4 7 1 8

so it's like 1 + 8 = 9
9 + 8 = 7 (use only the ones place, the digit on the right)
7 + 9 = 6
... and so on
Tom Cameron
Ranch Hand

Joined: Apr 30, 2006
Posts: 33
The output stops when the output are the same two numbers from the beginning input.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
can't see any recursion in your posted code, just a for loop
see if this makes any sense

Tom Cameron
Ranch Hand

Joined: Apr 30, 2006
Posts: 33
It works quite well, but it sorta destroyed the whole idea of a recursion. I tried to define the next number in terms of the previous two numbers that were being calculated. I never expected this to work your way like that.
Tom Cameron
Ranch Hand

Joined: Apr 30, 2006
Posts: 33
I just noticed, I also have to keep the input ability in the solution.
Tom Cameron
Ranch Hand

Joined: Apr 30, 2006
Posts: 33
you made your solution somewhat strange and hard for me to change, I can't get a user input from the way you wrote your solution.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

a) swap number1 and number2:
b) counter is incremented after the calculation, which leads to array[counter]==0 for every iteration while testing
c) you need a || instead of && - do while one of both conditions is false (or both).


[ May 31, 2006: Message edited by: Stefan Wagner ]

[ May 31, 2006: Message edited by: Stefan Wagner ]
[ June 01, 2006: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Tom Cameron
Ranch Hand

Joined: Apr 30, 2006
Posts: 33
That second post of yours Stefan makes much much more sense. When you changed my condition ( 2 messages above this) didn't work. I understand it now. Thanks.
Tom Cameron
Ranch Hand

Joined: Apr 30, 2006
Posts: 33
Conditions now work correctly, silly me, doh'. As always, you guys give a very speedy reply, thank you Michael Dunn and Stefan Wagner and everyone else here!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Recursion Functions: The proper condition to stop it