Skip to content

Commit 810601a

Browse files
committed
add problem2[0-9]
1 parent 7c51ce9 commit 810601a

11 files changed

+433
-0
lines changed

20-29/names.txt

+1
Large diffs are not rendered by default.

20-29/problem20.c

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 <gmp.h>
9+
10+
int main(void)
11+
{
12+
mpz_t n;
13+
char *str;
14+
int i;
15+
int sum = 0;
16+
17+
mpz_init(n);
18+
mpz_fac_ui(n, 100);
19+
str = mpz_get_str(NULL, 10, n);
20+
for (i = 0; str[i]; i++) {
21+
sum += str[i]-'0';
22+
}
23+
printf("%d\n", sum);
24+
25+
free(str);
26+
mpz_clear(n);
27+
28+
return 0;
29+
}
30+

20-29/problem21.c

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
static int divisors_sum(int n);
9+
10+
int main(void)
11+
{
12+
int i, d, sum = 0;
13+
14+
for (i = 2; i < 10000; i++) {
15+
d = divisors_sum(i);
16+
if (i < d && i == divisors_sum(d)) {
17+
sum += i + d;
18+
}
19+
}
20+
printf("%d\n", sum);
21+
22+
return 0;
23+
}
24+
25+
int divisors_sum(int n)
26+
{
27+
int sum= 1;
28+
int i, k = n;
29+
30+
for (i = 2; i <= k; i++) {
31+
int p = 1;
32+
while (k % i == 0) {
33+
p *= i;
34+
k /= i;
35+
}
36+
sum *= (p*i - 1)/(i-1);
37+
}
38+
return sum - n;
39+
}
40+

20-29/problem22.c

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 <string.h>
9+
#include <assert.h>
10+
11+
static int compare(const void *x, const void *y);
12+
13+
int main(void)
14+
{
15+
int count = 0;
16+
FILE *fp;
17+
char path[] = "names.txt";
18+
char **names;
19+
int i;
20+
int sum = 0;
21+
22+
fp = fopen(path, "r");
23+
if (!fp) {
24+
perror(path);
25+
return 1;
26+
}
27+
28+
/* parse names.txt */
29+
names = malloc(8192 * sizeof *names);
30+
names[count] = malloc(16);
31+
while (fscanf(fp, "\"%[^\"]\",", names[count]) != EOF) {
32+
names[++count] = malloc(16);
33+
}
34+
free(names[count]);
35+
fclose(fp);
36+
37+
qsort(names, count, sizeof *names, compare);
38+
39+
for (i = 0; i < count; i++) {
40+
int j, s = 0;
41+
for (j = 0; names[i][j] != 0; j++) {
42+
s += names[i][j]-'A'+1;
43+
}
44+
sum += s * (i+1);
45+
46+
free(names[i]);
47+
}
48+
free(names);
49+
50+
printf("%d\n", sum);
51+
52+
return 0;
53+
}
54+
55+
int compare(const void *x, const void *y)
56+
{
57+
return strcmp(*(char * const *)x, *(char * const *)y);
58+
}
59+

20-29/problem23.c

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
9+
static int is_abundant(int n);
10+
11+
int main(void)
12+
{
13+
int i, j;
14+
const int upper_limit = 28123;
15+
int *abundants, abundants_count = 0;
16+
char *expressed;
17+
int sum = 0;
18+
19+
for (i = 1; i < upper_limit; i++) {
20+
if (is_abundant(i)) {
21+
abundants_count++;
22+
}
23+
}
24+
25+
abundants = malloc(abundants_count * sizeof *abundants);
26+
j = 0;
27+
for (i = 1; i < upper_limit; i++) {
28+
if (is_abundant(i)) {
29+
abundants[j++] = i;
30+
}
31+
}
32+
33+
expressed = calloc(upper_limit, sizeof *expressed);
34+
for (i = 0; i < abundants_count; i++) {
35+
for (j = i; j < abundants_count; j++) {
36+
int k = abundants[i] + abundants[j];
37+
if (k >= upper_limit) {
38+
break;
39+
}
40+
expressed[k-1] = 1;
41+
}
42+
}
43+
44+
for (i = 1; i < upper_limit; i++) {
45+
if (!expressed[i-1]) {
46+
sum += i;
47+
}
48+
}
49+
printf("%d\n", sum);
50+
51+
free(abundants);
52+
free(expressed);
53+
54+
return 0;
55+
}
56+
57+
int is_abundant(int n)
58+
{
59+
int sum = 1;
60+
int i, k = n;
61+
for (i = 2; i <= k; i++) {
62+
int p = 1;
63+
while (k % i == 0) {
64+
p *= i;
65+
k /= i;
66+
}
67+
sum *= (p*i - 1)/(i-1);
68+
}
69+
return 2*n < sum;
70+
}
71+

