欧拉计划
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;
int main ( )
{
long long ans= 2 , a= 1 , b= 2 , c= 0 ;
while ( b<= 4000000 )
{
c= b;
b= a+ b;
a= 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;
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 ;
}