问题:
The Fibonacci sequence is defined by the recurrence relation:
Fn = Fn1 + Fn2, where F1 = 1 and F2 = 1.
Hence the first 12 terms will be:
F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144
The 12th term, F12, is the first term to contain three digits.
What is the first term in the Fibonacci sequence to contain 1000 digits?
答案:4782
#include
char s1[1000]={0};
char s2[1000]={0};
void sum(char *s1, char *s2, int len)
{
int flag=0, ex=0;
int i = 0, k = 0;
for (i=0; i {
k = (flag ? ex : 0) + s1[i]+ s2[i];
if (k<10)
{
s1[i]=k;
flag = 0;
}else
{
s1[i] = k%10;
flag=1;
ex= k / 10;
}
}
}
int main(void)
{
s1[0]=1;
s2[0]=1;
int i=2;
while(++i)
{
if(i%2 == 1)
{
sum(s1,s2,1000);
if(s1[999]) break;
}
else
{
sum(s2,s1,1000);
if(s2[999]) break;
}
}
printf("the first term in the Fibonacci sequence to contain 1000 digits is %d\n ", i);
return 0;
}
阅读(683) | 评论(0) | 转发(0) |