Fibonacci sequence question

Hello,

I am trying to write a fibonacci sequence generator program, here it is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<new>
 
using namespace std;
 
int main()
{
   int unsigned n,i,*sc;
   cout << "enter how many numbers you want: \n";
   cin >> i;
   sc=new int unsigned [i];
   *sc=0;*(sc+1)=1;
   for (n=2;n<i;n++)
   {
       *(sc+n)=*(sc+(n-2))+*(sc+(n-1));
       cout << *(sc+n) << "\n";
   }
       system("pause");
       return 0;
}       

Everything goes perfectly fine, but after some values, the numbers start going out wrong... I think it's because the last sane value passes the limit of the long int values...which is 4294967295 (unsigned). If that is really my problem, is there any possible way to overcome it? If that isn't the problem, then what is it?

Thanks in advance.
Some compilers accept '(unsigned) long long' (64bit ints).

On an unrelated note: you shouldn't have to include <new>. It's always available.
It works!It's not unlimited, but it gives me way more numbers...Thank you, gaminic.
Nowadays, with the new standard, compilers *have* to accept it. Thank goodness.

The bounds of a signed long long int are −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. The bounds of an unsigned long long int are 0 to somewhere around 1.844674407×1019. I hope that'll be enough space. ;)

EDIT: I'm late, aren't I?

EDIT2: If it absolutely has to be unlimited, then have you seen this? http://www.ginac.de/CLN/

-Albatross
Last edited on
wow, really? 9,223,372,036,854,775,807, signed?? That's REALLY a lot... the last normal number i get is 12,200,160,415,121,876,738 (unsigned), but i dont think i need more than that :P

edit: O.o that sounds like a real badass library xD too bad I don't know how to install 3rd party libraries... I started programming on c++ (for real) like a month ago, so I don't know much. I just made this program for fun, (I didn't even have fibonacci at school yet :P) so it doesn't really have to be unlimited,but it'd be really cool...
Last edited on
Topic archived. No new replies allowed.