Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does my Loop end after 11 Loops?
#65
(02-13-2023, 02:43 PM)Dimster Wrote: Hi Steve. I don't think I have a Print statement in my actual code however I am going to do a thorough search thru it to make sure. I believe you are pointing out, if I can structure a Recursive Loops which does the same thing as the For Looping, then the speed would likely be the same?

No it is not! I already mentioned that above. A recursion can never be as fast or even faster than an iteration because of the stack usage.
If you still have an old PC with an 80268/80368 CPU, you can try it out there. Calculate the Fibonacci number iteratively once, and then recursively. The iterative result is there immediately, the recursive result took over 30 seconds at n = 33. This no longer works today, the CPUs are too fast for that. You need bigger things.

@Dimster, I think you still don't understand the difference between iteration and recursion. I have several books in which recursion is explained in a very understandable way, but of course it's all in German.

There should also be problems that cannot (yet) be solved iteratively, i.e. only recursively. But I know more where I read that.

For 80286 fans (The recursive variant is above.):
Code: (Select All)
'Fibonacci iterativ - 13. Feb. 2022

Option _Explicit

Dim x, y As Long
Dim i, n As Integer

x = 1: y = 1: i = 1
Cls
Locate 3, 3
Print "Berechnet iterativ die Fibonaccizahl."

Locate 5, 3
Input "Geben Sie eine Zahl ein: ", n

Locate 7, 3
If n > 33 Then
  Beep: Print "Nur Eingaben bis 33!"
  GoTo Ende
End If

While i < n
  x = x + y: y = x - y: i = i + 1
Wend

Locate 7, 3
Print Using "Die Fibonaccizahl von ## ist: ###,#####"; n, x

Ende:
End
Reply


Messages In This Thread
Why does my Loop end after 11 Loops? - by Dimster - 02-06-2023, 07:08 PM
RE: Why does my Loop end after 11 Loops? - by Kernelpanic - 02-13-2023, 06:09 PM



Users browsing this thread: 2 Guest(s)