题目2:在斐波那契数列中,找出4百万以下的项中值为偶数的项之和
本帖最后由 欧拉计划 于 2017-1-14 17:41 编辑Even Fibonacci numbers
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
题目:
斐波那契数列中的每一项被定义为前两项之和。从 1 和 2 开始,斐波那契数列的前十项为:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
考虑斐波那契数列中数值不超过 4 百万的项,找出这些项中值为偶数的项之和。
def ou(x):
o =
a,b = 1,2
for i in range(2,x):
a,b=b,a+b
if b % 2 ==0:
o.append(b)
else:
return sum(o) def ou(x):
a,b =1,1
globaladd
for i in range(1,x):
a,b = b,a+b
if b % 2 == 0:
add = add + b 本帖最后由 HALLDY 于 2015-6-26 13:05 编辑
# include<stdio.h>
int main()
{
int i=2,j=1,q,total;
while(j<4000000)
{
q=i;
i=i+j;
j=q;
if(i%2==0)
{
total+=i;
}
}
printf("%d\n",total);
return 0;
} 本帖最后由 翅膀团 于 2015-11-16 14:08 编辑
我的解答是:
#include <stdio.h>
int main(void)
{
int a=0,b=1,result=0,i;
for(i=0;i<=4000000;i++)
{
a = a + b;
b = a + b;
if(a % 2 ==0)
{
result += a;
}
else if(b % 2 ==0)
{
result += b;
}
}
printf("%d\n",result);
}
如果有错误希望指出 def Fi(x):
yield 1
yield 2
n=1
n2=2
y=3
while x>y:
yield y
y=n+n2
n=n2
n2=y
sum(x for x in Fi(4000000) if not(x%2))
#include<stdio.h>
void main()
{
int f1 = 1, f2 = 2, f3;
int n =0;
double i = 3.0;
long int j = 4, sum = 0;
while(j<4000000)/*先算出不超过的一共有几项*/
{
j = f1 +f2;
f1 = f2;
f2 = j;
n++;
}
f1 =1;
f2 =2;
while(n - 1 > 0)/*判断是否为偶数如果是就 加到sum中*/
{
f3 = f1 + f2;
f1 = f2;
f2 = f3;
if(f3%2!=1)
{
sum += f3;
n--;
}
else
{
n--;
}
}
printf("sum = %ld\n",sum+2);
}
#include <stdio.h>
int main()
{
long int i,j,k,sum;
i = 1;
j = 2;
sum = 0;
while (j < 4000000)
{
if(j % 2 == 0)
{
sum += j;
}
k = j;
j += i;
i = k;
}
printf("%d\n",sum);
return 0;
}
答案输出4613732 瞬秒爆加速 发表于 2015-4-26 11:53
你好,我想问下,为什么我复制了你的代码贴进去 计算出来不显示结果呀?是我哪里操作错误了吗? 能帮忙告诉一下吗? 谢谢 鸡汤不加盐 发表于 2015-12-22 00:34
你好,我想问下,为什么我复制了你的代码贴进去 计算出来不显示结果呀?是我哪里操作错误了吗? 能帮忙告 ...
我用的是 python 写的 SUM = 0
l1 =
while(True):
n = l1[-1]+l1[-2]
if n > 4000000:
break
else:
l1.append(n)
for n in l1:
if n % 2 == 0:
SUM += n
print(SUM)
本帖最后由 ianv 于 2016-3-15 10:27 编辑
#include<stdio.h>
#include<stdlib.h>
int fib(int i)
{
if (i>1)
return fib(i-1)+fib(i-2);
else
{
if (0==i)
return 0;
if (1==i)
return 1;
}
}
int main()
{
int i=0;
long sum=0;
int N=100;
for (i;i<N;i++)
{
if(fib(i)<4000000)
{
if(fib(i)%2==0)
{
printf("%d\t",fib(i));
sum+=fib(i);
}
}
else
{
printf("finish!!");
break;
}
}
printf("\nsum=%d",sum);
getchar();
}
先用递归写了一个斐波那契数列,然后再测试程序中判断 sum = 0
f1 = 1
f2 = 1
f3 = 2
while f3 < 4000000:
if f3 % 2 == 0:
sum += f3
f1 = f2
f2 = f3
f3 = f1 + f2
print(sum)
运行还要比较缓慢 def fib(max_n=4000000):
a, b = 0, 1
while b < max_n:
if b%2 == 0:
yield b
a, b = b, a + b
print(sum(fib())) def fibs(num):
result =
for i in range(num-2):
result.append(result[-2]+result[-1])
return result
fib1 = fibs(34)
sum = 0
for i in fib1:
if i % 2 ==0:
sum +=i
print(sum)
a =
while a[-1]+a[-2]<4000000:
a.append(a[-1]+a[-2])
print(sum(num for num in a if num%2==0)) a,b=1,1
c=a+b
s=0
while c<=4000000:
if c%2==0 : s=s+c
c=a+b
a,b=b,c
print(s)
答案4613734 result = 0
a = 1
b = 1
c = a + b
while (c <= 4000000):
if c % 2 == 0:
result += c
a = b
b = c
c = a + b
print(c)
#include<stdio.h>
void main()
{
int a, b, c, sum = 0;
for (a = 1, b = 1, c = a + b;c <= 4000000;c++ )
{
if (c % 2 == 0)
sum += c;
a = b;
b = c;
c = a + b;
}
printf("sum = %d\n",sum);
}
两个代码结果不一样,求大神指出问题 此题的数据结果比较大,用long long类型比较好。。
4048803411799
#include<cstdio>
void sum(int n){
long long SUM=2;
int a=1,b=2,i;
for(i=3;i<=n;i++){
int t=b;
b=a+b;
a=t;
if(0==i%2)SUM+=b;
}
printf("%lld",SUM);
}
int main()
{
sum(4000000);
//4000000
return 0;
} #include<stdio.h>
int main()
{
int i=1,j=2;
long long sum=0;
long long k,a=2;
for(k=1;k<=4000000;k++)
{
sum=i+j;
i=j;
j=sum;
if(sum%2==0)
{
a+=sum;
}
}
printf("偶数的和是%d\n",a);
return 0;
}