20-29/problem24.cc

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2009, eagletmt
3+
* Released under the MIT License <http://opensource.org/licenses/mit-license.php>
4+
*/
5+
6+
#include <iostream>
7+
#include <algorithm>
8+
9+
#define N 1000000
10+
11+
int main(void)
12+
{
13+
using namespace std;
14+
15+
int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
16+
int i;
17+
18+
for (i = 0; i <= N; i++) {
19+
next_permutation(a, a + sizeof a/sizeof *a);
20+
}
21+
for (i = 0; i < 10; i++) {
22+
cout << a[i];
23+
}
24+
cout << endl;
25+
26+
return 0;
27+
}
28+

20-29/problem25.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 <string.h>
9+
#include <gmp.h>
10+
11+
int main(void)
12+
{
13+
mpz_t n;
14+
int i = 1;
15+
16+
mpz_init(n);
17+
18+
for (i = 1;; i++) {
19+
char *str;
20+
size_t len;
21+
22+
mpz_fib_ui(n, i);
23+
str = mpz_get_str(NULL, 10, n);
24+
len = strlen(str);
25+
free(str);
26+
if (len == 1000) {
27+
printf("%d\n", i);
28+
break;
29+
}
30+
}
31+
32+
mpz_clear(n);
33+
34+
return 0;
35+
}
36+

20-29/problem26.c

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 <gmp.h>
8+
9+
#define M 1000
10+
11+
int main(void)
12+
{
13+
mpz_t p, r;
14+
unsigned maxlen = 0, imax = 0;
15+
unsigned i;
16+
17+
mpz_init(p); mpz_init(r);
18+
19+
for (i = 2; i < M; i++) {
20+
unsigned len = 1;
21+
22+
if (i % 2 == 0 || i % 5 == 0) {
23+
continue;
24+
}
25+
26+
mpz_set_ui(p, 10);
27+
while (mpz_mod_ui(r, p, i), mpz_cmp_ui(r, 1) != 0) {
28+
len++;
29+
mpz_mul_ui(p, p, 10);
30+
}
31+
if (len > maxlen) {
32+
maxlen = len;
33+
imax = i;
34+
}
35+
}
36+
printf("%u\n", imax);
37+
38+
mpz_clear(p);
39+
mpz_clear(r);
40+
41+
return 0;
42+
}
43+

20-29/problem27.c

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 int is_prime(int n);
10+
11+
int main(void)
12+
{
13+
int a, b, n;
14+
int nmax = 0, amax = 0, bmax = 0;
15+
16+
for (a = -999; a < 1000; a++) {
17+
for (b = -999; b < 1000; b++) {
18+
for (n = 0; is_prime(n*n + a*n + b); n++);
19+
if (n > nmax) {
20+
nmax = n;
21+
amax = a;
22+
bmax = b;
23+
}
24+
}
25+
}
26+
printf("%d\n", amax * bmax);
27+
28+
return 0;
29+
}
30+
31+
int is_prime(int n)
32+
{
33+
int i;
34+
35+
if (n <= 1) {
36+
return 0;
37+
}
38+
if (n == 2) {
39+
return 1;
40+
}
41+
42+
for (i = 2; i < sqrt((double)n); i++) {
43+
if (n % i == 0) {
44+
return 0;
45+
}
46+
}
47+
return 1;
48+
}
49+

20-29/problem28.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 1001
9+
10+
int main(void)
11+
{
12+
int sum = 1, i;
13+
for (i = 3; i <= N; i += 2) {
14+
sum += 2*(2*i*i - 3*i + 3);
15+
}
16+
printf("%d\n", sum);
17+
return 0;
18+
}
19+

0 commit comments

Comments
 (0)