番杰
发表于 2021-10-25 16:32:22
#include<stdio.h>
#define MAX 4000000L
int main(void)
{
int result = 0,temp = 0;
int i1 = 1,i2 = 2;
while(i2 < MAX)
{
if(i2 % 2 == 0)
result += i2;
temp = i1 + i2;
i1 = i2;
i2 = temp;
}
printf("%d",result);
return 0;
}
learner-ray
发表于 2021-11-12 00:38:42
结果:sum = 9024243749793644159
#include<stdio.h>
int main(void)
{
unsigned long long int a1 = 1, a2 = 2;
unsigned int i;
unsigned long long int sum = a2;
unsigned long long int a;
for (i = 3; i < 300000; i++)
{
a = a1 + a2;
if (i % 2 == 0)
{
sum = sum + a;
}
else
{
;
}
a1 = a2;
a2 = a;
}
printf("sum = %llu\n", sum);
return 0;
}
鱼塘里的鱼儿
发表于 2022-4-17 09:53:25
本人使用Java写的 运行结果为4613762
int a=0,b=1,c=0,sum=0;
for(;c<=4000000;){
c=a+b;
a=b;
b=c;
if(c%2==0){
sum+=c;
}
清风揽月shine
发表于 2022-5-20 21:34:00
limit = 4000000
def even_sum():
fibonacci_list =
f0 = 1
f1 = 2
while True:
f1 = f0 + f1
f0 = f1 - f0
fibonacci_list.append(f1)
if f1 > limit:
break
even_list =
result = sum(even_list)
print(result)
雪舞凝霜
发表于 2022-7-24 19:21:05
#include <stdio.h>
#include <stdlib.h>
#define MAX 4000000
long fibs(int n) //求斐波那契数列
{
if(n==0)
return 0;
else if (n==1 || n==2)
return 1;
else
return fibs(n-1)+fibs(n-2);
}
int main()
{
int i;
long iFib,iSum;
iFib=0;
iSum=0;
for(i=1;iFib<MAX;i++){
iFib=fibs(i);
if(iFib%2==0)
iSum+=iFib;
}
printf("斐波那契数列小于4000000的偶数项之和为:%ld\n",iSum);
system("pause");
return 0;
}
Asss-whom
发表于 2022-8-9 12:28:35
pub fn run() -> Result<(), Box<dyn std::error::Error>> {
let (mut a, mut b) = (1u32, 2u32);
let mut sum = 0u32;
while b < 4000000 {
if b % 2 == 0 {
sum += b;
}
(a, b) = (b, a + b)
}
println!("{sum}");
Ok(())
}
4613732
erdf99
发表于 2022-11-8 19:56:16
int fibonacci_even(int n) {//在斐波那契数列中,找出n以下的项中值为偶数的项之和
int a = 1, b = 2, now = 0,sum=2;
while ((a + b) < n) {
now = a + b;
if (now % 2 == 0) { sum = sum + now; }
a = b;
b = now;
}
return sum;
}
元豪
发表于 2022-11-27 08:25:26
C++:
#include <iostream>
using namespace std;
int main(){
int a = 0, b = 1, a2 = 0, sum;
while (b < 4000000){
if (b % 2 == 0){
sum += b;
}
a2 = a;
a = b;
b = b + a2;
}
cout << sum << endl;
return 0;
}
python:
a = 0
b = 1
sum_ = 0
while b < 4000000:
if b % 2 == 0:
sum_ += b
a, b = b, b + a
print(sum_)
TLM
发表于 2022-12-19 19:15:32
python
#暴力循环,O(n)
NUM=4000000
f1=0
f2=1
num=0
while f2<NUM:
f2,f1=f2+f1,f2
if f2%2==0 and f2<NUM:
num=num+f2
print(num)
优化后O(1),其实数据太少都不值得优化
from math import sqrt
from math import log
# #函数表达,数列通项式,和的数列的通项式
# for i in range(10):
# print(sqrt(5)/5*(((1+sqrt(5))/2)**i)-sqrt(5)/5*(((1-sqrt(5))/2)**i))
def f(i):
return sqrt(5)/5*(((1+sqrt(5))/2)**i)-sqrt(5)/5*(((1-sqrt(5))/2)**i)
#获取N,预估加精确
N=int(log(NUM)/log((1+sqrt(5))/2))+1
if f(N)>NUM:
while f(N)>NUM:
N=N-1
N=N-1
else:
while f(N)<=NUM:
N=N+1
N=N-2
# N大起来就不行了,double位数不够
a=1/4*(3/sqrt(5)+1)
b=1/4*(3/sqrt(5)-1)
k1=2+sqrt(5)
k2=2-sqrt(5)
n=(N+1)//3
num2=a*(k1**n)-b*(k2**n)-1/2
print(int(num2))
funcE
发表于 2024-6-4 00:24:35
#include<iostream>
using namespace std;
int main(){
int sum=2,a=1,b=2;
//通过观察序列规律,第3,6,9,...项必为偶数,所以直接求出并累加即可
while(1){
for(int i=0;i<3;i++){
b+=a;
a=b-a;
}
if(b<4000000) sum+=b; else break;
}
cout << sum << endl;
return 0;
}
输出:
4613732
--------------------------------
Process exited after 0.01115 seconds with return value 0
请按任意键继续. . .
欢迎指正
funcE
发表于 2024-6-4 00:30:41
幻世伽蓝 发表于 2016-7-3 17:13
两个代码结果不一样,求大神指出问题
第二个代码fou循环的设置错了
for(a=1,b=1;c<4000000;c=a+b){
if(c%2==0) sum+=c;
a=b;
b=c;
}
Ian_Li
发表于 2024-9-11 18:38:56
C++练习打卡 day2
#include <iostream>
using namespace std;
int main() {
int sum = 0;
int a = 1;
int b = 2;
while (a <= 4000000) {
if (a % 2 == 0) sum += a;
int temp = a + b;
a = b;
b = temp;
}
cout << sum << endl;
return 0;
}