yhhpf
发表于 2020-8-21 15:09:43
l =
i = 1
sum = 2
while i <= 4000000:
i += 1
x = l+l
if x > 4000000:
break
l.append(x)
if x % 2 == 0 :
sum += x
print(l)
print(sum)
不会高数~~~平平无奇方案= =
4444567
发表于 2020-8-29 00:07:32
from time import time
def sum1(x):
a,b = 1,2
c = 2
while b<x+1:
a,b = b,a+b
if b % 2 == 0:
c = c + b
print("所求偶数项之和为 %s" % c)
t1 = time()
sum1(4000000)
t2 = time()
t = t2 -t1
print(f"运行时间为: {t} 秒")
所求偶数项之和为 4613732
运行时间为: 0.012990713119506836 秒
huanghuiyv
发表于 2020-9-12 09:57:28
sum = 2
f1 = 1
f2 = 2
f3 = 0
while f3 < 4000000:
f3 = f1 + f2
if f3 % 2 == 0:
sum += f3
f1 = f2
f2 = f3
print("在斐波那契数列中,找出4百万以下的项中值为偶数的项之和为:" + str(sum))
4613732
丨游戏灬需要
发表于 2020-10-21 17:59:21
def func(maxNum =4000000):
def fib(maxNum):
a =0
b =1
while True:
nextNum =a+b
if nextNum >maxNum:
break
else:
yield nextNum
a ,b =b ,nextNum
f =fib(maxNum)
allFib =
print(allFib)
evenNum_fib =
print(evenNum_fib)
result =sum(evenNum_fib)
return result
print(func())
gonorth
发表于 2020-11-4 20:19:43
def fib(n):
if n == 1:
return 1
if n == 2 :
return 2
if n > 2:
return fib(n-1) + fib(n-2)
a = []
for i in range(1, 10):
x = fib(i)
if x % 2 == 0:
a.append(x)
count = 0
for i in a:
count = count + i
print (count)
Dakyo
发表于 2020-11-26 10:23:37
每天进步一点点
xg-sco
发表于 2020-12-26 09:40:50
#include <stdio.h>
#define MAX 100
int main ()
{
int f = {1, 2};
int i, sum = 2;
printf("%d %d", f, f);
for (i = 2; i < MAX; i++)
{
f = f + f;
if (f > 4000000)
{
break;
}
printf("%d ", f);
if (f % 2 == 0)
{
sum += f;
}
}
printf("\n");
printf("sum = %d\n", sum);
return 0;
}
kaidada
发表于 2021-3-8 21:31:11
#include <stdio.h>
int fib( int i )
{
if( 1 == i )
{
return 1;
}
else if( 2 == i )
{
return 2;
}
else
{
return fib(i-1) + fib(i-2);
}
}
int main()
{
int sum = 0;
int i = 1;
while( fib(i) <= 4000000 )
{
if( fib(i)%2 == 0 )
{
sum += fib(i);
}
i++;
}
printf("%d\n",sum);
return 0;
}
{:10_256:}
hjg
发表于 2021-3-28 00:34:06
本帖最后由 hjg 于 2021-3-28 00:35 编辑
斐波那契 有3种解法自顶向下 带记录的自顶向下迭代式的从下往上
这边我直接写最优的解法java
https://xxx.ilovefishc.com/album/202103/28/003339dsx8elglwxrzredr.png
public static void main(String[] args) {
Solution s = new Solution();
System.out.println(s.fib(s.shu())
+"最接近4000000的那个斐波第:"+ s.shu()+"个数。。。");
}
static class Solution {
public int shu(){
while (true){
//递推关系
int prev = 0,curr = 1,count=0;
for (int i = 2;;i++){
int sum = prev + curr;
prev = curr;
curr = sum;
count++;
if (sum >4000000){
break;
}
}
return count;
}
}
//最优化解法
public double fib(int n) {
double count = 0;
//base case
if (n==0 || n==1) return n;
//递推关系
int prev = 0,curr = 1;
for (int i = 2;i<= n;i++){
int sum = prev + curr;
prev = curr;
curr = sum;
if (sum % 2 == 0){
count = count+sum;
}
}
return count;
}
}
hjg
发表于 2021-3-28 00:36:16
hjg 发表于 2021-3-28 00:34
斐波那契 有3种解法自顶向下 带记录的自顶向下迭代式的从下往上
这边我直接写最优的解法java
...
4613732
@0.0@sos
发表于 2021-3-31 19:55:57
int main(int argc, char *argv[]) {
int a=1;
int b=2;
int sum=0;
int result=0;
while(b<4000000){
if(a%2==0){
result=result+a;
}
if(b%2==0){
result=result+b;
}
if(sum%2==0){
result=result+sum;
}
sum=a+b;
a=b+sum;
b=a+sum;
}
printf("The result is:%d",result);
return 0;
}
手残待拯救
发表于 2021-4-20 05:43:53
需求:斐波那契数列中数值不超过 4 百万的项,找出这些项中值为偶数的项之和。
拆解:斐波那契数列,不超过 4 百万的项,值偶数的项。
检查:偶数的项求和(由于400万项,我的电脑顶不住,这里给出10项)(便于验证)
代码:
i,a,b = 0,1,2
list1 =[]
while i < 10:
list1.append(a)
c = a + b
a,b = b,c
i +=1
print(list1)
z = sum(x for x in list1 if not x % 2)
print(z)
最后
list1 =
z = 44
Kuri5u
发表于 2021-5-2 20:50:10
def fbnq():
added1=1
added2=1
result2=0
for num in range(3,100):
result1=added1+added2
added1=result1
added2=result1-added2
if result1%2==0:
result2+=result1
if result1>=4000000:
break
return result2
print(fbnq())
P.S斐波那契数列第100项已经远大于4000000了
中国好青年
发表于 2021-5-14 17:40:01
import time
T1 = time.perf_counter()
a = 1
b = 2
c = 0
summary = []
for i in range(31):
if b < 4000000 :
summary.append(b)
a,b = b,a+b
else:
continue
for each in summary:
if each % 2 == 0:
c += each
else:
continue
print(c)
print(summary)
T2 =time.perf_counter()
print('程序运行时间:%s毫秒' % ((T2 - T1)*1000))
461373234.97040000000007毫秒
wcq15759797758
发表于 2021-7-9 15:07:01
q = 0
a = 1
b = 1
c = 2
while c < 4000000:
if c % 2 == 0:
q = q + c
a = b
b = c
c = a + b
print(q)
卢本伟牛逼
发表于 2021-8-6 23:30:01
#include <stdio.h>
static int count;
static unsigned int result;
int fib1(int i)
{
count += 1;
if (i<=2) return 1;
return fib1(i-1) + fib1(i-2);
}
int fib2(int n)
{
int a = 1, b = 1;
int i;
if (n <= 2) return 1;
while(1)
{
if (a + b > n) break;
count += 1;
b = a + b;
a = b - a;
if (!(b%2)) result += b;
}
return b;
}
int main(void)
{
int i;
printf("%d\n", fib2(4000000));
printf("count = %d\n", count);
printf("result is %u\n", result);
return 0;
}
鱼塘里的鱼儿
发表于 2021-10-1 14:29:37
#include <iostream>
using namespace std;
int main()
{
int a=1,b=2,c=2,i=3;
while(i<4000001)
{
c=a+b;
a=b;
b=c;
i++;
}
cout<<c;
return 0;
}
ft215378
发表于 2021-10-7 19:41:03
fibonacci = []
for i in range(1,4000000):
if i <= 2:
fibonacci.append(i)
else:
fibonacci.append(fibonacci + fibonacci)
f = fibonacci
sum = 0
for i in f:
if i % 2 == 0:
sum += i
print(sum)
ft215378
发表于 2021-10-8 15:39:04
#斐波那契数列中值不超过4百万的项中值为偶数的项的和
#迭代dibonacci数列
def fibonacci(n):
n1 = 1
n2 = 1
n3 = 1
if n < 1 :
print('输入有误!')
return -1
while (n-2) > 0:
n3 = n2 + n1
n1 = n2
n2 = n3
n -= 1
return n3
#计算值为偶数的项之和
i = 2
result = 0
while i:
if fibonacci(i) > 4000000:
break
elif fibonacci(i) % 2 == 0:
result += fibonacci(i)
i += 1
else:
i += 1
print(result)
舞铲阶级
发表于 2021-10-16 14:51:03
#求斐波那契数列四百万内项数和
a =
sum = 0
for i in range(2,10000):
a.append(a+a)
if a<4000000:
if a%2 == 0:
sum=sum+a
else:
break
print(sum)