473,765 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fibonacci numbers

Hello, I have a small problem, I am trying to write a program that
will calculate the Fibonacci number series, and I have the code
complete with one problem. I used a long in to store the numbers, and
when the numbers get too large it maxes out the int and I can't count
any higher. I am trying to use extremely large numbers, I would like
to use up to 10^50 or so. So my question is how do I do this? I'm just
learning the language and I can't think of any way around variable
storage limitations. I would appreciate any advice.
Lee ;-)
Jul 19 '05
28 13118
<Alex Vinokur>
-----------------------
Algorithm-1 : See above
-----------------------
[1] 17.47 real, 17.16 user, 0.19 sys
[2] 17.44 real, 17.24 user, 0.15 sys
[3] 17.46 real, 17.19 user, 0.18 sys
[4] 17.36 real, 17.15 user, 0.13 sys
[5] 17.52 real, 17.22 user, 0.15 sys

--------------------------
Algorithm-2 : http://alexvn.freeservers.com/s1/fibonacci.html
--------------------------
[1] 9.26 real, 9.00 user, 0.19 sys
[2] 9.31 real, 9.06 user, 0.21 sys
[3] 9.38 real, 9.07 user, 0.24 sys
[4] 9.35 real, 9.06 user, 0.21 sys
[5] 9.33 real, 9.00 user, 0.27 sys

</>

Those are promissing results. Thank you, Alex!

Here is a functor-based Fibonacci generator. It is quicker than ever.
The bottleneck is in the output to the screen, that slows down the
program considerably. I guess this program can run 4 times faster than
it does now. If you want to impress us, fiddle with ios_base. Dietmar?

#include<iostre am>
#include<algori thm>
#include<vector >
static ostream_iterato r<int>out(cout) ;
static char base=10;
static char carry=0;
struct CarryChar
{
char&operator() (const char&a,const char&b)
{
char c=a+b+carry;
carry=0;
if(c>=base){c-=base;carry=1;}
return c;
}};
struct LongInt
{
CarryChar carrychar;
vector<char>v;
LongInt(char a){do v.push_back(a%b ase);while((a/=base)!=0);}
LongInt&operato r()(LongInt&a)
{
carry=0;
v.resize(a.v.si ze());
transform(v.beg in(),v.end(),a. v.begin(),v.beg in(),carrychar) ;
if(carry)v.push _back(1);
v.swap(a.v);
return*this;
}};
ostream&operato r<<(ostream&a,c onst LongInt&b)
{
copy(b.v.rbegin (),b.v.rend(),o stream_iterator <int>(a));
return a;
}
int main()
{
LongInt a(0);
LongInt b(1);
for(int c=0;c<5000;c++) cout<<a(b)<<'\n ';
return 0;
}

Jul 19 '05 #21
In article <bn**********@n ews1.tilbu1.nb. home.nl>,
mb************* ******@home.nl says...

[ ... ]
Here is a functor-based Fibonacci generator. It is quicker than ever.
Right now, it looks to me mostly like proof that you use a (badly)
broken compiler.
The bottleneck is in the output to the screen, that slows down the
program considerably. I guess this program can run 4 times faster than
it does now. If you want to impress us, fiddle with ios_base. Dietmar?

#include<iostre am>
#include<algori thm>
#include<vector >
static ostream_iterato r<int>out(cout) ;
The compiler _should_ complain about 'cout' being an undeclared name (I
suspect you intended to use std::cout).
char&operator() (const char&a,const char&b)
{
char c=a+b+carry;
carry=0;
if(c>=base){c-=base;carry=1;}
return c;
}};
This returns a reference to a local variable, which almost inevitably
leads to undefined behavior.

