Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Fibonacci-Zahlen Iterativ und Rekursiv @ C++

Empfohlene Antworten

Hallo Leute, ich bin gerade dabei zu entschlüsseln was ich für einen Fehler beim Rechnen mache.

Ich kenne mich in C++ aus und gehe aber nochmal die Algorhythmik durch.

Von der Seite "http.//tutorial.schornboeck.net/los/fibonacci.htm" gehe ich dieses Prinzip durch und bei der for Schleife komme ich nicht weiter.

Ich rechne folgendes:

a = 1

b = 1

result = 1

Zahl 8 eingeben -> von 3-8 = Differenz = 5

5 Mal Schleife durchgehen

[1]

[

result = a + b

(2 = 1 + 1)

a = b

(a = 1)

b = result

(b= 2)

]

[2]

[

result = a + b

(3 = 1 + 2)

a = b

(a = 2)

b = result

(b= 3)

]

[3]

[

result = a + b

(5 = 2 + 3)

a = b

(a = 3)

b = result

(b= 5)

]

[4]

[

result = a + b

(8 = 3 + 5)

a = b

(a = 5)

b = result

(b= 8)

]

[5]

[

result = a + b

(13 = 5 + 8)

a = b

(a = 5)

b = result

(b= 13)

]

Result "wäre" dem entsprechend "13":confused:

Die hilfsbereite Konsole meldet mir 21 als iterative Lösung :confused: :P

Wo liegt mein Fehler?

P.S ach ja, ich habe noch nicht mal das Abi hinter mir. Bin Tüfftler aus Leidenschaft :D @ 16 years, falls mathematische Kenntnisse nicht reichen

Edit: Was mich an der Sache stutzig macht ist das i. Denn wenn das keine Rolle spielen würde, könnte man auch bei 0 beginnen. Es hat wahrscheinlich irgendetwas mit der Regel/Gesetz? zu tun

Bearbeitet von c0dy

Zahl 8 eingeben -> von 3-8 = Differenz = 5

5 Mal Schleife durchgehen

Von 3 bis (einschließlich) 8 sind 6 Schleifendurchläufe.

[5]

[

result = a + b

(13 = 5 + 8)

a = b

(a = 5)

b = result

(b= 13)

]

a=8. Und im nächsten Schritt kommt dann 13 + 8 = 21 heraus.

Edit: Was mich an der Sache stutzig macht ist das i. Denn wenn das keine Rolle spielen würde, könnte man auch bei 0 beginnen. Es hat wahrscheinlich irgendetwas mit der Regel/Gesetz? zu tun

Es könnte auch bei 0 anfangen. Der Autor will damit aber vermutlich die Position der Zahl in der Fibonacci-Folge wiedergeben. Die fängt ja mit 0, 1, 1 an. Die Startwerte für a und b sind f1 und f2. Die Funktion berechnet also die Werte erst ab f3.

Ah vielen Dank, habs gerade mal mit Zettel und Stift rausbekommen. Danke!

Ach ja, vorher habe ich dem Compiler noch die Anweisung gegeben und da habe ich auch 6 Läufe gesehen. Tjaja dieses Sache mit Einschließlich hatte ich immer bei "Mensch ärgere dich nicht" verwechselt:D

 Result: 2
a: 1
b: 2

Result: 3
a: 2
b: 3

Result: 5
a: 3
b: 5

Result: 8
a: 5
b: 8

Result: 13
a: 8
b: 13

Result: 21
a: 13
b: 21[/PHP]

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.