programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# Recursion Functions: The proper condition to stop it

Ranch Hand
Posts: 33
• Number of slices to send:
Optional 'thank-you' note:
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

Ranch Hand
Posts: 96
• Number of slices to send:
Optional 'thank-you' note:
I'm not sure I follow your problem description, could you post a sample output of what the functioning program will produce?

Ranch Hand
Posts: 2412
• Number of slices to send:
Optional 'thank-you' note:
Are you just writing it iteratively to test it or do you need to set it up recursively?

Tom Cameron
Ranch Hand
Posts: 33
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
The output stops when the output are the same two numbers from the beginning input.

Ranch Hand
Posts: 4632
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
I just noticed, I also have to keep the input ability in the solution.

Tom Cameron
Ranch Hand
Posts: 33
• Number of slices to send:
Optional 'thank-you' note:
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.

Ranch Hand
Posts: 1923
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

Tom Cameron
Ranch Hand
Posts: 33
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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!

 grapes are vegan food pellets. Eat this tiny ad: free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter