-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
433 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2009, eagletmt | ||
* Released under the MIT License <http://opensource.org/licenses/mit-license.php> | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <gmp.h> | ||
|
||
int main(void) | ||
{ | ||
mpz_t n; | ||
char *str; | ||
int i; | ||
int sum = 0; | ||
|
||
mpz_init(n); | ||
mpz_fac_ui(n, 100); | ||
str = mpz_get_str(NULL, 10, n); | ||
for (i = 0; str[i]; i++) { | ||
sum += str[i]-'0'; | ||
} | ||
printf("%d\n", sum); | ||
|
||
free(str); | ||
mpz_clear(n); | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2009, eagletmt | ||
* Released under the MIT License <http://opensource.org/licenses/mit-license.php> | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
static int divisors_sum(int n); | ||
|
||
int main(void) | ||
{ | ||
int i, d, sum = 0; | ||
|
||
for (i = 2; i < 10000; i++) { | ||
d = divisors_sum(i); | ||
if (i < d && i == divisors_sum(d)) { | ||
sum += i + d; | ||
} | ||
} | ||
printf("%d\n", sum); | ||
|
||
return 0; | ||
} | ||
|
||
int divisors_sum(int n) | ||
{ | ||
int sum= 1; | ||
int i, k = n; | ||
|
||
for (i = 2; i <= k; i++) { | ||
int p = 1; | ||
while (k % i == 0) { | ||
p *= i; | ||
k /= i; | ||
} | ||
sum *= (p*i - 1)/(i-1); | ||
} | ||
return sum - n; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) 2009, eagletmt | ||
* Released under the MIT License <http://opensource.org/licenses/mit-license.php> | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <assert.h> | ||
|
||
static int compare(const void *x, const void *y); | ||
|
||
int main(void) | ||
{ | ||
int count = 0; | ||
FILE *fp; | ||
char path[] = "names.txt"; | ||
char **names; | ||
int i; | ||
int sum = 0; | ||
|
||
fp = fopen(path, "r"); | ||
if (!fp) { | ||
perror(path); | ||
return 1; | ||
} | ||
|
||
/* parse names.txt */ | ||
names = malloc(8192 * sizeof *names); | ||
names[count] = malloc(16); | ||
while (fscanf(fp, "\"%[^\"]\",", names[count]) != EOF) { | ||
names[++count] = malloc(16); | ||
} | ||
free(names[count]); | ||
fclose(fp); | ||
|
||
qsort(names, count, sizeof *names, compare); | ||
|
||
for (i = 0; i < count; i++) { | ||
int j, s = 0; | ||
for (j = 0; names[i][j] != 0; j++) { | ||
s += names[i][j]-'A'+1; | ||
} | ||
sum += s * (i+1); | ||
|
||
free(names[i]); | ||
} | ||
free(names); | ||
|
||
printf("%d\n", sum); | ||
|
||
return 0; | ||
} | ||
|
||
int compare(const void *x, const void *y) | ||
{ | ||
return strcmp(*(char * const *)x, *(char * const *)y); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (c) 2009, eagletmt | ||
* Released under the MIT License <http://opensource.org/licenses/mit-license.php> | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
static int is_abundant(int n); | ||
|
||
int main(void) | ||
{ | ||
int i, j; | ||
const int upper_limit = 28123; | ||
int *abundants, abundants_count = 0; | ||
char *expressed; | ||
int sum = 0; | ||
|
||
for (i = 1; i < upper_limit; i++) { | ||
if (is_abundant(i)) { | ||
abundants_count++; | ||
} | ||
} | ||
|
||
abundants = malloc(abundants_count * sizeof *abundants); | ||
j = 0; | ||
for (i = 1; i < upper_limit; i++) { | ||
if (is_abundant(i)) { | ||
abundants[j++] = i; | ||
} | ||
} | ||
|
||
expressed = calloc(upper_limit, sizeof *expressed); | ||
for (i = 0; i < abundants_count; i++) { | ||
for (j = i; j < abundants_count; j++) { | ||
int k = abundants[i] + abundants[j]; | ||
if (k >= upper_limit) { | ||
break; | ||
} | ||
expressed[k-1] = 1; | ||
} | ||
} | ||
|
||
for (i = 1; i < upper_limit; i++) { | ||
if (!expressed[i-1]) { | ||
sum += i; | ||
} | ||
} | ||
printf("%d\n", sum); | ||
|
||
free(abundants); | ||
free(expressed); | ||
|
||
return 0; | ||
} | ||
|
||
int is_abundant(int n) | ||
{ | ||
int sum = 1; | ||
int i, k = n; | ||
for (i = 2; i <= k; i++) { | ||
int p = 1; | ||
while (k % i == 0) { | ||
p *= i; | ||
k /= i; | ||
} | ||
sum *= (p*i - 1)/(i-1); | ||
} | ||
return 2*n < sum; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2009, eagletmt | ||
* Released under the MIT License <http://opensource.org/licenses/mit-license.php> | ||
*/ | ||
|
||
#include <iostream> | ||
#include <algorithm> | ||
|
||
#define N 1000000 | ||
|
||
int main(void) | ||
{ | ||
using namespace std; | ||
|
||
int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | ||
int i; | ||
|
||
for (i = 0; i <= N; i++) { | ||
next_permutation(a, a + sizeof a/sizeof *a); | ||
} | ||
for (i = 0; i < 10; i++) { | ||
cout << a[i]; | ||
} | ||
cout << endl; | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2009, eagletmt | ||
* Released under the MIT License <http://opensource.org/licenses/mit-license.php> | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <gmp.h> | ||
|
||
int main(void) | ||
{ | ||
mpz_t n; | ||
int i = 1; | ||
|
||
mpz_init(n); | ||
|
||
for (i = 1;; i++) { | ||
char *str; | ||
size_t len; | ||
|
||
mpz_fib_ui(n, i); | ||
str = mpz_get_str(NULL, 10, n); | ||
len = strlen(str); | ||
free(str); | ||
if (len == 1000) { | ||
printf("%d\n", i); | ||
break; | ||
} | ||
} | ||
|
||
mpz_clear(n); | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2009, eagletmt | ||
* Released under the MIT License <http://opensource.org/licenses/mit-license.php> | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <gmp.h> | ||
|
||
#define M 1000 | ||
|
||
int main(void) | ||
{ | ||
mpz_t p, r; | ||
unsigned maxlen = 0, imax = 0; | ||
unsigned i; | ||
|
||
mpz_init(p); mpz_init(r); | ||
|
||
for (i = 2; i < M; i++) { | ||
unsigned len = 1; | ||
|
||
if (i % 2 == 0 || i % 5 == 0) { | ||
continue; | ||
} | ||
|
||
mpz_set_ui(p, 10); | ||
while (mpz_mod_ui(r, p, i), mpz_cmp_ui(r, 1) != 0) { | ||
len++; | ||
mpz_mul_ui(p, p, 10); | ||
} | ||
if (len > maxlen) { | ||
maxlen = len; | ||
imax = i; | ||
} | ||
} | ||
printf("%u\n", imax); | ||
|
||
mpz_clear(p); | ||
mpz_clear(r); | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2009, eagletmt | ||
* Released under the MIT License <http://opensource.org/licenses/mit-license.php> | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <math.h> | ||
|
||
static int is_prime(int n); | ||
|
||
int main(void) | ||
{ | ||
int a, b, n; | ||
int nmax = 0, amax = 0, bmax = 0; | ||
|
||
for (a = -999; a < 1000; a++) { | ||
for (b = -999; b < 1000; b++) { | ||
for (n = 0; is_prime(n*n + a*n + b); n++); | ||
if (n > nmax) { | ||
nmax = n; | ||
amax = a; | ||
bmax = b; | ||
} | ||
} | ||
} | ||
printf("%d\n", amax * bmax); | ||
|
||
return 0; | ||
} | ||
|
||
int is_prime(int n) | ||
{ | ||
int i; | ||
|
||
if (n <= 1) { | ||
return 0; | ||
} | ||
if (n == 2) { | ||
return 1; | ||
} | ||
|
||
for (i = 2; i < sqrt((double)n); i++) { | ||
if (n % i == 0) { | ||
return 0; | ||
} | ||
} | ||
return 1; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright (c) 2009, eagletmt | ||
* Released under the MIT License <http://opensource.org/licenses/mit-license.php> | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
#define N 1001 | ||
|
||
int main(void) | ||
{ | ||
int sum = 1, i; | ||
for (i = 3; i <= N; i += 2) { | ||
sum += 2*(2*i*i - 3*i + 3); | ||
} | ||
printf("%d\n", sum); | ||
return 0; | ||
} | ||
|
Oops, something went wrong.