1.If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.Find the sum of all the multiples of 3 or 5 below 100
(计算1000以内能被3或5整除的数之和)
#include <iostream>
using namespace std;
int main()
{
int i;
int sum = 0;
for (i = 1; i <= 1000; i++)
{
if (i % 3 == 0 || i % 5 == 0)
sum = sum + i;
}
cout << sum << endl;
return 0;
}
运行结果:
234168
2.Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...Find the sum of all the even-valued terms in the sequence which do not exceed one million.
(计算斐波那契数列中小于100万的偶数项之和)
#include <iostream>
using namespace std;
int main()
{
int a = 1, b = 2;
int sum = 0;
while(a < 1000000 && b < 1000000)
{
if (a % 2 == 0)
sum = sum + a;
if (b % 2 == 0)
sum = sum + b;
a = a + b;
b = b + a;
}
cout << sum << endl;
return 0;
}
运行结果:
257114
3.The prime factors of 13195 are 5, 7, 13 and 29.What is the largest prime factor of the number 317584931803?
(分解出317584931803的因数)
#include <iostream>
using namespace std;
int main()
{
int i;
long long n = 317584931803;
for (i = 2; i <= n; i++)
{
while (n != i)
{
if (n % i == 0)
{
n = n / i;
}
else
break;
}
} //从2开始将每个数作为n的除数,若整除则为因数后重新计算n再判断
cout << n << endl;//前面只输入前面几个较小因数,重新计算的最后一个n为最大因数
return 0;
}
运行结果:
3919
4.A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.Find the largest palindrome made from the product of two 3-digit numbers.
(找出两个三位数相乘的最大回文数)