Re: Fibonacci numbers

From: rossum (rossum48_at_coldmail.com)
Date: 10/12/03


Date: Sun, 12 Oct 2003 14:13:56 +0100

On 10 Oct 2003 18:02:07 -0700, dleecurt@cc.usu.edu wrote:

>Here is the code that I wrote, It is quite simple. Dose anyone have
>and advice on fixing it.I am compiling it under Linux with gcc.
>
>
>#include<iostream.h>
#include <iostream>
>
>int main(void)
int main()
>{
> long fib1 = 1;
> long fib2 = 1;
> long fib3;
> long fib4;
>
> while (fib4 < 400000000)
> {
> fib3 = fib1 + fib2;
> fib4 = fib3 + fib2;
> fib1 = fib3;
> fib2 = fib4;
>
> cout << fib3 << endl;
> cout << fib4 << endl;
            fib3 = fib1 + fib2;
            fib4 = fib3 + fib2;
            fib1 = fib3;
            fib2 = fib4;

            std::cout << fib3 << std::endl;
            std::cout << fib4 << std::endl;
> }
> return 0;
>}
>
>I would appriciate any advice or examples on how make the code do what
>I want.
>Lee

Lee,

I don't know exactly how much you know so my apologies if this is at
the wrong level for you. First three minor points:

1 Replace <iostream.h> with <iostream>. The ".h" headers are
non-standard and only for backward compatibility so new code should
avoid them. With the standard headers everything is declared in the
std namespace so you will need "std::cout" and so on.

2 int main(void) is unusual style, int main() is more usual. Using
void like that looks like C rather than C++.

3 You are not indentating the body of your while loop.

The major point is that you cannot fit anything into a long beyond the
maximum you have already reached. You will have to create your own
big integer class to hold larger numbers. Other people have already
made some suggestions as to how you might implement it.

Looking at your code you only do six things with your fib# variables:
you declare them, assign to them from a long, assign to them from each
other, compare them to 400000000, add them and output them. There is
also an implicit destruction when they go out of scope. Your big
integer class will have to do those six things and possibly have a
Destructor as well depending on the implementation you pick.

You will need to implement:

  Constructor
  Destructor [if needed]
  operator= [assign from a long]
  operator= [assign from a big integer]
  operator+
  operator<
  operator<< [stream output, not a bit shift]

You will need to decide on the details of your chosen implementation
and provide at least the six or seven functions needed for the minimal
big integer class. Make sure you know how big the largest number you
want to hold is. You mentioned 10^50 which has 51 digits; Fib(5000)
has over 1000 digits.

Lastly, there is at least one extra function that you are going to
need for your class. It is not something that you actually do in your
code, but it is implied by what you have written. I am sure that you
will notice it once you start to try out your new class.

rossum

--
The Ultimate Truth is that there is no Ultimate Truth


Relevant Pages

  • class __getitem__ when item is not a sequence ???
    ... How can I use an instance's data by reference to the instance name, ... instance of an integer class. ... Since it refers to only a single value ... type like int, float etc. ...
    (comp.lang.python)
  • Re: Need some help please
    ... //convert integer to string ... //init an array with numbers 0-9 ... int a; ... Integer class) ...
    (microsoft.public.dotnet.vjsharp)
  • Re: Double to Int and back
    ... I'm not sure if this is the smartest way, ... Use Integer Class: ... First make an integer instance: ... value to int. ...
    (comp.lang.java.help)
  • Re: Double to Int and back
    ... I'm not sure if this is the smartest way, ... Use Integer Class: ... First make an integer instance: ... value to int. ...
    (comp.lang.java.help)
  • Re: Some questions
    ... The destructor will never be called, and your program will leave blobs ... imagine somearg[] as a collection of base objects. ... Thus foo should be able to add something to that collection (and could ... int gid; ...
    (comp.lang.cpp)