Last active
December 26, 2015 02:29
-
-
Save 46bit/7078676 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <strings.h> | |
#include <string.h> | |
#include <inttypes.h> | |
#include <math.h> | |
#include <gmp.h> | |
// http://projecteuler.net/problem=15 | |
// | |
// Starting in the top left corner of a 2×2 grid, and only being able to | |
// move to the right and down, there are exactly 6 routes to the bottom | |
// right corner. | |
// | |
// How many such routes are there through a 20×20 grid? | |
void lattice_paths (mpz_ptr v, int64_t size); | |
void fibonacci_coefficient (mpz_ptr v, int64_t n, int64_t r); | |
void factorial (mpz_ptr v, int64_t n); | |
int main () { | |
mpz_t paths; | |
mpz_init(paths); | |
for (int64_t size = 0; size < 21; size++) { | |
lattice_paths(paths, size); | |
printf("lattice_paths(%" PRId64 ") = ", size); | |
mpz_out_str(stdout, 10, paths); | |
printf("\n"); | |
} | |
return 0; | |
} | |
void lattice_paths (mpz_ptr v, int64_t size) { | |
fibonacci_coefficient(v, 2 * size, size); | |
} | |
void fibonacci_coefficient (mpz_ptr v, int64_t n, int64_t r) { | |
mpz_t a, b, c; | |
mpz_init(a); | |
mpz_init(b); | |
mpz_init(c); | |
factorial(a, n); | |
factorial(b, r); | |
factorial(c, n - r); | |
mpz_mul(b, b, c); | |
mpz_div(a, a, b); | |
mpz_set(v, a); | |
} | |
void factorial (mpz_ptr v, int64_t n) { | |
mpz_set_si(v, 1); | |
for (int64_t i = n; i > 1; i--) { | |
mpz_mul_si(v, v, i); | |
} | |
} |
This file contains hidden or 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
fortysix@46mbp:euler ∴ g++ -o p15 p15.c | |
Undefined symbols for architecture x86_64: | |
"___gmpz_fdiv_q", referenced from: | |
fibonacci_coefficient(__mpz_struct*, long long, long long)in ccnAfSXb.o | |
"___gmpz_init", referenced from: | |
_main in ccnAfSXb.o | |
fibonacci_coefficient(__mpz_struct*, long long, long long)in ccnAfSXb.o | |
"___gmpz_mul", referenced from: | |
fibonacci_coefficient(__mpz_struct*, long long, long long)in ccnAfSXb.o | |
"___gmpz_mul_si", referenced from: | |
factorial(__mpz_struct*, long long)in ccnAfSXb.o | |
"___gmpz_out_str", referenced from: | |
_main in ccnAfSXb.o | |
"___gmpz_set", referenced from: | |
fibonacci_coefficient(__mpz_struct*, long long, long long)in ccnAfSXb.o | |
"___gmpz_set_si", referenced from: | |
factorial(__mpz_struct*, long long)in ccnAfSXb.o | |
ld: symbol(s) not found for architecture x86_64 | |
collect2: ld returned 1 exit status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment