# 工学1号馆

home

## 欧拉计划1-5

By Wu Yudong on January 14, 2017

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 1000.

10以下的自然数中，属于3和5的倍数的有3,5,6和9，它们之和是23.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void solve()
{
int sum, i;
sum = 0;
for (i = 3; i < 1000; i++) {
if (i % 3 == 0 || i % 5 == 0) {
sum += i;
}
}
printf("%d\n", sum);

}

int main()
{
solve();
return 0;
}

Completed on Sun, 31 Mar 2013, 14:35

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, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#define N 4000000

int a[1001];

void solve()
{
int a, b, c, n, count = 2;
a = 1, c = 0, b = 2;
n = 3;
while (c <= N) {
c = a + b;
if (n % 2 != 0) {
a = c;
} else {
b = c;
}
n++;
if (c % 2 == 0) {
count += c;
}
}
printf("%d", count);
}

int main()
{
solve();
return 0;
}

Completed on Tue, 2 Apr 2013, 06:36

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

13195的质数因子有5,7,13和29.

600851475143的最大质数因子是多少？

#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#define N 600851475143

bool prim(int n)
{
int i;
for (i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}

int main()
{
long long s = sqrt(N);
while (s--) {
if (s % 2 != 0 && prim(s) && (N % s == 0)) {

printf("%lld\n", s);
break;
}
}
return 0;
}

Completed on Tue, 23 Jul 2013, 17:18

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.

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<stdbool.h>

bool palindromic(int n)			//判断一个整数是否为回文数
{
char s[10];
sprintf(s, "%d", n);		//将整数n保存在字符数组s中
int i, len;
len = strlen(s);
for (i = 0; i < len / 2; i++) {
if (s[i] != s[len - i - 1])
return false;
}
return true;
}

bool have_the_factor(int n)		//判断是否含有两个3位数的因数
{
int s = 999;
int r, b;
while (s > 100) {
if ((n % s) == 0 && ((n / s) > 100 && (n / s) < 1000))
return true;
s--;
}
return false;
}

int main()
{
int i = 1000000;
while (i > 0) {
if (palindromic(i) && have_the_factor(i)) {
printf("%d\n", i);
break;
}
i--;
}
return 0;
}

Completed on Wed, 24 Jul 2013, 04:34

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

2520是最小的能被1-10中每个数字整除的正整数。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#define N 20

int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}

int lcm(int a, int b)
{
return a / (gcd(a, b)) * b;
}

void solve()
{
int i, s = 2;
for (i = 3; i <= N; i++) {
s = lcm(s, i);
}
printf("%d\n", s);
}

int main()
{
solve();
return 0;
}