PDA

View Full Version : Spirals and the Fibonacci Function


lanceA
11-10-2006, 07:45 PM
I'm trying to develop a fibonacci method to solve the following:

A guy is at the bottom of a mountain, the mountain is to the North, he is facing South. He says, "Now that I am at the bottom of the mountain, I need to turn around." After he turns he says, "Then I walk in a spiral of six quarter turns to the left. If I do this I will be at the opening for the magic tunnel at the bottom of the mountain."

This has to be solved using recursion. The method to be invoked must employee fibonacci computation methods. Otherwise his movement will be similar to 3rd grade herky-jerky movements. :)

Has anyone successfully used recursion in solving Fibonacci computations in ALICE and would you be willing to share your code?

Computing the fibonacci numbers will cause the MAN to walk in a true spiral, not a circle or square or rectangle, etc.


Thanks in advance

P.S. The answer is NOT:
Loop 6 times
Do together
man.move. forward .1 meters
man.turn.left 1/4 revolution

DrJim
11-11-2006, 11:14 AM
See attached code for the recursive sequence generation. Some notes:

1. Technically, this doesn't violate the cheating policy since problem 7-7a, p. 209 in the Herbert text asks for pseudo-code and this is the real thing. Still - comments anyone? (I'm not being a smart ass here - I really tried and couldn't figure out a way to explain this without posting the code.)

2. The algorithm is from a 20+ year old reference http://www.atarimagazines.com/creative/v11n4/98_More_than_one_way_to_skin.php that used Basic (ohh :eek: ) and a dreaded "GOTO" (ahh, gasp :( :p ). To get the recursive version, I just replaced the GOTO with the "obviously superior" (OK, I can be a smart ass :D ) recursive method call. The reference also gives a formula for calculating individual numbers in a Fibonacci sequence, although I haven't tried it.

3. To generate the spiral once you have the sequence, see http://library.thinkquest.org/27890/theSeries6a.html . Note that a spiral generated this way will have discontinuous second derivatives and will be a bit "herky-jerky."

lanceA
11-11-2006, 06:41 PM
Thanks for the suggestion. I notice your method requires 2 input params.....I prefer the students do it with one. I'm trying to teach them efficient coding techniques

I think I've worked it out with a for-loop, using 4 lines of code in Alice. But I will give them credit if they use your method because it does work.

Thanks again,

DrJim
11-13-2006, 09:38 AM
I wrote that code specifically to demonstrate a purely recursive approach and therefore avoided any iterative construct such as a loop. Other that to say I think it's a great topic for class discussion, I won't comment directly on relative efficiencies, since that's also part c of the problem in the Herbert text.

I will note, however, that the few times I've actually used the sequence (it's a good benchmark because of it's slightly "herky-jerky" behavior) I've used either a table lookup or an iterative approach to generate it.

Shadow Sovereign
11-14-2006, 07:33 PM
Not more Fibonnaci.... :(


....I think my head is gonna explode. :eek: