1
0
Fork 0

p20 and a minor change in p11

This commit is contained in:
Henrik Hautakoski 2010-05-17 08:48:30 +02:00
parent fe45c3ab43
commit 64b6d0ae49
2 changed files with 55 additions and 9 deletions

18
p11.c
View file

@ -18,15 +18,15 @@
#define prd(x, y) (grid[y][x] * grid[y-1][x+1] * grid[y-2][x+2] * grid[y-3][x+3])
/* macro for checking all directions of a square x,y */
#define check_square(x, y) \
do { \
if (x < 17) \
setprod(px(x, y)); \
if (y < 17) \
setprod(py(x, y)); \
if (x < 17 && y < 17) \
setprod(pd(x, y)); \
if (x >= 3 && y >= 3) \
#define check_square(x, y) \
do { \
if (x < 17) \
setprod(px(x, y)); \
if (y < 17) \
setprod(py(x, y)); \
if (x < 17 && y < 17) \
setprod(pd(x, y)); \
if (x >= 3 && y >= 3) \
setprod(prd(x, y)); \
} while(0)

46
p20.c Normal file
View file

@ -0,0 +1,46 @@
/*
* http://projecteuler.net
*
* Projecteuler - Problem 20
* ------------------------
* 2010-05-17 Henrik Hautakoski
*
* easy with a good lib for doing arbitrary-precision arithmetic
*/
#include <stdio.h>
#include <malloc.h>
#include <gmp.h>
char* fak(int n) {
mpz_t v;
mpz_init(v);
mpz_fac_ui(v, n);
return mpz_get_str(NULL, 10, v);
}
int sum(char *n) {
int num = 0;
for(; *n != '\0'; n++)
num += (*n) - 0x30;
return num;
}
int main() {
char *n = fak(100);
if (n == NULL)
return 1;
printf("%i\n", sum(n));
free(n);
return 0;
}