Re: Fibonacci [5,000,000] contains 1044938 decimal digits
- To: mathgroup at smc.vnet.net
- Subject: [mg17201] Re: Fibonacci [5,000,000] contains 1044938 decimal digits
- From: Alex Vinokur <alexander.vinokur at telrad.co.il>
- Date: Mon, 26 Apr 1999 01:20:42 -0400
- Organization: Deja News - The Leader in Internet Discussion
- References: <7f4ffu$6dj$1@nnrp1.dejanews.com> <Mo2OZGACWlF3Ewez@raos.demon.co.uk>
- Sender: owner-wri-mathgroup at wolfram.com
In article <Mo2OZGACWlF3Ewez at raos.demon.co.uk>,
Sunil Rao <sunil.rao at ic.ac.uk> wrote:
> Alex Vinokur <alexander.vinokur at telrad.co.il> wrote, and I reply...
> >Several large Fibonacci numbers were calculated using only
> >the well-known explicit formula:
> > Fib (0) = 0, Fib (1) = 1,
> > Fib (n) = Fib (n-1) + Fib (n-2), n >= 2
> >All the (decimal) digits of these numbers were obtained.
> >
> >It was using the EXPLICIT Fibonacci formula and
> > computing ALL the large Fibonacci numbers digits
> >that were set as an object.
> >Using just EXPLICIT Fibonacci formula is not an imperative requirement.
> >But to get ALL digits of the large Fibonacci number is very advisable one.
>
> Can you post your code?
[snip]
Hi,
Yes, I can.
Here is one.
Alex
#########################################################
=== File #1 of 3 : fib_class_of_alex_vinokur.H ==========
------------------- C++ code : BEGIN --------------------
// ==============================================================
//
// Copyright (c) 1999 by Alex Vinokur. This work and all works
// derived from it may be copied and modified without any
// restrictions other than that a copy of this copyright notice
// must be included in any copy of this work or any derived work.
//
// ==============================================================
static char id_h[] = "@(#)Author ## "__FILE__" ::: Alex Vinokur";
// ##############################################################
// =============================
// Very Large Fibonacci Numbers
// =============================
//
// FILE : fib_class_of_alex_vinokur.H
//
// AUTHOR : Alex Vinokur
//
// DESCRIPTION :
// Template classes :
// - avDesirableLongInt
// - avClassTemplateFibonacci
// - avClassTemplateOneFibonacci
// - avClassDecUnitFibonacci
// - avClassDecUnitOneFibonacci
// - avClassHexUnitFibonacci
// - avClassHexUnitOneFibonacci
//
// DATE VERSION
// ---- -------
// Apr-22-1999 AVF 1.0
//
// ##############################################################
#ifndef __fib_class_of_alex_vinokur_H
#define __fib_class_of_alex_vinokur_H
//###===###===###===###===###===###===###===###===###===###
#include <assert.h>
#include <string>
#include <strstream>
#include <vector>
#include <iomanip.h>
//#######################################################
//##### PART#1 : DEFINES & CONSTANTS ####################
//#######################################################
#define MAX_VALUE(x,y) ((x) > (y) ? (x) : (y))
#define ASSERT(x) assert (x)
#define INFO_MESSAGE
const long unsigned int Max_Unit_Value_CNS = (ULONG_MAX >> 2);
const unsigned int INFO_period_CNS = 10000;
static const string UNITS_Delimeter_CNS = "";
//#######################################################
//##### PART#2 : DECLARATIONS ###########################
//#######################################################
template <unsigned int EXPO, unsigned int UNIT_BASE>
class avDesirableLongInt;
template <unsigned int EXPO, unsigned int UNIT_BASE>
avDesirableLongInt<EXPO, UNIT_BASE> operator+ (
const avDesirableLongInt<EXPO, UNIT_BASE>&
left_i,
const avDesirableLongInt<EXPO, UNIT_BASE>&
right_i
);
//#######################################################
//##### PART#3 : The avDesirableLongInt class ###########
//#######################################################
//#############################
template <unsigned int EXPO, unsigned int UNIT_BASE>
class avDesirableLongInt
{
template <unsigned int EXPO, unsigned int UNIT_BASE>
friend avDesirableLongInt<EXPO, UNIT_BASE> operator+ (
const avDesirableLongInt<EXPO,
UNIT_BASE>& left_i,
const avDesirableLongInt<EXPO,
UNIT_BASE>& right_i
);
private :
long unsigned int full_base_;
vector <long unsigned int> vector_unit_;
public : avDesirableLongInt (); avDesirableLongInt (long unsigned int
unit_value_i); avDesirableLongInt ( const avDesirableLongInt<EXPO,
UNIT_BASE>& left_i, const avDesirableLongInt<EXPO, UNIT_BASE>& right_i );
~avDesirableLongInt () {} void set_full_base (); string getValueComment
(ios& show_base_i (ios&)) const; string getStrValue (ios& show_base_i
(ios&)) const; string getAllInfo (ios& show_base_i (ios&)) const; unsigned
int getSize (ios& show_base_i (ios&)) const; static unsigned int getSize_S
(const string& strValue_i); unsigned int getTotalUnits () const {return
vector_unit_.size ();} };
//=====================
template <unsigned int EXPO, unsigned int UNIT_BASE>
avDesirableLongInt<EXPO, UNIT_BASE> operator+ (
const avDesirableLongInt<EXPO, UNIT_BASE>&
left_i,
const avDesirableLongInt<EXPO, UNIT_BASE>&
right_i
)
{
avDesirableLongInt<EXPO, UNIT_BASE> ret_avDesirableLongInt_Value;
const long unsigned int max_size_CNS = MAX_VALUE (
left_i.vector_unit_.size (),
right_i.vector_unit_.size ()
);
long unsigned cur_big_value;
long unsigned int head = 0;
for (long unsigned int theIndex = 0; theIndex < max_size_CNS;
theIndex++)
{
ASSERT (head < Max_Unit_Value_CNS);
cur_big_value =
((theIndex < left_i.vector_unit_.size ()) ?
left_i.vector_unit_ [theIndex] : 0) +
((theIndex < right_i.vector_unit_.size ()) ?
right_i.vector_unit_ [theIndex] : 0) +
head;
//------------------------
ret_avDesirableLongInt_Value.vector_unit_.push_back
(cur_big_value%(left_i.full_base_));
ASSERT (ret_avDesirableLongInt_Value.vector_unit_
[ret_avDesirableLongInt_Value.vector_unit_.size () - 1] < left_i.full_base_);
//------------------------
head = cur_big_value/(left_i.full_base_);
} // for (long unsigned int theIndex = 0; theIndex < max_size_CNS;
theIndex++
ASSERT (ret_avDesirableLongInt_Value.vector_unit_.size () ==
max_size_CNS);
if (head)
{
ret_avDesirableLongInt_Value.vector_unit_.push_back (head);
}
//======================
return ret_avDesirableLongInt_Value;
//======================
} // avDesirableLongInt<EXPO, UNIT_BASE> operator+
//=====================
// Constructor-0
template <unsigned int EXPO, unsigned int UNIT_BASE>
avDesirableLongInt<EXPO, UNIT_BASE>::avDesirableLongInt ()
{
set_full_base ();
}
//=====================
// Constructor-1
template <unsigned int EXPO, unsigned int UNIT_BASE>
avDesirableLongInt<EXPO, UNIT_BASE>::avDesirableLongInt (long unsigned int
unit_value_i)
{
set_full_base ();
if (!(unit_value_i < full_base_))
{
cout << "FATAL ERROR : Number Value = "
<< unit_value_i
<< " (too big)"
<< "; It must be less "
<< full_base_
<< "; "
<< __FILE__
<< ", #"
<< __LINE__
<< endl;
exit (1);
}
ASSERT (unit_value_i < full_base_);
vector_unit_.push_back (unit_value_i);
}
//=====================
// Constructor-2
template <unsigned int EXPO, unsigned int UNIT_BASE>
avDesirableLongInt<EXPO, UNIT_BASE>::avDesirableLongInt (
const avDesirableLongInt<EXPO, UNIT_BASE>&
left_i,
const avDesirableLongInt<EXPO, UNIT_BASE>&
right_i
)
{
set_full_base ();
(*this) = left_i + right_i;
}
//=====================
template <unsigned int EXPO, unsigned int UNIT_BASE>
string avDesirableLongInt<EXPO, UNIT_BASE>::getStrValue (ios& show_base_i
(ios&)) const
{
string ret_Value;
strstream tmp_strstream;
ASSERT ((show_base_i == dec) | (show_base_i == hex) | (show_base_i ==
oct));
//====================================
if (!vector_unit_.empty ())
{
for (long unsigned int theIndex = (vector_unit_.size () - 1);
theIndex > 0; theIndex--)
{
tmp_strstream << show_base_i;
tmp_strstream << vector_unit_ [theIndex];
tmp_strstream << UNITS_Delimeter_CNS;
tmp_strstream << setw (EXPO)
<< setfill ('0');
}
tmp_strstream << show_base_i;
tmp_strstream << vector_unit_ [0];
}
else
{
tmp_strstream << "NoValue";
}
//====================================
//====================================
tmp_strstream << dec;
tmp_strstream << ends;
ret_Value = tmp_strstream.str(); tmp_strstream.rdbuf()->freeze (0);
return ret_Value;
}
//===================== template <unsigned int EXPO, unsigned int UNIT_BASE>
string avDesirableLongInt<EXPO, UNIT_BASE>::getAllInfo (ios& show_base_i
(ios&)) const { string ret_Value; string stringValue = getStrValue
(show_base_i); strstream tmp_strstream;
tmp_strstream << stringValue;
tmp_strstream << "; Size = ";
tmp_strstream << getSize_S (stringValue);
tmp_strstream << getValueComment (show_base_i);
tmp_strstream << ends;
ret_Value = tmp_strstream.str(); tmp_strstream.rdbuf()->freeze (0);
return ret_Value;
}
//=====================
template <unsigned int EXPO, unsigned int UNIT_BASE>
unsigned int avDesirableLongInt<EXPO, UNIT_BASE>::getSize (ios& show_base_i
(ios&)) const
{
return getSize_S (getStrValue (show_base_i));
}
//===================== // static template <unsigned int EXPO, unsigned int
UNIT_BASE> unsigned int avDesirableLongInt<EXPO, UNIT_BASE>::getSize_S (const
string& strValue_i) { int counter = 0; string::size_type ret_index; string
cur_substr = strValue_i; if (!UNITS_Delimeter_CNS.empty ()) { while
(!((ret_index = cur_substr.find (UNITS_Delimeter_CNS)) == string::npos)) {
counter++; cur_substr = cur_substr.substr (ret_index + 1); } // while }
//======================
return (strValue_i.size () - counter*UNITS_Delimeter_CNS.size ());
//======================
}
//=====================
template <unsigned int EXPO, unsigned int UNIT_BASE>
string avDesirableLongInt<EXPO, UNIT_BASE>::getValueComment (ios& show_base_i
(ios&)) const
{
string ret_Value;
ASSERT ((show_base_i == dec) | (show_base_i == hex) | (show_base_i ==
oct));
ret_Value += " (";
//=======================
if (show_base_i == hex)
{
ret_Value += "hex";
}
if (show_base_i == oct)
{
ret_Value += "oct";
}
if (show_base_i == dec)
{
ret_Value += "dec";
}
//=======================
ret_Value += " digits)";
return ret_Value;
}
//=====================
template <unsigned int EXPO, unsigned int UNIT_BASE>
void avDesirableLongInt<EXPO, UNIT_BASE>::set_full_base ()
{
ASSERT (EXPO > 1);
ASSERT ((UNIT_BASE == 8) || (UNIT_BASE == 10) || (UNIT_BASE == 16));
//==================
full_base_ = 1;
for (long unsigned int theIndex = 1; theIndex <= EXPO; theIndex++)
{
full_base_ *= UNIT_BASE;
if ((full_base_ >= Max_Unit_Value_CNS) || (full_base_ == 0))
{
cout << "FATAL ERROR : EXPO Value = "
<< EXPO
<< " (too big)"
<< "; It must be less "
<< theIndex
<< " (Note! UNIT_BASE == "
<< UNIT_BASE
<< ")"
<< __FILE__
<< ", #"
<< __LINE__
<< endl;
exit (1);
}
ASSERT (UNIT_BASE * ((full_base_/UNIT_BASE) + 1) <
Max_Unit_Value_CNS);
ASSERT (full_base_ != 0);
}
}
//#############################
//#######################################################
//##### PART#4 : The avClassTemplateFibonacci class #####
//#######################################################
//#############################
//=====================
template <unsigned int EXPO, unsigned int UNIT_BASE>
class avClassTemplateFibonacci
{
private :
vector< avDesirableLongInt<EXPO, UNIT_BASE> > fib_vector_;
protected :
avClassTemplateFibonacci (int n_i = 0);
public :
string getAllNumbers (ios& show_base_i (ios&)) const;
virtual avDesirableLongInt<EXPO, UNIT_BASE> getFibNumber
(int n_i = 0);
virtual ~avClassTemplateFibonacci () {}
};
//-----------------------
// Constructor
template <unsigned int EXPO, unsigned int UNIT_BASE>
avClassTemplateFibonacci<EXPO, UNIT_BASE>::avClassTemplateFibonacci (int n_i)
{
getFibNumber (n_i);
}
//-----------------------
template <unsigned int EXPO, unsigned int UNIT_BASE>
avDesirableLongInt<EXPO, UNIT_BASE> avClassTemplateFibonacci<EXPO,
UNIT_BASE>::getFibNumber (int n_i)
{
const unsigned int cur_size = fib_vector_.size ();
//========================
if (n_i < 0)
{
cout << "FATAL ERROR : n = "
<< n_i
<< "; "
<< __FILE__
<< ", #"
<< __LINE__
<< endl;
exit (1);
}
for (unsigned int i = cur_size; i <= (unsigned int) n_i; i++) { switch
(i) { case 0 : fib_vector_.push_back (avDesirableLongInt<EXPO, UNIT_BASE>
(0)); break;
case 1 : if (fib_vector_.empty ()) { fib_vector_.push_back
(avDesirableLongInt<EXPO, UNIT_BASE> (0)); } fib_vector_.push_back
(avDesirableLongInt<EXPO, UNIT_BASE> (1)); break;
default :
fib_vector_.push_back (
avDesirableLongInt<EXPO,
UNIT_BASE> (
getFibNumber (i - 2),
getFibNumber (i - 1)
//fib_vector_ [i - 2],
//fib_vector_ [i - 1]
)
);
break;
} // switch (n_i)
//=============================
#ifdef INFO_MESSAGE
if ((i%INFO_period_CNS == 0) & (i > 0))
{
cout << "INFO"
<< " ("
<< __FILE__
<< ", Line#"
<< __LINE__
<< ")"
<< " : "
<< " Fib#"
<< i
<< " calculated; Total Units = "
<< fib_vector_ [fib_vector_.size () -
1].getTotalUnits ()
<< endl;
}
#endif
//=============================
} // for (int i = cur_size; i <= n_i; i++)
//========================
return fib_vector_ [n_i];
} // long unsigned int avClassTemplateFibonacci::getFibNumber (int n_i)
//-----------------------
template <unsigned int EXPO, unsigned int UNIT_BASE>
string avClassTemplateFibonacci<EXPO, UNIT_BASE>::getAllNumbers (ios&
show_base_i (ios&)) const
{
string ret_Value;
strstream tmp_strstream;
for (unsigned int i = 0; i < fib_vector_.size (); i++)
{
tmp_strstream << "Fib [" << i << "] = "
<< fib_vector_ [i].getAllInfo (show_base_i)
<< endl;
}
tmp_strstream << ends;
ret_Value = tmp_strstream.str(); tmp_strstream.rdbuf()->freeze (0);
//==========
return ret_Value;
} // string avClassTemplateFibonacci::getAllNumbers () const
//#######################################################
//##### PART#5 : The avClassTemplateOneFibonacci class ##
//#######################################################
//#############################
//=====================
template <unsigned int EXPO, unsigned int UNIT_BASE>
class avClassTemplateOneFibonacci : public avClassTemplateFibonacci<EXPO,
UNIT_BASE>
{
private :
protected :
avClassTemplateOneFibonacci (int n_i = 0) :
avClassTemplateFibonacci (n_i) {}
public : avDesirableLongInt<EXPO, UNIT_BASE> getFibNumber (int n_i = 0);
~avClassTemplateOneFibonacci () {} };
//-----------------------
template <unsigned int EXPO, unsigned int UNIT_BASE>
avDesirableLongInt<EXPO, UNIT_BASE> avClassTemplateOneFibonacci<EXPO,
UNIT_BASE>::getFibNumber (int n_i)
{
vector < avDesirableLongInt<EXPO, UNIT_BASE> > one_vector_;
one_vector_.push_back (avDesirableLongInt<EXPO, UNIT_BASE> (0));
one_vector_.push_back (avDesirableLongInt<EXPO, UNIT_BASE> (1));
one_vector_.push_back (avDesirableLongInt<EXPO, UNIT_BASE> (0));
//========================
if (n_i < 0)
{
cout << "FATAL ERROR : n = "
<< n_i
<< "; "
<< __FILE__
<< ", #"
<< __LINE__
<< endl;
exit (1);
}
//========================
for (int theIndex = 0; theIndex < n_i; theIndex++)
{
one_vector_.erase (one_vector_.begin ());;
one_vector_.push_back (one_vector_ [0] + one_vector_ [1]);
#ifdef INFO_MESSAGE
if ((theIndex%INFO_period_CNS == 0) & (theIndex > 0))
{
cout << "INFO"
<< " ("
<< __FILE__
<< ", Line#"
<< __LINE__
<< ")"
<< " : "
<< " Fib#"
<< theIndex
<< " calculated; Total Units = "
<< one_vector_ [2].getTotalUnits ()
<< endl;
}
#endif
}
//========================
return one_vector_ [2];
} // long unsigned int avClassTemplateOneFibonacci::getFibNumber (int n_i)
//#######################################################
//##### PART#6 : The avClassDecUnitFibonacci class ######
//#######################################################
//#############################
//=====================
template <unsigned int EXPO>
class avClassDecUnitFibonacci : public avClassTemplateFibonacci <EXPO, 10>
{
public :
avClassDecUnitFibonacci (int n_i = 0) : avClassTemplateFibonacci<EXPO,
10> (n_i) {}
~avClassDecUnitFibonacci () {}
};
//==========================
typedef avClassDecUnitFibonacci<9> ClassFibonacci;
//==========================
//#######################################################
//##### PART#7 : The avClassDecUnitOneFibonacci class ###
//#######################################################
//############################# //===================== template <unsigned
int EXPO> class avClassDecUnitOneFibonacci : public
avClassTemplateOneFibonacci <EXPO, 10> { public :
avClassDecUnitOneFibonacci (int n_i = 0) : avClassTemplateOneFibonacci<EXPO,
10> (n_i) {} ~avClassDecUnitOneFibonacci () {} };
//==========================
typedef avClassDecUnitOneFibonacci<9> ClassOneFibonacci;
//==========================
//#######################################################
//##### PART#8 : The avClassHexUnitFibonacci class ######
//#######################################################
//#############################
//=====================
template <unsigned int EXPO>
class avClassHexUnitFibonacci : public avClassTemplateFibonacci <EXPO, 16>
{
public :
avClassHexUnitFibonacci (int n_i = 0) : avClassTemplateFibonacci<EXPO,
16> (n_i) {}
~avClassHexUnitFibonacci () {}
};
//#######################################################
//##### PART#9 : The avClassHexUnitOneFibonacci class ###
//#######################################################
//############################# //===================== template <unsigned
int EXPO> class avClassHexUnitOneFibonacci : public
avClassTemplateOneFibonacci <EXPO, 16> { public :
avClassHexUnitOneFibonacci (int n_i = 0) : avClassTemplateOneFibonacci<EXPO,
16> (n_i) {} ~avClassHexUnitOneFibonacci () {} };
//#######################################################
//##### PART#10 : FUNCTIONS #############################
//#######################################################
bool stringIsDigit (const string& string_i);
bool stringIsNonDigit (const string& string_i);
void printAllFib (
long unsigned int n_i,
const string& msg_i = string (),
ios& show_base_i (ios&) = dec
);
void printOneFib (
long unsigned int n_i,
const string& msg_i = string (),
ios& show_base_i (ios&) = dec
);
void printSomeFib (
const vector<long unsigned int>& vect_i,
const string& msg_i = string (),
ios& show_base_i (ios&) = dec
);
int mainFib (int argc, char **argv);
//###===###===###===###===###===###===###===###===###===###
#endif // __fib_class_of_alex_vinokur_H
//###################################################
//############ END OF FILE ##########################
//###################################################
------------------- C++ code : END ----------------------
=== File #1 of 3 : fib_class_of_alex_vinokur.H ==========
#########################################################
=== File #2 of 3 : fib_class_of_alex_vinokur.C ==========
------------------- C++ code : BEGIN --------------------
// ==============================================================
//
// Copyright (c) 1999 by Alex Vinokur. This work and all works
// derived from it may be copied and modified without any
// restrictions other than that a copy of this copyright notice
// must be included in any copy of this work or any derived work.
//
// ==============================================================
static char id[] = "@(#)Author ## "__FILE__" ::: Alex Vinokur";
// ##############################################################
// =============================
// Very Large Fibonacci Numbers
// =============================
//
// FILE : fib_class_of_alex_vinokur.C
//
// AUTHOR : Alex Vinokur
//
// DESCRIPTION :
// Functions :
// - stringIsDigit
// - stringIsNonDigit
// - printAllFib
// - printOneFib
// - printSomeFib
// - printErrorUsageMessage
// - argv_check
// - mainFib
//
// DATE VERSION
// ---- -------
// Apr-22-1999 AVF 1.0
//
// ##############################################################
#include "fib_class_of_alex_vinokur.H"
//###################################################
//############ FUNCTION #############################
//###################################################
//#####################################################
bool stringIsDigit (const string& string_i)
{
ASSERT (!string_i.empty ());
for (long unsigned int theIndex= 0; theIndex < string_i.size ();
theIndex++)
{
if (!isdigit (string_i [theIndex]))
{
return false;
}
}
return true;
} // bool stringIsDigit (const string& string_i)
//#####################################################
bool stringIsNonDigit (const string& string_i)
{
ASSERT (!string_i.empty ());
for (long unsigned int theIndex= 0; theIndex < string_i.size ();
theIndex++)
{
if (isdigit (string_i [theIndex]))
{
return false;
}
}
return true;
} // bool stringIsNonDigit (const string& string_i)
//#####################################################
void printAllFib (
long unsigned int n_i,
const string& msg_i,
ios& show_base_i (ios&)
)
{
string print_string;
strstream tmp_strstream;
if (!msg_i.empty())
{
tmp_strstream << endl;
tmp_strstream << "################################" << endl;
tmp_strstream << "###" << "\t" << msg_i << endl;
tmp_strstream << "################################" << endl;
}
tmp_strstream << ClassFibonacci (n_i).getAllNumbers (show_base_i);
tmp_strstream << endl;
tmp_strstream << ends;
print_string = tmp_strstream.str(); tmp_strstream.rdbuf()->freeze (0);
cout << print_string;
} // printAllFib
//#####################################################
void printOneFib (
long unsigned int n_i,
const string& msg_i,
ios& show_base_i (ios&)
)
{
string print_string;
strstream tmp_strstream;
if (!msg_i.empty())
{
tmp_strstream << endl;
tmp_strstream << "################################" << endl;
tmp_strstream << "###" << "\t" << msg_i << endl;
tmp_strstream << "################################" << endl;
}
ClassOneFibonacci f1;
tmp_strstream << "Fib#"
<< n_i
<< " = "
<< f1.getFibNumber (n_i).getAllInfo (show_base_i)
<< endl;
tmp_strstream << endl;
tmp_strstream << ends;
print_string = tmp_strstream.str(); tmp_strstream.rdbuf()->freeze (0);
cout << print_string;
} // printOneFib
//#####################################################
void printSomeFib (
const vector<long unsigned int>& vect_i,
const string& msg_i,
ios& show_base_i (ios&)
)
{
string print_string;
strstream tmp_strstream;
if (!msg_i.empty())
{
tmp_strstream << endl;
tmp_strstream << "################################" << endl;
tmp_strstream << "###" << "\t" << msg_i << endl;
tmp_strstream << "################################" << endl;
}
ClassFibonacci f1;
for (unsigned int theIndex = 0; theIndex < vect_i.size (); theIndex++)
{
tmp_strstream << "Fib#"
<< vect_i [theIndex]
<< " = "
<< f1.getFibNumber (vect_i [theIndex]).getAllInfo
(show_base_i)
<< endl;
} // for (unsigned int theIndex = 0; theIndex < vect_i.size ();
theIndex++)
tmp_strstream << endl;
//====================
tmp_strstream << ends;
print_string = tmp_strstream.str(); tmp_strstream.rdbuf()->freeze (0);
cout << print_string;
} // printSomeFib
//#####################################################
//#####################################################
//#####################################################
void printErrorUsageMessage (
int argc,
char **argv
)
{
cout << endl
<< "FATAR ERROR!"
<< endl
<< "============"
<< endl
<< "USAGE : "
<< argv[0]
<< " All <number>"
<< endl
<< " : "
<< argv[0]
<< " <number> [<number>, <number>, ...]"
<< endl;
}
//#####################################################
bool argv_check (
int argc,
char **argv,
long unsigned int cur_index_i,
long unsigned int& cur_numberValue_o
)
{
//============================
ASSERT (argc >= 2);
ASSERT (cur_index_i >= 1);
//============================
if (!stringIsDigit (string (argv[cur_index_i])))
{
printErrorUsageMessage (argc, argv);
cout << endl
<< "NOT NUMBER : "
<< "argv ["
<< cur_index_i
<< "] = "
<< argv [cur_index_i]
<< endl;
return false;
}
cur_numberValue_o = strtoul (argv[cur_index_i], (char**)NULL, 10);
if (cur_numberValue_o == ULONG_MAX)
{
cout << endl
<< "FATAR ERROR!"
<< endl
<< "============"
<< endl
<< "TOO LARGE VALUE : "
<< "argv ["
<< cur_index_i
<< "] = "
<< argv [cur_index_i]
<< endl;
return false;
}
//=====================
return true;
} // bool argv_check (long unsigned int cur_index_i)
//#####################################################
int mainFib (int argc, char **argv)
{
long unsigned int cur_numberValue;
long unsigned int cur_index;
vector<long unsigned int> vect;
switch (argc)
{
case 0 :
abort ();
break;
case 1 :
printErrorUsageMessage (argc, argv);
exit (1);
break;
case 2 :
cur_index = 1;
if (!argv_check (argc, argv, cur_index,
cur_numberValue))
{
exit (1);
}
printOneFib (cur_numberValue);
break;
case 3 :
if (string ("All") == argv [1])
{
cur_index = 2;
if (!argv_check (argc, argv, cur_index,
cur_numberValue))
{
exit (1);
}
printAllFib (cur_numberValue);
break;
}
default :
for (int theIndex = 1; theIndex < argc; theIndex++)
{
cur_index = theIndex;
if (!argv_check (argc, argv, cur_index,
cur_numberValue))
{
exit (1);
}
vect.push_back (cur_numberValue);
}
printSomeFib (vect);
break;
} // switch (argc)
//=================
return 0;
//=================
} // int mainFib (int argc, char **argv)
//###################################################
//############ END OF FILE ##########################
//###################################################
------------------- C++ code : END ----------------------
=== File #2 of 3 : fib_class_of_alex_vinokur.C ==========
#########################################################
=== File #3 of 3 : fib_main_of_alex_vinokur.C ===========
------------------- C++ code : BEGIN --------------------
// ==============================================================
//
// Copyright (c) 1999 by Alex Vinokur. This work and all works
// derived from it may be copied and modified without any
// restrictions other than that a copy of this copyright notice
// must be included in any copy of this work or any derived work.
//
// ==============================================================
static char id[] = "@(#)Author ## "__FILE__" ::: Alex Vinokur";
// ##############################################################
// =============================
// Very Large Fibonacci Numbers
// =============================
//
// FILE : fib_main_of_alex_vinokur.C
//
// AUTHOR : Alex Vinokur
//
// DESCRIPTION : Main program
//
// DATE VERSION
// ---- -------
// Apr-22-1999 AVF 1.0
//
// ##############################################################
#include "fib_class_of_alex_vinokur.H"
//###################################################
//###################################################
//###################################################
//==============================
int main (int argc, char **argv)
{
return mainFib (argc, argv);
}
//###################################################
//############ END OF FILE ##########################
//###################################################
------------------- C++ code : END ----------------------
=== File #3 of 3 : fib_main_of_alex_vinokur.C ===========
#########################################################
------------------- Compilation : BEGIN -----------------
Compilation Command Line :
g++ -O3 fib_main_of_alex_vinokur.C fib_class_of_alex_vinokur.C
------------------- Compilation : END -==----------------
#########################################################
------------------- Running : BEGIN ---------------------
=========
Running#1
=========
Command Line :
--------------
a.out 12
Output :
-------
Fib#12 = 144; Size = 3 (dec digits)
=========
Running#2
=========
Command Line :
--------------
a.out 5 9 10 12
Output :
-------
Fib#5 = 5; Size = 1 (dec digits)
Fib#9 = 34; Size = 2 (dec digits)
Fib#10 = 55; Size = 2 (dec digits)
Fib#12 = 144; Size = 3 (dec digits)
=========
Running#1
=========
Command Line :
--------------
a.out All 12
Output :
-------
Fib [0] = 0; Size = 1 (dec digits)
Fib [1] = 1; Size = 1 (dec digits)
Fib [2] = 1; Size = 1 (dec digits)
Fib [3] = 2; Size = 1 (dec digits)
Fib [4] = 3; Size = 1 (dec digits)
Fib [5] = 5; Size = 1 (dec digits)
Fib [6] = 8; Size = 1 (dec digits)
Fib [7] = 13; Size = 2 (dec digits)
Fib [8] = 21; Size = 2 (dec digits)
Fib [9] = 34; Size = 2 (dec digits)
Fib [10] = 55; Size = 2 (dec digits)
Fib [11] = 89; Size = 2 (dec digits)
Fib [12] = 144; Size = 3 (dec digits)
------------------- Running : END -----------------------
#########################################################
------------------- Compiler & System ------------------
g++ -v : gcc version egcs-2.91.57 19980901
(egcs-1.1 release)
uname -a : SunOS <nodename> 5.6 Generic_105181-09
sun4m sparc SUNW,SPARCstation-5
---------------------------------------------------------
//#########################################################
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own