RE: 题目2:在斐波那契数列中,找出4百万以下的项中值为偶数的项之和
def x(n):s = 0
a = 0
b = 1
while b < n:
if b % 2 == 0:
s += b
a, b = b, a + b
return s
print(x(4000000))
终于会发代码了。。。
用的VC++6.0,不支持C99,所以最大也只能用unsigned long类型。所以最大数值只能是2^32-1。试了一下,速度还是很快的。
#include <stdio.h>
#include <math.h>
void main()
{
unsigned long s=4294967295,t=1,i;
for(i=2;i<s;i++)
{
if(s%i==0)
{
s=s/i;
t=t*i;
i=2;
}
if(i>sqrt(s)) break;
}
printf("%u\n",t);
} 本帖最后由 impossible 于 2016-8-18 09:39 编辑
是第一项到四百万项,还是最多是四百万值的项;
以下是第一项到四百万项的代码
#include <stdio.h>
#define min(a,b) ((a)>(b)?(b):(a))
#define NUMWIDTH 250000
/*数组的空间,四百万项站多少位我也不知道,但我运行到20万项时数字写到文件时已经有37kb了
*如果超出这个空间就用vector
*我只放九位数字到一个int里,int最后一位放两数相加溢出的数字。数组的最后一个放当前数字站数组到哪一个int。
*/
inline void add(unsigned int *p1,unsigned int *p2);
int main()
{
unsigned int loop=0,i,j={1},k={2},sum={0};
j=0;
k=0;
sum=0;
for(i=2;i<4000000;i+=2){
if(k%2==0){
add(sum,k);
}
add(j,k);
if(j%2==0){
add(sum,j);
}
add(k,j);
}
for(i=NUMWIDTH-2;i!=0;i--)
{
printf("%u",sum);
}
printf("\n");
return 0;
}
inline void add(unsigned int *p1,unsigned int *p2)
{
unsigned int loop=0;
for(loop=0;loop<=min(p1,p2);loop++){
p1+=p2;
p1+=p2/1000000000;
}
if(p1<loop && p1)p1=loop;
}
C:\Users\Administrator\Desktop\2.png #include<stdio.h>
int main()
{
unsigned long long i=0,j=1,g,sum=0;
int a;
for(a=0;a<=4000000;a++)
{
g=i+j;
i=j;j=g;
//printf("%llu\n",g);
if(g%2==0)
{
sum+=g;
}
}
printf("和:%llu\n",sum);
return 0;
}
我的答案是6280710680130704614 i = 0
i1 = 0
i2 = 1
sum1 = 0
while i < 4000000:
if i%2 == 0:
sum1 += i
i = i1 + i2
i2 = i1
i1 = i
print(sum1)
>>> 4613732 def Our(x):
if x % 2:
return False
else:
return True
def FBNQ():
a = 1;b = 1
list1 = []
while a < 4000000:
a,b=b,a+b
if Our(a):
list1.append(a)
return sum(list1)
if __name__=='__main__':
a = FBNQ()
print(a)
答案:4613732 #include <stdio.h>
#include <stdlib.h>
int main()
{
long a;
long i = 3;
a = 1;
a = 2;
long s = 2;
do
{
a = a + a;
if(a % 2==0)
{
s+=a;
}
}while(a<=4000000);
printf("%ld",s);
}
最后答案为4613732 import java.util.ArrayList;
import java.util.List;
public class EvenFibonacciNumbers
{
public static void main(String[] args)
{
List<Integer> li = new ArrayList<Integer>();
li.add(1);
li.add(2);
int sum = li.get(1);
int i = 2;
while(li.get(i-1) + li.get(i-2) <= 4000000)
{
li.add(li.get(i-1) + li.get(i-2));
if(li.get(i)%2 == 0)
sum += li.get(i);
i++;
}
System.out.println("数值在400万内的斐波那契数中偶数的和为:" + sum);
}
}
def fib(x):
if x == 0:
return 0
elif x == 1:
return 1
else:
return fib(x-2) + fib(x-1)
def euler(upper_limit):
list_x = []
count = 0
while fib(count) <= upper_limit:
if not fib(count)%2:
list_x.append(fib(count))
count += 1
return sum(list_x)
if __name__ == '__main__':
print(euler(4000000)) i = 1
j = 2
sum = 2
while True:
k = i
i = j
j = i + k
if j>4000000:
break
if j%2 == 0:
sum = sum + j
print(sum)
答案是4613732 本帖最后由 joker11111 于 2016-10-26 17:34 编辑
//-------------------------------------------
//02--在斐波那契数列中,找到小于4百万的项为偶数的项之和
//-------------------------------------------
#include <windows.h>
#include <iostream>
#include <time.h>
using namespace std;
long int fun(int m);
long int fibo(long int m);
int main()
{
clock_t start, finish;
double totaltime;
start = clock();
long int m = 4000000;
cout << " 结果为:" << fun(m) << endl;
finish = clock();
totaltime = (double)(finish - start) / CLOCKS_PER_SEC;
cout << "此程序的运行时间为" << totaltime << endl;
system("pause");
return 0;
}
long int fun(int m)
{
long int s = 0, i = 1, fb = fibo(1);
while (fb < m)
{
if (0 == fb % 2)
{
s += fb;
}
i++;
fb = fibo(i);
}
return s;
}
long int fibo(long int m)
{
while (m > 0)
{
if (1 == m)
return 1;
else if (2 == m)
return 2;
else
return fibo(m - 1) + fibo(m - 2);
}
}
结果为:4613732
运行时间为:0.828 var i = 1, y = 2, sum = 0;
while(true){
if(!(y%2)){
sum += y;
}
y = y + i;
i = y - i;
if(y > 4000000){
break;
}
} #include<iostream>
#include<stdlib.h>
using namespace std;
int facbosum(int n)
{
int f1=1,f2=2,sum=2,f3=f1+f2;
while(f3<n)
{
f3=f1+f2;
f1=f2;
f2=f3;
if(f3%2==0)
{
sum+=f3;
}
}
return sum;
}
int main()
{
system("color 3f");
cout<<facbosum(4000000);
}
结果是4613732
大家帮忙看看!!!! 本帖最后由 梦想绘制者 于 2016-11-2 17:03 编辑
# Python 3.5实现
# 求数值不超过4E6的斐波那契数列中偶数项的和
# 代码思路:
# 一变生成Fibonacci数列一边判断奇偶性同时求和
# 由于递归算法在数据量大时效率低(且占用大量存储空间)于for/while循环,考虑使用循环算法
def evenFib(nMax):
evenSum = 0
a = b = 1
while True:
a, b = b, a + b
if b < nMax:
if not(b % 2):
evenSum += b
else:
break
return evenSum
eFib = evenFib(4e6)
print('Sum of even terms of Fibonacci sequence within 4 million is %d' %eFib)
>>>
Sum of even terms of Fibonacci sequence within 4 million is 4613732 #include <stdio.h>
int main()
{
int c=0,a=1,b=2,add=2;
while (c<=4000000)
{
c=a+b;
a=b;
b=c;
if (c%2==0)
{
add=add+c;
}
}
printf("%d\n",add);
return 0;
} """
欧拉计划
找出斐波那契数列中数值不超过400万的项,求这些项中为偶数的项之和
"""
fibo=
sum=2
a=0
while a<4000000:
lenf=len(fibo)
a=fibo+fibo
fibo.append(a)
if a%2==0:
sum=sum+a
print("求和结果为:"+str(sum)) def euler02(maxvalue=10000):
"""
斐波那契数列中每一项被定义为前两项之和。从1和2开始,斐波那契数列的前十项为:
1,2,3,5,8,13,21,34,55,89,...
考虑斐波那契数列中数值不超过400万的项,找出这些项中值为偶数的项之和
"""
fiblist =
while fiblist[-1]+fiblist[-2]<=maxvalue:
fiblist.append(fiblist[-1]+fiblist[-2])
return sum()
print(euler02(maxvalue = 4000000))
得到:4613732 本帖最后由 〃陌殇、怀素 于 2016-11-18 14:04 编辑
#include <stdio.h>
#include <stdlib.h>
#define MAX 4000000
int find()
{
int i = 1,j = 2,sum = 0;
int fibonacci = 0; //斐波那契数
for(;fibonacci < MAX ; )
{
fibonacci = i + j;
i = j;
j = fibonacci;
if( fibonacci%2 == 0)
{
sum = fibonacci + sum;
}
}
return sum;
}
void main()
{
int sum = 0;
sum = find();
printf("不大于四百万的斐波那契数中,偶数项的和为:%d",sum);
return 0;
}
def fbnq():
a=1
b=2
while b<=4000000:
if b%2==0:
yield b
a,b=b,a+b
c=fbnq()
sum(i for i in c)
4613732
a = 1
b = 2
i = 0
summation = 0
while i < 4000000:
i = a + b#第3个数为前两数之和
a = b #第2个数的值做为下次计算的第1个数
b = i #将得到的值做为下次计算的第2个数
if i % 2 == 0:
summation += i
print(summation + 2) #2没计算,加上