Skip to content

Commit 893224c

Browse files
committed
add problem3[0-9]
1 parent 810601a commit 893224c

10 files changed

+515
-0
lines changed

30-39/problem30.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2009, eagletmt
3+
* Released under the MIT License <http://opensource.org/licenses/mit-license.php>
4+
*/
5+
6+
#include <stdio.h>
7+
#include <math.h>
8+
9+
#define M 1000000
10+
#define E 5
11+
12+
int main(void)
13+
{
14+
char buf[16];
15+
int i;
16+
int ttl = 0;
17+
for (i = 10; i < M; i++) {
18+
int sum = 0, j;
19+
20+
snprintf(buf, sizeof buf, "%d", i);
21+
for (j = 0; buf[j]; j++) {
22+
sum += pow(buf[j]-'0', E);
23+
}
24+
if (i == sum) {
25+
ttl += i;
26+
}
27+
}
28+
printf("%d\n", ttl);
29+
return 0;
30+
}
31+

30-39/problem31.c

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2009, eagletmt
3+
* Released under the MIT License <http://opensource.org/licenses/mit-license.php>
4+
*/
5+
6+
#include <stdio.h>
7+
8+
int main(void)
9+
{
10+
int z = 0;
11+
int n = 200;
12+
13+
int a, b, c, d, e, f, g;
14+
for (a = 0; a <= n; a += 200)
15+
for (b = 0; b <= n; b += 100)
16+
for (c = 0; c <= n; c += 50)
17+
for (d = 0; d <= n; d += 20)
18+
for (e = 0; e <= n; e += 10)
19+
for (f = 0; f <= n; f += 5)
20+
for (g = 0; g <= n; g += 2)
21+
if (a+b+c+d+e+f+g <= 200)
22+
z++;
23+
printf("%d\n", z);
24+
25+
return 0;
26+
}
27+

30-39/problem32.c

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2009, eagletmt
3+
* Released under the MIT License <http://opensource.org/licenses/mit-license.php>
4+
*/
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <math.h>
9+
#include <string.h>
10+
11+
static int compare(const void *a, const void *b);
12+
static int is_pandigital(char *buf);
13+
#define DIGITS(x) (lround(floor(log10((double)x))))
14+
15+
int main(void)
16+
{
17+
int product, multiplier, multiplicand, remaindar;
18+
int sum = 0;
19+
char buf[10];
20+
21+
for (product = 1111; product < 100000; product++) {
22+
for (multiplicand = 2; multiplicand*multiplicand < product; multiplicand++) {
23+
remaindar = product % multiplicand;
24+
if (remaindar != 0) {
25+
continue;
26+
}
27+
multiplier = product / multiplicand;
28+
if (DIGITS(multiplicand) + DIGITS(multiplier) + DIGITS(product) != 6) {
29+
continue;
30+
}
31+
32+
snprintf(buf, sizeof buf, "%d%d%d", multiplicand, multiplier, product);
33+
if (is_pandigital(buf)) {
34+
sum += product;
35+
break;
36+
}
37+
}
38+
}
39+
printf("%d\n", sum);
40+
41+
return 0;
42+
}
43+
44+
static int compare(const void *a, const void *b)
45+
{
46+
return *(const char *)a - *(const char *)b;
47+
}
48+
49+
static int is_pandigital(char *buf)
50+
{
51+
qsort(buf, 9, 1, compare);
52+
return memcmp(buf, "123456789", 9) == 0;
53+
}
54+

30-39/problem33.c

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2009, eagletmt
3+
* Released under the MIT License <http://opensource.org/licenses/mit-license.php>
4+
*/
5+
6+
#include <stdio.h>
7+
#include <math.h>
8+
9+
static unsigned gcd(unsigned a, unsigned b);
10+
static int cancel(unsigned n1, unsigned d1, unsigned *n2, unsigned *d2);
11+
12+
int main(void)
13+
{
14+
unsigned ans_n = 1, ans_d = 1;
15+
unsigned i;
16+
for (i = 11; i < 100; i++) {
17+
unsigned j;
18+
19+
if (i % 10 == 0) {
20+
continue;
21+
}
22+
23+
for (j = 11; j < i; j++) {
24+
unsigned g, n, d;
25+
unsigned n2, d2;
26+
27+
if (j % 10 == 0) {
28+
continue;
29+
}
30+
31+
if ((g = gcd(i, j)) == 1) {
32+
continue;
33+
}
34+
/* cancel numerically */
35+
n = j/g;
36+
d = i/g;
37+
38+
/* cancel symbolically */
39+
if (cancel(j, i, &n2, &d2)) {
40+
unsigned g2 = gcd(n2, d2);
41+
n2 /= g2; d2 /= g2;
42+
if (n == n2 && d == d2) {
43+
ans_n *= n;
44+
ans_d *= d;
45+
}
46+
}
47+
}
48+
}
49+
printf("%u\n", ans_d / gcd(ans_n, ans_d));
50+
return 0;
51+
}
52+
53+
unsigned gcd(unsigned a, unsigned b)
54+
{
55+
unsigned r;
56+
while ((r = a % b) != 0) {
57+
a = b; b = r;
58+
}
59+
return b;
60+
}
61+
62+
int cancel(unsigned n1, unsigned d1, unsigned *n2, unsigned *d2)
63+
{
64+
if (n1%10 == d1%10) {
65+
/* e.g. 24 and 34 */
66+
*n2 = n1/10;
67+
*d2 = d1/10;
68+
return 1;
69+
}
70+
else if (n1%10 == d1/10) {
71+
/* e.g. 24 and 41 */
72+
*n2 = n1/10;
73+
*d2 = d1%10;
74+
return 1;
75+
}
76+
else if (n1/10 == d1%10) {
77+
/* e.g. 24 and 32 */
78+
*n2 = n1%10;
79+
*d2 = d1/10;
80+
return 1;
81+
}
82+
else if (n1/10 == d1/10) {
83+
/* e.g. 24 and 21 */
84+
*n2 = n1%10;
85+
*d2 = d1%10;
86+
return 1;
87+
}
88+
else {
89+
*n2 = n1;
90+
*d2 = d1;
91+
return 0;
92+
}
93+
}
94+

30-39/problem34.c

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2009, eagletmt
3+
* Released under the MIT License <http://opensource.org/licenses/mit-license.php>
4+
*/
5+
6+
#include <stdio.h>
7+
8+
unsigned fact(unsigned n);
9+
10+
int main(void)
11+
{
12+
unsigned i, sum = 0;
13+
char buf[16];
14+
15+
for (i = 3; i < 100000; i++) {
16+
unsigned j, s = 0;
17+
18+
snprintf(buf, sizeof buf, "%u", i);
19+
for (j = 0; buf[j] != 0; j++) {
20+
s += fact(buf[j]-'0');
21+
}
22+
if (i == s) {
23+
sum += s;
24+
}
25+
}
26+
printf("%u\n", sum);
27+
28+
return 0;
29+
}
30+
31+
unsigned fact(unsigned n)
32+
{
33+
unsigned f = 1;
34+
while (n != 0) {
35+
f *= n--;
36+
}
37+
return f;
38+
}
39+

30-39/problem35.c

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2009, eagletmt
3+
* Released under the MIT License <http://opensource.org/licenses/mit-license.php>
4+
*/
5+
6+
#include <stdio.h>
7+
#include <math.h>
8+
9+
#define N 1000000
10+
11+
static int is_prime(unsigned n);
12+
13+
int main(void)
14+
{
15+
unsigned i, count = 0;
16+
17+
for (i = 2; i < N; i++) {
18+
unsigned digits;
19+
unsigned k;
20+
unsigned j;
21+
22+
if (!is_prime(i)) {
23+
continue;
24+
}
25+
digits = log10(i);
26+
k = i;
27+
for (j = 0; j < digits; j++) {
28+
unsigned l = pow(10, digits);
29+
unsigned t = k % l;
30+
k /= l;
31+
k += t*10;
32+
if (!is_prime(k)) {
33+
goto NEXT;
34+
}
35+
}
36+
count++;
37+
NEXT:
38+
;
39+
}
40+
printf("%u\n", count);
41+
42+
return 0;
43+
}
44+
45+
int is_prime(unsigned n)
46+
{
47+
if (n <= 1) {
48+
return 0;
49+
}
50+
else if (n == 2) {
51+
return 1;
52+
}
53+
else {
54+
unsigned i;
55+
for (i = 2; i*i <= n; i++) {
56+
if (n % i == 0) {
57+
return 0;
58+
}
59+
}
60+
return 1;
61+
}
62+
}
63+

30-39/problem36.c

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2009, eagletmt
3+
* Released under the MIT License <http://opensource.org/licenses/mit-license.php>
4+
*/
5+
6+
#include <stdio.h>
7+
8+
#define N 1000000
9+
10+
static int palindromic_base(int n, int base);
11+
12+
int main(void)
13+
{
14+
int i;
15+
int sum = 0;
16+
17+
for (i = 1; i < N; i++) {
18+
if (palindromic_base(i, 10) && palindromic_base(i, 2)) {
19+
sum += i;
20+
}
21+
}
22+
printf("%d\n", sum);
23+
24+
return 0;
25+
}
26+
27+
int palindromic_base(int n, int base)
28+
{
29+
int reversed = 0;
30+
const int orig = n;
31+
32+
while (n) {
33+
reversed = base*reversed + n%base;
34+
n /= base;
35+
}
36+
return orig == reversed;
37+
}
38+

0 commit comments

Comments
 (0)