posted 11 years ago

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

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

posted 11 years ago

Are you just writing it iteratively to test it or do you need to set it up recursively?

Tom Cameron

Ranch Hand

Posts: 33

posted 11 years ago

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

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

Tom Cameron

Ranch Hand

Posts: 33

Tom Cameron

Ranch Hand

Posts: 33

Tom Cameron

Ranch Hand

Posts: 33

posted 11 years ago

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 ]

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 ]

Tom Cameron

Ranch Hand

Posts: 33