芒果加黄桃
发表于 2017-1-5 23:24:37
#计算400万以下的斐波那契数列偶数项之和
from time import time
start = time()
temp =
sum = 0
for i in range(2,100):
t =temp + temp
if t%2 == 0:
sum += t
if t<=4000000:
temp.append(t)
else:
break
print(sum)
end = time()
print('cost %.3f \'s' % (end - start))
marmot
发表于 2017-1-9 20:33:13
本帖最后由 marmot 于 2017-1-9 20:45 编辑
import time
start=time.time()
def feb(n):
if n == 1:
return 1
if n == 2:
return 2
return feb(n-1)+feb(n-2)
sum=0
for n in range(1,2000):
if feb(n) < 4000000 and feb(n)%2 == 0:
sum =sum + feb(n)
if feb(n)>= 4000000:
break
print(sum)
print('%.10f' % (time.time()-start))
————————————————————迭代函数————————————————————
结果:
4613732
10.7411253452
marmot
发表于 2017-1-9 20:45:22
import time
start=time.time()
f1,f2 = 1,2
f3=0 #随便定义
sum=0
while f3 < 4000000:
f3 = f1 + f2 #斐波那契数列传参
f1 = f2
f2 = f3
if f3 % 2 == 0:
sum += f3
print(sum+2)
print('花费时间%.5f' % (time.time()-start))
——————————循环————————————————————
结果:
4613732
花费时间0.00000
渡风
发表于 2017-1-11 18:28:33
使用Matlab编程
Problem2所用时间为0.00036606
Problem2的答案为4613732
function Sum=Problem2(Input)
tic
if nargin==0
Input=4*10^6;
end
Fibonacci=zeros(1,8000);
Fibonacci(1)=1;
Fibonacci(2)=2;
for ii=3:8000
Fibonacci(ii)=Fibonacci(ii-1)+Fibonacci(ii-2);
if Fibonacci(ii)>=Input&&Fibonacci(ii-1)<Input
Node=ii-1;
break
end
end
Temp=Fibonacci(1:1:Node);
for kk=1:Node
if mod(Temp(kk),2)~=0
Temp(kk)=0;
end
end
Sum=sum(Temp);
toc;
disp(['Problem2所用时间为',num2str(toc)])
disp(['Problem2的答案为',num2str(Sum)])
end
kalao
发表于 2017-1-20 16:50:10
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#include<algorithm>
int main()
{
queue<int > Q;
Q.push(1);
Q.push(2);
int sum=0;
int temp1,temp2;
while(!Q.empty())
{
temp1=Q.front();
Q.pop();
if(temp1>4000000)break;
if(!(temp1&1))sum+=temp1;
temp2=Q.front();
Q.push(temp1+temp2);
}//4613732
cout<<sum;
return 0;
}
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#include<algorithm>
int main()
{
int a=1,b=2,c=2;
int sum=0;
while(c<=4000000)
{
if(!(c&1))sum+=c;
c=a+b;
a=b;
b=c;
}//4613732
cout<<sum;
return 0;
}
shinningpika
发表于 2017-1-22 13:42:33
b=1
c=2
a=
d=0
while d<=4000000:
d=b+c
if d<=4000000: a.append(d)
b=c
c=d
sum1=0
for each in a:
if each%2==0:
sum1+=each
print(sum1)
答案4613732
Hughues
发表于 2017-1-24 10:35:39
'''
欧拉计划·2:
斐波那契数列中的每一项都被定义为两项之和
从1和2开始,斐波那契数列的前十项为:
1、2、3、5、8、13、21、34、55、89
求解:斐波那契数列中数值不超过4百万的项,并找出这些项中值为偶数的项之和
'''
def fibonacci() : # 生成斐波那契数列
a, b = 1, 2
max = 40000000
num =
while b < max : # 循环至最大值之前
b, a = a + b, b
if b % 2 == 0 : # 检测是不是偶数
num.append(b)
print(sum(num)) # 求和
{:10_249:}没解决怎么往面输入上限值的问题,希望有会的大神们不吝赐教
FlySelf
发表于 2017-2-2 01:29:05
import time
def fibonacci_1(max_value=10000):
'迭代,找出菲波那切数列中数值不超过4百万的偶数项之和'
value1 = 1
value2 = 2
value = 0
count = value2
while value <= max_value:
value = value1 + value2
value1 = value2
value2 = value
if value % 2 == 0:
count += value
return count
start = time.clock()
print(fibonacci_1(4000000))
end = time.clock()
print('程序执行了%fs。' %(end - start))
执行结果:
4613732
程序执行了0.005757s。
yxf865354395
发表于 2017-2-13 13:02:57
赞一个
piperacillin
发表于 2017-2-15 22:25:23
def fab(num):
a = 1
b = 1
if num == 0 or num == 1:
return num
else:
for i in range(1,num):
c = a+b
a = b
b = c
return b
i = 1
k = 0
while fab(i) <= 4000000:
if fab(i) % 2 == 0:
k +=fab(i)
i +=1
print(k)
邪神降临
发表于 2017-2-18 14:18:22
C++版本
4613732#include<iostream>
using namespace std;
long long Sum(int maxint)
{
long long sum = 2;
int i = 1,j = 2,a=0;
while(a <= maxint)
{
if(a%2 == 0)
sum+=a;
a = i+j;
i = j;
j = a;
}
return sum;
}
int main()
{
long long sum = Sum(4000000);
cout<<sum<<endl;
return 0;
}
0mrli0
发表于 2017-2-19 16:46:49
/*斐波那契数列中,找出4百万以下项中值为偶数的项之和*/
#include <stdio.h>
int main()
{
int i, j, sum, t;
i = 1; j = 1; sum = 0; t = 0;
while(j < 4000000)
{
t = j;
j = i + j;
i = t;
sum += j%2 ? 0 : j;//本来想判断j%2等于0的,将错就错,调换了后面的输出项
}
printf("sum = %d", sum);
return 0;
}
marmot
发表于 2017-2-20 21:13:02
print(""" 斐波那契数列中的每一项被定义为前两项之和。从 1 和 2 开始,斐波那契数列的前十项为:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
考虑斐波那契数列中数值不超过 4 百万的项,找出这些项中值为偶数的项之和。
----------------------------------------------------""")
#斐波拉契数列表
list_feb =
while (True):
n = list_feb[-1] + list_feb[-2]
if n > 4000000:
break
else:
list_feb.append(n)
#取出偶数并相加
aw = 0
for x in list_feb:
if x%2 == 0:
aw += x
print("答案是: " + str(aw))
print("PS小于四百万的斐波拉契数列列表为:" + str(list_feb))
6leensky
发表于 2017-2-24 09:57:44
even_fib = []
a = b = 1
while b<4000000:
a, b = b, a+b
if b%2 == 0:
even_fib.append(b)
sum(even_fib)
我的结果是4613732
NiceB
发表于 2017-2-28 17:28:52
maxnum=4000000
def Fibonacci(old,oldd):
new = old+oldd
if new >maxnum:
return 0
else:
if new%2==0:
return new + Fibonacci(new,old)
else:
return Fibonacci(new,old)
print (Fibonacci(0,1))
4613732
余欲渔
发表于 2017-3-1 14:41:22
python
fibo=
while True:
x=sum(fibo[-2:])
if x<=4000000:
fibo.append(x)
else:break
print(sum(fibo[::2]))
== RESTART: C:\Users\ASUS\AppData\Local\Programs\Python\Python35-32\test.py ==
3524577
>>>
夜魔时生
发表于 2017-3-6 11:08:50
l=
a=0
for i in range(2,1000):
a=l+l
if a>=4000000:
break
l.append(a)
sum=0
for j in l:
if j%2==0:
sum+=j
print(sum)
546397641
发表于 2017-3-17 13:01:01
a=0
b=1
sum = 0
while a<4000000:
a,b=b,a+b
if b<4000000:
# print(b,end=" ")
if b%2==0:
#print(b,end=" ")
sum += b
print(sum)
JonTargaryen
发表于 2017-3-26 15:35:54
本帖最后由 JonTargaryen 于 2017-3-26 15:38 编辑
#include <stdio.h>
int main(void)
{
int num1 = 1, num2 = 1, num3;
int sum = 0;
while(num2 < 4000000)
{
num3 = num1 + num2;
num1 = num2;
num2 = num3;
if(!(num2 % 2))
{
sum += num2;
}
}
printf("%d\n", sum);
return 0;
}
JonTargaryen
发表于 2017-3-26 16:03:37
result = 0
num1 = 1
num2 = 1
while num2 < 4000000:
num3 = num1 + num2
num1 = num2
num2 = num3
if num2 % 2 == 0:
result += num2
print(result)