zjhnz
发表于 2018-1-4 10:41:46
#include <stdio.h>
int main(void)
{
long f1=1,f2=2;
long f3=0;
long sum=2;
while( f3 < 4000000 )
{
f3=f2+f1;
if ( f3 % 2==0)
{
sum+=f3;
}
f1=f2;
f2=f3;
}
printf("%ld",sum);
}
artistlu
发表于 2018-2-8 12:30:38
#include <stdio.h>
#define MAX 4000000
int main(void)
{
int first = 1;
int second = 2;
int tmp;
long sum = 0;
while (first <= MAX && second <= MAX )
{
if (second % 2 == 0)
{
sum += second;
}
tmp = first;
first = second;
second += tmp;
}
printf("%ld\n", sum);
return 0;
}
°蓝鲤歌蓝
发表于 2018-3-4 20:39:51
t = lambda x: x if x < 2 else t(x-1)+t(x-2)
sum1 = sum( if not j%2])
victor.xu
发表于 2018-3-14 12:27:54
def fib(n):
if n == 1:
return 1
elif n == 2:
return 2
else:
return fib(n - 1) + fib(n - 2)
lis = []
i = 1
s = 0
while fib(i) <= 4000000:
lis.append(fib(i))
i += 1
for j in lis:
if j % 2 == 0:
s += j
print('Fib数列为:',lis)
print('Fib数列中偶数和为:%d' % s)
阿bang
发表于 2018-3-26 14:43:07
import time
def fibosum(maxnum):
global begin
begin = time.time()
a = 1
b = 2
sum = 2
while b < maxnum:
a, b =b, a + b
if b % 2 == 0:
sum += b
return sum
print fibosum(4000000)
print time.time() - begin
python
独我雅想
发表于 2018-4-1 17:26:08
public class Forehe {
public static void main(String[] args) {
int sum = 2;
for (int a = 1,b = 2, c = a+b;c <4000000;c = a+b)
{
if(c % 2 == 0)
{
sum += c;
}
a = b;
b = c;
}
System.out.println(sum);
}
}
soulwyb
发表于 2018-4-11 20:21:55
def fbnq2():
x = 1
y = 2
n = 0
lis = []
while n<= 4000000:
n = y
y += x
x = n
if n % 2 == 0:
lis.append(n)
return sum(lis)
print fbnq2()
soulwyb
发表于 2018-4-11 20:22:50
结果是4613732
li1347zhen
发表于 2018-5-22 15:28:59
#include<stdio.h >
int main()
{
long int sum = 2;
long int f1, f2, f3;
f1 = 1;
f2 = 2;
f3 = f1 + f2;
while (f3 < 4000000) {
f1 = f2;
f2 = f3;
f3 = f1 + f2;
if (f2 % 2 == 0) {
sum += f2;
}
}
printf("%d\n", sum);
return 0;
}
野花菜子
发表于 2018-6-22 10:48:19
def Fibonacci(n):
n1 = 1
n2 = 1
n3 = 1
while (n-2) > 0:
n3 = n2 + n1
n1 = n2
n2 = n3
n -= 1
return n3
list1 = []
sum = 0
for n in range(1, 100):
if Fibonacci(n) < 4000000:
list1.append(Fibonacci(n))
for each in list1:
if each % 2 == 0:
sum += each
print(sum)
1314xxxxxx
发表于 2018-7-10 21:31:46
Fibonacci = list()
evenNumber_in_Fibonacci = list() #verrible2
num_of_terms = 100000
num1 = int(1)
num2 = int(1)
num3 = int()
Fibonacci.extend()#First term and second term
for i in range(num_of_terms): #four million terms
Fibonacci.append(Fibonacci[-2] + Fibonacci[-1])
for i in range(1, num_of_terms):
evenNumber_in_Fibonacci.append(Fibonacci) if i % 3 == 0 else None
sum_of_verrible2 = sum(evenNumber_in_Fibonacci)
最多到十万(只用了一秒)……不然就会卡死(指数级上升的感觉,四十万压力会很大)
如果用C的话可能会好点,差不多到一百万左右
shotgun
发表于 2018-7-16 17:46:33
#include <stdio.h>
int main(void)
{
int i=1,j=2,k,sum=0;
while(i<4000000)
{
k=j;
j=j+i;
i=k;
if(j%2==0)
{
sum+=j;
}
}
printf("%d",sum);
return 0;
}
思考了有一阵子翻了翻前面的答案才想明白{:10_258:}
ゆりお姉さん
发表于 2018-7-19 17:29:41
a,b = 1,1
c = []
while b<4000000:
a,b = b,a+b
if b % 2 == 0:
c.append(b)
a = 0
for each in c:
a += each
print(a)
塔利班
发表于 2018-8-23 10:37:46
def fib():
a,b=0,1
while b<4000000:
a,b=b,a+b
if not b%2:
yield b
print(sum(fib()))
茶侘酒寂
发表于 2018-8-25 23:49:02
python
i1 = 1
i2 = 2
t = 2
while 1:
j = i1 + i2
i1 = i2
i2 = j
if (j > 4000000):
break
if (j%2 == 0):
t = t+j
print(t)
yuc_mi
发表于 2018-9-18 11:16:32
#斐波那契数列中的每一项被定义为前两项之和。从 1 和 2 开始,斐波那契数列的前十项为:
#1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
print("考虑斐波那契数列中数值不超过 4 百万的项,找出这些项中值为偶数的项之和")
def fab(n):
he = 0
a1 = 1
a2 = 1
a3 = 0
while a3 < n:
a3 = a1 + a2
a1 = a2
a2 = a3
if a3 % 2 == 0:
print(a3)
he += a3
return he
n = int(input('请输入一个正数:'))
print(fab(n))
shanczp
发表于 2018-10-9 19:13:55
DAY 发表于 2016-7-11 10:31
此题的数据结果比较大,用long long类型比较好。。
4048803411799
..你的总和是i<=400万之中偶数和,并不是题目要的偶数和。而且你前十个真的试过吗。。
shanczp
发表于 2018-10-9 19:16:47
输出结果:4613732
int i=0,k=0,sum=0;
int j=1;
for (; j < 4000000; )
{
k=j;
j+=i;
i=k;
if (j%2==0)
sum+=j;
}
printf("%d",sum);
system("pause");
cupbbboom
发表于 2018-11-13 08:06:45
n1 = 1
n2 = 2
sum = 2
def fib(n1,n2):
n3 = n1 + n2
if n3 <= 4000000:
if n3 % 2 == 0:
global sum
sum += n3
return fib(n2,n3)
else:
print('上一个数为:%s'%str(n2))
print('现在已经到达 %s'%str(n3))
print('目前总和为:%s'%str(sum))
fib(n1,n2)
计算速度,0.1秒{:5_109:}
我的答案:4613732
最后一个项中值:3524578
跳出函数项中值:5702887
cupbbboom
发表于 2018-11-13 08:10:52
DAY 发表于 2016-7-11 10:31
此题的数据结果比较大,用long long类型比较好。。
4048803411799
这么大吗?{:5_94:}