fibonacci sequence [infinity]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 8 Exercise 4
Using std_lib_facilities.h by Bjarne Stroustrup.

main.cpp main file

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 8 Exercise 4

#include "std_lib_facilities.h"

vector<int> myVec;
string label = "l";

void print(string& _label, vector<int>& _vecInput) {
	for (int x : _vecInput) {
		cout << _label << " " << x << "\n";
	}
}

void fibonacci(int x, int y, vector<int>& v) {
	v.push_back(x);
	v.push_back(y);
	while(true){
		int temp;
		if (x > 0 && y > 0 && x + y > 0) {
			v.push_back(x + y);
			temp = y;
			y = x + y;
			x = temp;
		}
		else {
			break;
		}
	}
}

int main() 
try
{
	fibonacci(1, 2, myVec);
	print(label, myVec);

	cout << "The maximum number of the fibonacci sequence is: " << &myVec[myVec.size() - 1] << "\n";

	keep_window_open();

}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open(".");
}
catch (...) {
	cout << "Extiting" << "\n";
	keep_window_open(".");
}
Output:
l 1
l 2
l 3
l 5
l 8
l 13
l 21
l 34
l 55
l 89
l 144
l 233
l 377
l 610
l 987
l 1597
l 2584
l 4181
l 6765
l 10946
l 17711
l 28657
l 46368
l 75025
l 121393
l 196418
l 317811
l 514229
l 832040
l 1346269
l 2178309
l 3524578
l 5702887
l 9227465
l 14930352
l 24157817
l 39088169
l 63245986
l 102334155
l 165580141
l 267914296
l 433494437
l 701408733
l 1134903170
l 1836311903
The maximum number of the fibonacci sequence is: 000002A172660740
Please enter a character to exit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.