cupbbboom
发表于 2018-11-13 08:15:38
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.0001秒{:5_109:}
我的答案:4613732
最后一个项中值:3524578
跳出函数项中值:5702887
cupbbboom
发表于 2018-11-13 08:19:22
1314xxxxxx 发表于 2018-7-10 21:31
最多到十万(只用了一秒)……不然就会卡死(指数级上升的感觉,四十万压力会很大)
如果用C的话可能 ...
不要用列表应该会快很多吧
蓦然灬回首
发表于 2018-11-16 10:57:57
#include <stdio.h>
int main(){
int before = {1, 2};
int sum = 2;
int thisone = 0;
for(int i = 0; ; i++){
thisone = before + before;
before = before;
before = thisone;
if(i % 3 == 2){
if(thisone < 4000000)
sum += thisone;
else
break;
}
}
printf("%d", sum);
}
cclovepython
发表于 2018-11-24 15:29:22
for i in range(40):
if b % 2 == 0:
result += b
a,b = b,a+b
if b >= 4000000:
print(a,result)
break
python6
发表于 2018-12-18 21:13:12
4613732
zhaoming
发表于 2019-2-5 13:07:34
#include<stdio.h>
int main()
{
int result1,result2,sum = 0,number = 40000;
long f,f1=1,f2=1;
while(number)
{
f=f1+f2;
f1=f2;
f2=f;
if(f1%2==0)
{
result1 += f1;
}
if(f2%2==0)
{
result2 += f2;
}
f1++;
f2++;
number--;
}
sum = result1+result2;
printf("%d",sum);
return 0;
}
LJYUYU
发表于 2019-2-16 22:53:53
本帖最后由 LJYUYU 于 2019-2-16 22:56 编辑
#include<stdio.h>
#define MAX 4000000
//利用宏变量把最大值定义
int main()
{
int sum;
sum = 2;
//已知前两个第二个为2,一开始sum就定义为2
int i, p=1, q=2, s;
for(i = 3; sum <= MAX; i++){//i:为第几项
s = p + q;//当前项的值
p = q;//当前项的千亿项
q = s;
if(s % 2 == 0)//判断是否为偶数值
sum+=s;
}
printf("%d",sum);
return 0;
}
4613732
996982937
发表于 2019-2-21 14:28:36
#include<stdio.h>
void main()
{
long int a=1,b=2;
long int sum=0;
for(a=1,b=2;a<4000000&&b<4000000;a=a+b,b=b+a)
{
if(a%2==0)
sum+=a;
if(b%2==0)
sum+=b;
}
printf("%d\n",sum);
}
输出结果4613732
夜宵
发表于 2019-3-7 23:15:17
#include<stdio.h>
int main(){
int i=1;
int j=2;
int sum=0;
A : if (i<=4000000&&j<=4000000)
{
if (i%2==0)
{
sum+=i;
}
if (j%2==0)
{
sum+=j;
}
i+=j;
j+=i;
goto A;
}
printf("%d\n",sum);
return 0;
}
4613732
要是不用goto 好像循环不了,为啥呀
夜宵
发表于 2019-3-7 23:31:59
#include<stdio.h>
int main(){
int i=1;
int j=2;
int sum=0;
for (;i<=4000000&&j<=400000;i+=j,j+=i)
{
if (i%2==0)
{
sum+=i;
}
if (j%2==0)
{
sum+=j;
}
}
printf("%d\n",sum);
return 0;
}
这个输出结果为257114。。。为啥呀
愿你
发表于 2019-3-8 21:22:07
#include <stdio.h>
main()
{
int i,j,num,sum;
i=1;
j=2;
sum=2;
do{
num=i+j;
i=j; //2 3
j=num; //3 5
if(num%2==0)
{
sum=sum+num;
}
}while(num<=4000000);
printf("斐波那契数列中数值不超过 4 百万的项中为偶数项的总和为%d\n",sum);
}
我算出的结果为:4613732
ps:想问一句 有答案吗 每个人发的代码都不太一样 有的人计算出来的结果也不太一样呢{:10_266:}也不懂自己算的对不对{:10_257:}
ietar
发表于 2019-4-17 21:19:32
python
4613732
def p2():
def fib(x,x1=1,x2=1):
if x == 1 or x == 2:
return x2
else:
return fib(x-1,x2,x1+x2)
temp = 0
n = 1
while n:
if fib(n)>=4000000:
break
if not fib(n)%2:
temp += fib(n)
n += 1
print(temp)
return
雷克
发表于 2019-5-8 14:16:34
我算的是13504612,对吗{:10_333:}
18553261791
发表于 2019-6-10 10:45:21
#include<cstdio>
using namespace std;
int main(){
int a=1,b=2,c=0;
long long sum=2;
for(int i=1;c<=4000000;i++){
c=a+b;
if(c%2==0)
sum+=c;
a=b;
b=c;
}
printf("%lld",sum);
return 0;
}
doodu
发表于 2019-6-14 14:22:54
def w400_Fsum(a_1):
r = a_1
a =
bb = 0
u = 1
tt = 0
while (tt <= r):
tt = a + a
a.append(tt)
a.pop(0)
if not(a%2) and (a < r):
bb += a
return print(bb)
hi = 4000000
w400_Fsum(hi)
Weiwei7
发表于 2019-6-25 17:26:56
a=input('\n please input a nutural number ');
x=1;
y=2;
s=0;
while i <= a
i = x+y;
x=y;
y=i;
if mod(i,2)==0
s=s+i;
end
end
disp(s+2);
KSD给力熊
发表于 2019-7-13 15:34:03
def fab(n):
n1 = 1
n2 = 1
num =1
while (n-2) > 0:
num = n1+n2
n1 = n2
n2 = num
n -= 1
return num
num_list = []
num3 = 0
for num in range(1,100):
num1 = fab(num)
if num1 < 4000000:
num_list.append(fab(num))
#print(num_list)
for num2 in range(len(num_list)):
if num_list % 2 == 0:
num3 += num_list
print(num3)
叫我专车司机
发表于 2019-7-21 17:02:14
public static void main(String args[]){
long i=1,j=2,count=2;
long temp=0,total=2;
while(count<4000000){
temp=i+j;
if(temp%2==0)
total+=temp;
i=j;
j=temp;
count++;
}
System.out.println(Long.valueOf(total));
}
为什么数据类型是long和int的结果会不一样?
永恒的蓝色梦想
发表于 2019-7-21 18:55:15
本帖最后由 永恒的蓝色梦想 于 2020-4-18 12:55 编辑
sum=0
a=b=1
while b<4000000:
if not b&1:
sum+=b
a,b=b,a+b
print(sum)
RRRex
发表于 2019-8-6 17:45:09
#include <stdio.h>
#include <time.h>
int main()
{
int begin_time, end_time;
begin_time = clock();
int a = 1, b = 2, temp, sum = 0;
while(b < 4000000)
{
if(b % 2 == 0)
{
sum += b;
}
temp = a;
a = b;
b = a + temp;
}
printf("斐波那契数列中数值不超过 4 百万的偶数项之和为%d\n", sum);
end_time = clock();
printf("\n程序一共运行%dms\n", end_time - begin_time);
return 0;
}