尤拉計劃
阿新 • • 發佈:2021-01-19
尤拉計劃
- 1
10以下的自然數中,屬於3和5的倍數的有3,5,6和9,它們之和是23.
找出1000以下的自然數中,屬於3和5的倍數的數字之和。
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define N 1000
int main()
{
long long ans=0;
long long l=(3+(N-1)/3*3)*((N-1)/3)/2;
ans+=l;
l=(5+(N-1)/5*5)*((N-1)/5)/2;
ans+=l;
l=(15+(N-1) /15*15)*((N-1)/15)/2;
ans-=l;
printf("%lld\n",ans);
return 0;
}
- 2
斐波那契數列中的每一項被定義為前兩項之和。從 1 和 2 開始,斐波那契數列的前十項為:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
考慮斐波那契數列中數值不超過 4 百萬的項,找出這些項中值為偶數的項之和。
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
/*
斐波那契數列中的每一個新項都是通過將前兩項相加而生成的。從1和2開始,前10個術語將是:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
通過考慮斐波那契數列中值不超過400萬的項,求偶數項之和。
*/
int main()
{
long long ans=2,a=1,b=2,c=0;
while(b<=4000000)
{
c=b;
b=a+b;
a=c;
//printf("%lld %lld %lld %lld\n",i,a,b,c);
if(b%2==0)ans+=b;
}
printf("%lld\n",ans);
return 0;
}
- 3
最大質因數
思路:從i=2開始除去所有因數,直到n=i
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
/*
13195的質數因子有5,7,13和29.
600851475143的最大質數因子是多少?
*/
void fun(long long n)
{
long long i=2,ans=n;
while(i<n)
{
if(n%i==0)n=n/i;
else {i++;ans=n;}
}ans=n;
printf("%d\n",ans);
}
int main()
{
long long n;
while(scanf("%lld",&n)!=-1)
fun(n);
return 0;
}