Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Recursion Functions: The proper condition to stop it

Tom Cameron
Ranch Hand
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
Posts: 96
I'm not sure I follow your problem description, could you post a sample output of what the functioning program will produce?

Keith Lynn
Ranch Hand
Posts: 2409
Are you just writing it iteratively to test it or do you need to set it up recursively?

Tom Cameron
Ranch Hand
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
Posts: 33
The output stops when the output are the same two numbers from the beginning input.

Michael Dunn
Ranch Hand
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
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
Posts: 33
I just noticed, I also have to keep the input ability in the solution.

Tom Cameron
Ranch Hand
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
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 ]

Stefan Wagner
Ranch Hand
Posts: 1923

Tom Cameron
Ranch Hand
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
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!