Calculating a Fibonacci Number

Is this a common source code amongst "Loop topic programming"?
And also one of the two classic Hello Worlds of functional programming, the other one being a factorial function.
Well, I made my own Fibonacci Sequence Calculator that doesn't invole factorial functions. Here's the source code if you want it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;

double long NUMBEROFRANGE;

void Calculus();
void Result();
int Finish();

	
	double long Morph = 1;
	double long New = 1;
	

int main()
{
	int Choice;

	cout << "Make a choice: \n\n1- Calculate Fibonacci Sequence \n2- Close Program" << endl;
	cin >> Choice;
	switch (Choice)
	{
	case 1:
		Calculus();
		break;

	case 2:
		Finish();
		break;
	
	}
		return 0;
}


void Calculus()
{

	New = New + Morph;
	Morph = New - Morph;
	
	if (New == 2)
	{
		cout << "Insert number of range: " << endl;
		cin >> NUMBEROFRANGE;
		system ("cls");
	}

	if (New == 2)
	{
		cout << "0 \n1 \n1 \n";
	}

	
Result();

}
void Result()
{ 


	while (New <= NUMBEROFRANGE){
		cout  <<  New << endl;
		Calculus();
	}
}

int Finish()
{
	return 0;
}


The only problem is the both first "1's" I had to put as strings. Enjoy
...
ah how could anyone forget "Hello World"
kibestar...interesting code I'm pretty new to C++ and I just kept seeing constant loops for Fibonacci numbers...One included 100 loops and it just seems rediculous to repeat this code over and over...Am I being Naive or is 100 loops common as well?
Given that the nth Fibonacci number, F(n), has the formula:

F(n) = 1, n < 3
= F(n-1) + F(n-2), n >= 3

it stands to reason that it can be implemented very simply (albeit inefficiently) as a
recursive function containing exactly one if-else statement.

Alternatively the recursion can be unrolled into a simple loop that counts from 1...N
or N...1 in order to compute F(N).
Argh, that is such an weird convention... The zero-th Fibonacci number morally should be 1.
F(-1)=0
F(0)=1
F(1)=1

The reason why this labeling convention is more natural is the following: the Fibonacci numbers appear naturally as the coefficients in the power series expansion of 1/(1-x-x^2). The power series expansion starts like this:
1
2
1/(1-x-x^2) = 1    +   x+  2 x^2+  3 x^3+  5 x^4+  8 x^5+...
            = F(0)+F(1)x+F(2)x^2+F(3)x^3+F(4)x^4+F(5)x^5+...



Your choice of Fibonacci number labels corresponds to the power series of the function x/(1-x-x^2) which one superfluous power of x more.
Last edited on
Topic archived. No new replies allowed.