Skip to content

Commit

Permalink
add problem2[0-9]
Browse files Browse the repository at this point in the history
  • Loading branch information
eagletmt committed Jun 2, 2009
1 parent 7c51ce9 commit 810601a
Show file tree
Hide file tree
Showing 11 changed files with 433 additions and 0 deletions.
1 change: 1 addition & 0 deletions 20-29/names.txt

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions 20-29/problem20.c
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;
}

40 changes: 40 additions & 0 deletions 20-29/problem21.c
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;
}

59 changes: 59 additions & 0 deletions 20-29/problem22.c
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);
}

71 changes: 71 additions & 0 deletions 20-29/problem23.c
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;
}

28 changes: 28 additions & 0 deletions 20-29/problem24.cc
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;
}

36 changes: 36 additions & 0 deletions 20-29/problem25.c
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;
}

43 changes: 43 additions & 0 deletions 20-29/problem26.c
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;
}

49 changes: 49 additions & 0 deletions 20-29/problem27.c
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;
}

19 changes: 19 additions & 0 deletions 20-29/problem28.c
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;
}

Loading

0 comments on commit 810601a

Please sign in to comment.