| | |
fibonocci sequence question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
What is the first term in the Fibonacci sequence to contain 1000 digits?
so i made a program that finds the fibonocci's sequence (fs).
I tried to do it recursively but it takes too long for big numbers. so i made a manual one.
BUt as i count how many digits there are for each (fs). a problem exist. After F(1476)--which has 309 digits, the program stops suddenly. I think its because of memory space or something. Any ways, this is why I turned to you. any help
here is my code
try running the program to get a better understanding , and then maybe you can help be better. thanks
so i made a program that finds the fibonocci's sequence (fs).
I tried to do it recursively but it takes too long for big numbers. so i made a manual one.
BUt as i count how many digits there are for each (fs). a problem exist. After F(1476)--which has 309 digits, the program stops suddenly. I think its because of memory space or something. Any ways, this is why I turned to you. any help
here is my code
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<fstream> #include<iomanip> #include<cmath> using namespace std; int countdigits(unsigned long double x) { int count(0); while(x>.99999999999999) { x/=10; count++; } return count; } int main(){ unsigned long double result = 0.0; unsigned long double x = 1.0; unsigned long double y = 1.0; unsigned long double result2 = 0.0; int count(0); for(int i = 3;;i++) { result = x+y; y = x; x = result; result2 = result; cout<<setprecision(10)<<"F of "<<i<<" is : "<<result/1000000<<" count is : "<<countdigits(result2)<<endl; if(( countdigits(result2) == 1000 ) ) { cin.get(); } } return 0; }
try running the program to get a better understanding , and then maybe you can help be better. thanks
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
Double's range is higher than int's range, but it's still way less than 1000 digits, at least on my machine.
http://www.cplusplus.com/reference/s...ic_limits.html
It's doubtful you're going to be able to do this problem with the normal data types.
http://www.cplusplus.com/reference/s...ic_limits.html
It's doubtful you're going to be able to do this problem with the normal data types.
•
•
Join Date: Mar 2008
Posts: 1,400
Reputation:
Solved Threads: 113
C++ Syntax (Toggle Plain Text)
unsigned long double result = 0.0; unsigned long double x = 1.0; unsigned long double y = 1.0; unsigned long double result2 = 0.0;
As you only need to use integers, you should try using a 64-bit integer variable type instead of a double, as using a double will also give you inaccurate results. But you wont be able to get accurate big numbers using just the standard C++ library, you will need to use a seperate big number library or class such as this.
Hope this helps.
•
•
Join Date: Jan 2009
Posts: 4
Reputation:
Solved Threads: 0
use a dp solution
#include<iostream.h>
#include<string.h>
#include<vector.h>
#include<iterator.h>
int main()
{
vector<long long int>v1(101);
vector<long long int>v2(101);
int i=0;
for(;i<101;i++)
{
if(i==0||i==1)
v1[i]==1;
else
v1[i]=i;
}
v2[0]=v2[1]=1;
i=2;
for(;i<101;i++)
{
v2[i]=v2[i-1]*v1[i];
}
cout<<"nter no for which u want factorial";
istream_iterator<long long int>is(cin);
ostream_iterator<long long int>os(cout,"\n");
int a;
a=*is;
*os=v2[a];
cout<<"the factorialof "<<a<<"is";
*os;
return 0;
}
#include<iostream.h>
#include<string.h>
#include<vector.h>
#include<iterator.h>
int main()
{
vector<long long int>v1(101);
vector<long long int>v2(101);
int i=0;
for(;i<101;i++)
{
if(i==0||i==1)
v1[i]==1;
else
v1[i]=i;
}
v2[0]=v2[1]=1;
i=2;
for(;i<101;i++)
{
v2[i]=v2[i-1]*v1[i];
}
cout<<"nter no for which u want factorial";
istream_iterator<long long int>is(cin);
ostream_iterator<long long int>os(cout,"\n");
int a;
a=*is;
*os=v2[a];
cout<<"the factorialof "<<a<<"is";
*os;
return 0;
}
![]() |
Other Threads in the C++ Forum
- Previous Thread: Visual C++ 2008 playing a sound
- Next Thread: for and while HELP
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