[ ...]
int main()
{
LongInt a(0);
LongInt b(1);
for(int c=0;c<5000;c++) cout<<a(b)<<'\n ';


And _this_ is about as counter-intuitive as anything could possibly be.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #22
In article <bn**********@n ews1.tilbu1.nb. home.nl>,
mb************* ******@home.nl says...

[ ... ]
Here is a functor-based Fibonacci generator. It is quicker than ever.
The bottleneck is in the output to the screen, that slows down the
program considerably. I guess this program can run 4 times faster than
it does now. If you want to impress us, fiddle with ios_base. Dietmar?


Just for reference, I wrote up a short little Fibonacci number generator
using NTL. I then ran both it and your program (after fixing it) on my
machine, with the output re-directed to NUL (/dev/null for UNIX types)
to mostly remove the I/O from the picture.

The NTL program was approximately 4 times as fast as your's. If you
want to impress us, write some code that's correct and readable...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #23


Agent Mulder wrote:
struct CarryChar
{
char&operator() (const char&a,const char&b)


what is the point in passing a single character per reference?
Dito for the return value.
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 19 '05 #24
<Agent Mulder>
char&operator() (const char&a,const char&b) </>

<Karl Heinz Buchegger> what is the point in passing a single character per reference?
Dito for the return value.

</>

No point. Just forgot to remove it. I did remove comment
and indentation, by accident. I put it back in, but remember

"to road to perdition is paved with good indentation"
---Andrei Alexandrescu

//fast Fibonacci generator for large numbers. Email to mb******@home.n l
#include<iostre am>
#include<algori thm>
#include<vector >

//global data base, carry. Used by CarryChar and LongInt
static char base=10;
static char carry=0;

//struct CarryChar is used as functor in the STL transform algorithm
struct CarryChar
{
char operator()(cons t char a,const char b)
{
char c=a+b+carry;
carry=0;
if(c>=base){c-=base;carry=1;}
return c;
}};

//global data carrychar, one instance of the above struct CarryChar
CarryChar carrychar;

//struct LongInt holds an expanding vector<char> and uses carrychar
struct LongInt
{
vector<char>v;
LongInt(char a){do v.push_back(a%b ase);while((a/=base)!=0);}
LongInt&operato r()(LongInt&a)
{
v.resize(a.v.si ze());
carry=0;
transform(v.beg in(),v.end(),a. v.begin(),v.beg in(),carrychar) ;
if(carry)v.push _back(1);
v.swap(a.v);
return*this;
}};

//operator<< outputs a LongInt object (to the screen)
ostream&operato r<<(ostream&a,c onst LongInt&b)
{
copy(b.v.rbegin (),b.v.rend(),o stream_iterator <int>(a));
return a;
}

//main. Note how LongInt(0) is forced cout<< to produce 0,1,1,2,3,5,8,1 3...
int main()
{
LongInt a(0);
LongInt b(1);
cout<<a<<'\n';
for(int c=0;c<20;c++)co ut<<a(b)<<'\n';
return 0;
}

Jul 19 '05 #25
I hate to misspell a quote, so here once again:

"the road to perdition is paved with good indentation"
---Andrei Alexandrescu

-X
Jul 19 '05 #26

"Agent Mulder" <mb************ *******@home.nl > wrote in message news:bn******** **@news1.tilbu1 .nb.home.nl...
<Alex Vinokur>
-----------------------
Algorithm-1 : See above
-----------------------
[1] 17.47 real, 17.16 user, 0.19 sys
[2] 17.44 real, 17.24 user, 0.15 sys
[3] 17.46 real, 17.19 user, 0.18 sys
[4] 17.36 real, 17.15 user, 0.13 sys
[5] 17.52 real, 17.22 user, 0.15 sys

--------------------------
Algorithm-2 : http://alexvn.freeservers.com/s1/fibonacci.html
--------------------------
[1] 9.26 real, 9.00 user, 0.19 sys
[2] 9.31 real, 9.06 user, 0.21 sys
[3] 9.38 real, 9.07 user, 0.24 sys
[4] 9.35 real, 9.06 user, 0.21 sys
[5] 9.33 real, 9.00 user, 0.27 sys </>

Those are promissing results. Thank you, Alex!


Thank you, Agent!

Here is a functor-based Fibonacci generator. It is quicker than ever.
The bottleneck is in the output to the screen, that slows down the
program considerably.
I think, one should measure getting only n-th Fibonacci number (not all Fibonacci number 1 until n)
I guess this program can run 4 times faster than
it does now. If you want to impress us, fiddle with ios_base. Dietmar?

// ############### ############### #############
// Computing very long Fibonacci numbers : BEGIN
// Algorithm-1b
// ############### ############### #############
#include<iostre am>
#include<algori thm>
#include<vector >
static ostream_iterato r<int>out(cout) ;
static char base=10;
static char carry=0;
struct CarryChar
{
char&operator() (const char&a,const char&b)
{
char c=a+b+carry;
carry=0;
if(c>=base){c-=base;carry=1;}
return c;
}};
struct LongInt
{
CarryChar carrychar;
vector<char>v;
LongInt(char a){do v.push_back(a%b ase);while((a/=base)!=0);}
LongInt&operato r()(LongInt&a)
{
carry=0;
v.resize(a.v.si ze());
transform(v.beg in(),v.end(),a. v.begin(),v.beg in(),carrychar) ;
if(carry)v.push _back(1);
v.swap(a.v);
return*this;
}};
ostream&operato r<<(ostream&a,c onst LongInt&b)
{
copy(b.v.rbegin (),b.v.rend(),o stream_iterator <int>(a));
return a;
}
int main()
{
LongInt a(0);
LongInt b(1);
for(int c=0;c<5000;c++) cout<<a(b)<<'\n ';
return 0;
}

To get only n-th number I changed main() as following :
------ main ------
int main(int argc, char** argv)
{
assert (argc == 2);
LongInt a(0);
LongInt b(1);
const int number (atoi(argv[1]) - 1);
for (int c = 0 ; c < number; c++) a(b);
cout << a(b) << "\n";
return 0;
}
------------------

I also changed
char& operator()(cons t char&a,const char&b)
to
char operator()(cons t char&a,const char&b)
because of compilation warning : " warning: reference to local variable `c' returned" (GNU g++ 3.3.1)

Program above (with changes) is called Algorithm-1b

// ############### ############### #############
// Computing very long Fibonacci numbers : END
// Algorithm-1b
// ############### ############### #############

Here is Algorithm-2b.

// ############### ############### #############
// Computing very long Fibonacci numbers : BEGIN
// Algorithm-2b
// ############### ############### #############

#include <assert.h>
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
#define MAX_VALUE(x,y) ((x) > (y) ? (x) : (y))
#define ASSERT(x)
// #define ASSERT(x) assert(x)

#define MAX_UNIT_VALUE (ULONG_MAX >> 2)
#define BASE 10
typedef unsigned int uint;
typedef unsigned long ulong;

// ==============
class BigInt
// ==============
{
friend class Aux;
friend ostream& operator<< (ostream& o, const BigInt& ins_i);

private :
static ulong base_s;
vector<ulong> units_;

static void Set_Full_Base ();

const BigInt operator+ (const BigInt& rhs_i) const;

public :

BigInt () {}
BigInt (ulong unit_i)
{
if (base_s == 1) Set_Full_Base ();
ASSERT (unit_i < base_s);
units_.push_bac k (unit_i);
}
BigInt (const BigInt& lhs_i, const BigInt& rhs_i)
{
(*this) = lhs_i + rhs_i;
}
~BigInt () {}

};
// --------------
class Aux
{
private :
ulong& head;

public :
ulong operator() (ulong n1, ulong n2)
{
ulong value = n1 + n2 + head;
head = value/BigInt::base_s;
return (value%BigInt:: base_s);
}

Aux (ulong& head_io) : head (head_io) {}
};

// --------------
inline const BigInt BigInt::operato r+ (const BigInt& rhs_i) const
{
BigInt ret;

const ulong max_size = MAX_VALUE (units_.size (), rhs_i.units_.si ze ());

vector<ulong> u1 (units_);
vector<ulong> u2 (rhs_i.units_);

u1.resize(max_s ize);
u2.resize(max_s ize);
ret.units_.resi ze(max_size);

ulong head = 0;
transform (u1.begin(), u1.end(), u2.begin(), ret.units_.begi n(), Aux (head));

if (head) ret.units_.push _back (head);

return ret;

}

// --------------
inline ostream& operator<< (ostream& os, const BigInt& ins_i)
{
ASSERT (!ins_i.units_. empty ());
for (ulong i = (ins_i.units_.s ize () - 1); i > 0; i--)
{
os << ins_i.units_ [i] << setw (BASE - 1) << setfill ('0');
}
return os << ins_i.units_ [0];
}
// --------------
void BigInt::Set_Ful l_Base ()
{
ASSERT (base_s == 1);

// ------------------
for (ulong i = 1; i <= (BASE - 1); i++)
{
base_s *= BASE;

ASSERT (!(base_s >= MAX_UNIT_VALUE) || (base_s == 0));
ASSERT (BASE * ((base_s/BASE) + 1) < MAX_UNIT_VALUE) ;
ASSERT (base_s != 0);
}
}
// =============== ======
class Fibonacci
// =============== ======
{
private :
vector<BigInt> fibs_;
BigInt get_number (uint n_i = 0);

public :
void show_all () const;
void show_number () const;

Fibonacci (uint n_i = 0) { get_number (n_i); }
~Fibonacci () {}

};
// -----------------------
BigInt Fibonacci::get_ number (uint n_i)
{
const uint cur_size = fibs_.size ();

for (uint i = cur_size; i <= n_i; i++)
{
switch (i)
{
case 0 :
fibs_.push_back (BigInt(0));
break;

case 1 :
if (fibs_.empty ()) fibs_.push_back (BigInt (0));
fibs_.push_back (BigInt (1));
break;

default :
fibs_.push_back (BigInt (get_number (i - 2), get_number (i - 1)));
break;
}
}

ASSERT (n_i < fibs_.size());
return fibs_ [n_i];

}
// -----------------------
void Fibonacci::show _all () const
{
ostringstream oss;

for (uint i = 0; i < fibs_.size (); i++)
{
oss << "Fib [" << i << "] = " << fibs_ [i] << "\n";
}
cout << oss.str();
}
// -----------------------
void Fibonacci::show _number () const
{
ostringstream oss;

oss << "Fib [" << (fibs_.size() - 1) << "] = " << fibs_.back() << "\n";

cout << oss.str();
}
// -------------------
ulong BigInt::base_s (1);
// =============== ===============
#define ALL_FIBS "all"
#define ONE_FIB "th"

int main (int argc, char **argv)
{
string option;
if ((argc <= 2) || !(((option = argv[2]) == ALL_FIBS) || (option == ONE_FIB)))
{
cerr << "\tUSAGE : " << argv[0] << " " << "<N - number> " << ALL_FIBS << "|" << ONE_FIB << endl;
if (argc < 2) return 1;

cerr << "\t " << "-----------------------" << endl;
cerr << "\t " << setw(3) << std::left << ALL_FIBS << " - Fibonacci [0 - N]" << endl;
cerr << "\t " << setw(3) << std::left << ONE_FIB << " - Fibonacci [N]" << endl;
cerr << "\t " << "-----------------------" << endl;
return 2;
}

Fibonacci fib(atoi(argv[1]));
(option == ALL_FIBS) ? fib.show_all() : fib.show_number ();

return 0;
}

// ############### ############### #############
// Computing very long Fibonacci numbers : END
// Algorithm-2b
// ############### ############### #############


// ############### ############### #############
// Comparative performance : BEGIN
// ############### ############### #############
=============== ==============
Windows 2000 Professional
Intel(R) Celeron(R) CPU 1.70. GHz
CYGWIN_NT-5.0 1.5.4(0.94/3/2)
GNU g++ version 3.3.1 (cygming special)
GNU time 1.7
=============== ==============

Complexity of two algorithms has been compared while computing Fibonacci-5000, Fibonacci-10000, Fibonacci-25000.

--------------
Algorithm-1b :
--------------

Fibonacci-5000
--------------
[1] 0.27 real, 0.25 user, 0.04 sys, 10704 max-resident
[2] 0.27 real, 0.25 user, 0.03 sys, 10704 max-resident
[3] 0.27 real, 0.25 user, 0.03 sys, 10704 max-resident
[4] 0.27 real, 0.25 user, 0.03 sys, 10704 max-resident
[5] 0.27 real, 0.26 user, 0.01 sys, 10704 max-resident
Fibonacci-10000
---------------
[1] 0.95 real, 0.94 user, 0.03 sys, 10736 max-resident
[2] 0.94 real, 0.92 user, 0.04 sys, 10736 max-resident
[3] 0.94 real, 0.92 user, 0.03 sys, 10736 max-resident
[4] 0.94 real, 0.92 user, 0.03 sys, 10736 max-resident
[5] 0.94 real, 0.92 user, 0.04 sys, 10736 max-resident
Fibonacci-25000
---------------
[1] 5.63 real, 5.61 user, 0.03 sys, 10800 max-resident
[2] 5.67 real, 5.64 user, 0.03 sys, 10800 max-resident
[3] 5.62 real, 5.59 user, 0.02 sys, 10800 max-resident
[4] 5.63 real, 5.60 user, 0.04 sys, 10800 max-resident
[5] 5.66 real, 5.62 user, 0.04 sys, 10800 max-resident


--------------
Algorithm-2b :
--------------

Fibonacci-5000
--------------
[1] 0.22 real, 0.20 user, 0.03 sys, 14960 max-resident
[2] 0.22 real, 0.20 user, 0.04 sys, 14960 max-resident
[3] 0.23 real, 0.20 user, 0.04 sys, 14960 max-resident
[4] 0.22 real, 0.20 user, 0.03 sys, 14960 max-resident
[5] 0.22 real, 0.19 user, 0.04 sys, 14960 max-resident
Fibonacci-10000
---------------
[1] 0.52 real, 0.50 user, 0.03 sys, 21552 max-resident
[2] 0.52 real, 0.49 user, 0.05 sys, 21552 max-resident
[3] 0.53 real, 0.51 user, 0.03 sys, 21552 max-resident
[4] 0.53 real, 0.50 user, 0.04 sys, 21552 max-resident
[5] 0.53 real, 0.50 user, 0.04 sys, 21552 max-resident
Fibonacci-25000
---------------
[1] 2.03 real, 2.00 user, 0.01 sys, 20144 max-resident
[2] 2.02 real, 1.97 user, 0.07 sys, 20144 max-resident
[3] 2.03 real, 1.98 user, 0.06 sys, 20144 max-resident
[4] 2.03 real, 1.98 user, 0.07 sys, 20144 max-resident
[5] 2.36 real, 2.31 user, 0.05 sys, 20144 max-resident
Note. max-resident is Maximum resident set size of the process during its lifetime, in Kilobytes.

// ############### ############### #############
// Comparative performance : END
// ############### ############### #############
--
=============== =============== =======
Alex Vinokur
mailto:al****@c onnect.to
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=============== =============== =======



Jul 19 '05 #27

"Alex Vinokur" <al****@bigfoot .com> wrote in message news:bn******** ****@ID-79865.news.uni-berlin.de...

[snip]
// ############### ############### #############
// Computing very long Fibonacci numbers : BEGIN
// Algorithm-2b
// ############### ############### #############


[snip]
Here is Algorithm-2c for computing very long Fibonacci numbers.

Algorithm-2c is an improved version of Algorithm-2b.

// ############### ############### #######
// Computing very long Fibonacci numbers
// Algorithm-2c
// BEGIN
// ############### ############### #######

#include <assert.h>
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
#define MAX_VALUE(x,y) ((x) > (y) ? (x) : (y))
#define ASSERT(x)
// #define ASSERT(x) assert(x)

#define MAX_UNIT_VALUE (ULONG_MAX >> 2)
#define BASE 10
typedef unsigned int uint;
typedef unsigned long ulong;

// ==============
class BigInt
// ==============
{
friend ostream& operator<< (ostream& o, const BigInt& ins_i);

private :
static ulong head_s;
static ulong base_s;
vector<ulong> units_;

static void Set_Full_Base ();
public :
BigInt (ulong unit_i)
{
if (base_s == 1) Set_Full_Base ();
ASSERT (unit_i < base_s);
units_.push_bac k (unit_i);
}

BigInt (BigInt lhs_i, BigInt rhs_i)
{
const ulong max_size = MAX_VALUE (lhs_i.units_.s ize (), rhs_i.units_.si ze ());

lhs_i.units_.re size(max_size);
rhs_i.units_.re size(max_size);
units_.resize(m ax_size);

head_s = 0;
transform (lhs_i.units_.b egin(), lhs_i.units_.en d(), rhs_i.units_.be gin(), units_.begin(), *this);

if (head_s) units_.push_bac k (head_s);

}

ulong operator() (ulong n1, ulong n2)
{
ulong value = n1 + n2 + head_s;
head_s = value/base_s;
return (value%base_s);
}

};
// --------------
inline ostream& operator<< (ostream& os, const BigInt& ins_i)
{
ASSERT (!ins_i.units_. empty ());
for (ulong i = (ins_i.units_.s ize () - 1); i > 0; i--)
{
os << ins_i.units_ [i] << setw (BASE - 1) << setfill ('0');
}
return os << ins_i.units_ [0];
}
// --------------
void BigInt::Set_Ful l_Base ()
{
ASSERT (base_s == 1);

// ------------------
for (ulong i = 1; i <= (BASE - 1); i++)
{
base_s *= BASE;

ASSERT (!(base_s >= MAX_UNIT_VALUE) || (base_s == 0));
ASSERT (BASE * ((base_s/BASE) + 1) < MAX_UNIT_VALUE) ;
ASSERT (base_s != 0);
}
}
// =============== ======
class Fibonacci
// =============== ======
{
private :
vector<BigInt> fibs_;
BigInt get_number (uint n_i = 0);

public :
void show_all () const;
void show_number () const;

Fibonacci (uint n_i = 0) { get_number (n_i); }
~Fibonacci () {}

};
// -----------------------
BigInt Fibonacci::get_ number (uint n_i)
{
const uint cur_size = fibs_.size ();

for (uint i = cur_size; i <= n_i; i++)
{
switch (i)
{
case 0 :
fibs_.push_back (BigInt(0));
break;

case 1 :
if (fibs_.empty ()) fibs_.push_back (BigInt (0));
fibs_.push_back (BigInt (1));
break;

default :
fibs_.push_back (BigInt (get_number (i - 2), get_number (i - 1)));
break;
}
}

ASSERT (n_i < fibs_.size());
return fibs_ [n_i];

}
// -----------------------
void Fibonacci::show _all () const
{
ostringstream oss;

for (uint i = 0; i < fibs_.size (); i++)
{
oss << "Fib [" << i << "] = " << fibs_ [i] << "\n";
}
cout << oss.str();
}
// -----------------------
void Fibonacci::show _number () const
{
ostringstream oss;

oss << "Fib [" << (fibs_.size() - 1) << "] = " << fibs_.back() << "\n";

cout << oss.str();
}
// -------------------
ulong BigInt::head_s (0);
ulong BigInt::base_s (1);
// =============== ===============
#define ALL_FIBS "all"
#define ONE_FIB "th"

int main (int argc, char **argv)
{
string option;
if ((argc <= 2) || !(((option = argv[2]) == ALL_FIBS) || (option == ONE_FIB)))
{
cerr << "\tUSAGE : " << argv[0] << " " << "<N - number> " << ALL_FIBS << "|" << ONE_FIB << endl;
if (argc < 2) return 1;

cerr << "\t " << "-----------------------" << endl;
cerr << "\t " << setw(3) << std::left << ALL_FIBS << " - Fibonacci [0 - N]" << endl;
cerr << "\t " << setw(3) << std::left << ONE_FIB << " - Fibonacci [N]" << endl;
cerr << "\t " << "-----------------------" << endl;
return 2;
}

Fibonacci fib(atoi(argv[1]));
(option == ALL_FIBS) ? fib.show_all() : fib.show_number ();

return 0;
}
// ############### ############### #######
// END
// Algorithm-2c
// Computing very long Fibonacci numbers
// ############### ############### #######

// ############### ############### ##
// Comparative performance : BEGIN
// ############### ############### ##
=============== ==============
Windows 2000 Professional
Intel(R) Celeron(R) CPU 1.70. GHz
CYGWIN_NT-5.0 1.5.4(0.94/3/2)
GNU g++ version 3.3.1 (cygming special)
GNU time 1.7
=============== ==============
Complexity of two algorithms has been compared while computing
* Fibonacci-5000,
* Fibonacci-10000,
* Fibonacci-25000.
Note. Output has been redirected to a file.
------------
Algorithm-2b
------------

Fibonacci-5000
--------------
[1] 0.22 real, 0.20 user, 0.03 sys, 14960 max-resident
[2] 0.22 real, 0.22 user, 0.03 sys, 14960 max-resident
[3] 0.22 real, 0.22 user, 0.02 sys, 14960 max-resident
[4] 0.23 real, 0.20 user, 0.03 sys, 14960 max-resident
[5] 0.22 real, 0.20 user, 0.03 sys, 14960 max-resident
Fibonacci-10000
---------------
[1] 0.52 real, 0.50 user, 0.04 sys, 21552 max-resident
[2] 0.52 real, 0.49 user, 0.04 sys, 21552 max-resident
[3] 0.52 real, 0.50 user, 0.05 sys, 21552 max-resident
[4] 0.52 real, 0.51 user, 0.03 sys, 21552 max-resident
[5] 0.52 real, 0.51 user, 0.03 sys, 21552 max-resident
Fibonacci-25000
---------------
[1] 2.01 real, 1.94 user, 0.09 sys, 20144 max-resident
[2] 2.02 real, 1.97 user, 0.06 sys, 20144 max-resident
[3] 2.00 real, 1.96 user, 0.05 sys, 20144 max-resident
[4] 2.00 real, 1.91 user, 0.11 sys, 20144 max-resident
[5] 2.00 real, 1.96 user, 0.05 sys, 20144 max-resident

------------
Algorithm-2c
------------

Fibonacci-5000
--------------
[1] 0.19 real, 0.17 user, 0.03 sys, 12384 max-resident
[2] 0.20 real, 0.19 user, 0.03 sys, 12384 max-resident
[3] 0.19 real, 0.18 user, 0.02 sys, 12384 max-resident
[4] 0.19 real, 0.17 user, 0.04 sys, 12384 max-resident
[5] 0.20 real, 0.18 user, 0.03 sys, 12384 max-resident
Fibonacci-10000
---------------
[1] 0.46 real, 0.45 user, 0.03 sys, 21600 max-resident
[2] 0.46 real, 0.42 user, 0.05 sys, 21600 max-resident
[3] 0.46 real, 0.44 user, 0.03 sys, 21600 max-resident
[4] 0.47 real, 0.43 user, 0.04 sys, 21600 max-resident
[5] 0.46 real, 0.45 user, 0.03 sys, 21600 max-resident
Fibonacci-25000
---------------
[1] 1.91 real, 1.86 user, 0.05 sys, 12608 max-resident
[2] 1.92 real, 1.87 user, 0.07 sys, 12608 max-resident
[3] 1.91 real, 1.83 user, 0.09 sys, 12608 max-resident
[4] 1.91 real, 1.88 user, 0.05 sys, 12608 max-resident
[5] 1.91 real, 1.85 user, 0.07 sys, 12608 max-resident
// ############### ###############
// Comparative performance : END
// ############### ###############
--
=============== =============== =======
Alex Vinokur
mailto:al****@c onnect.to
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=============== =============== =======

Jul 19 '05 #28
fazavon
1 New Member
you guys are making this to hard... i have this and it works great

#include "stdafx.h"
#include <iostream>
using namespace std;

unsigned long fibo(unsigned long n, unsigned long x)
{
long sum;
cout << x << " ";
sum=n + x;
if (n < 214748364725552 875423484564368 465463456756432 163748648675412 )
{
return sum + fibo(x,sum);
}
else
{
return x;
}
};

int main(int argc, char* argv[])
{
int y;
fibo(0,1);
cin >> y;
return 0;
}
Jul 25 '06 #29

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2344
by: Alex Vinokur | last post by:
An algorithm which computes very long Fibonacci numbers http://groups.google.com/groups?selm=bnni5p%2412i47o%241%40ID-79865.news.uni-berlin.de was used as a performance testsuite to compare speed of the code produced by various compilers. =========================================================== Windows 2000 Professional Ver 5.0 Build 2195 Service Pack 2 Intel(R) Celeron(R) CPU 1.70 GHz GNU time 1.7 (to get the real time used)
5
7745
by: Niks | last post by:
Can anybody explain me what is a "Fibonacci search"? even an URL will do. Thanks for reading.
4
4861
by: YS Sze | last post by:
If you know the exact longitude and latitude for a specific location, would anyone think it'd make any sense to find out if this set of location numbers is really part of the Fibonacci series or not? Or, another way to look at this is that: Would anyone of you think it is worth a while to find out if there is any location on earth with the set of longitude and latitude numbers that coincides with the Fibonacci series? As I see it, if...
62
5449
by: jugaaru | last post by:
How to generate fibonacci mubers in C ?
8
10974
by: srinpraveen | last post by:
I know to write a program to print the fibonacci series. But the problem is my teacher has asked us to write a program to print the natural numbers that are not involved in the fibonacci series. For example if the user gives 7 terms of the series to be displayed, then the display of the fibonacci series is 0, 1,1, 2, 3, 5, 8. But the natural numbers not involved are 4, 6 and 7. That's what my teacher wants. But I am struggling to write a...
13
3174
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that creates a character string containing the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) = F(1) = 1 -, separated by comma. n should be given as an argument to the program. The recursive function should only take one parameter, n, and...
6
4975
by: Andrew Tatum | last post by:
I'm having some problems with the below equation. I have no problems when it comes to positives. Negatives create the problem.. C 2 1 4 However, this doesn't work:
8
3194
by: help2008 | last post by:
Hi I have been doing this working on an assignment for the last week and have stumbled across a part which I cant get my head around. I was hoping that someone could explain what I am missing. I do not expect you to do my work for me. I have completed the rest of the assignment and I am just stuck here (at the very end). Here is my problem. "I have to find out the decimal values of the ratios of consecutive fibonacci numbers." As I said...
1
8837
by: altaey | last post by:
Question Details: Write a program to find and print a Fibonacci sequence of numbers. The Fibonacci sequence is defined as follow: Fn = Fn-2 + Fn-1, n >= 0 F0 = 0, F1 = 1, F2 = 1 Your program should prompt the user to enter a limit and indicate whether the last number in the sequence printed is either even or odd.
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.