Last active
January 27, 2017 23:47
-
-
Save VincentTam/985de63d527e04301da01d2b10c0b228 to your computer and use it in GitHub Desktop.
Catalan no. & Pascal Triangle use GNU MP
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> | |
float myfct(int i) { | |
return i == 1? 1 : (366.0-i)/365.0*myfct(i - 1); | |
} | |
int main() { | |
int i; | |
double ans; | |
printf("Enter a value: "); | |
scanf("%d", &i); | |
ans = myfct(i); | |
printf("ans: %f", ans); | |
return 0; | |
} |
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 <gmp.h> | |
int main() { | |
const int N = 30; | |
const int SLEEP_TIME = 5; | |
mpz_t ans[N][N]; | |
for (int i = 0; i < N; i++) { | |
printf("%d: ", i+1); | |
for (int j = 0; j <= N; j++) { | |
mpz_init(ans[i][j]); | |
if (j == 0 || j == i+1) { | |
mpz_set_ui(ans[i][j], 1); | |
gmp_printf("%Zd ", ans[i][j]); | |
} else if (j > i+1) { | |
mpz_set_ui(ans[i][j], 0); | |
} else { | |
mpz_add(ans[i][j], ans[i-1][j-1], ans[i-1][j]); | |
gmp_printf("%Zd ", ans[i][j]); | |
sleep(SLEEP_TIME); | |
} | |
} | |
printf("\n"); | |
} | |
return 0; | |
} |
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 <gmp.h> | |
void catalan(mpz_t cn, int n); | |
int main() { | |
int n=150; | |
mpz_t cn; | |
mpz_init(cn); // init and set to 0 | |
printf("List Catalan no. up to: %d\n", n); | |
for (int i = 1; i <= n; i++) { | |
catalan(cn, i); | |
gmp_printf("%d: %Zd\n", i, cn); | |
mpz_set_ui(cn, 0); | |
} | |
return 0; | |
} | |
void catalan(mpz_t cn, int n) { | |
if (n == 0) { | |
mpz_set_ui(cn, 1); | |
return; | |
} | |
mpz_t reccur; | |
mpz_init(reccur); | |
catalan(reccur, n-1); | |
mpz_mul_ui(cn, reccur, 2*(2*n-1)); | |
mpz_tdiv_q_ui(cn, cn, n+1); | |
} |
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 <gmp.h> | |
void catalan(mpz_t cn, int n); | |
int main() { | |
int n=17; | |
mpz_t cn; | |
mpz_init(cn); // init and set to 0 | |
printf("List Catalan no. up to: %d\n", n); | |
for (int i = 1; i <= n; i++) { | |
catalan(cn, i); | |
gmp_printf("%d: %Zd\n", i, cn); | |
mpz_set_ui(cn, 0); | |
} | |
return 0; | |
} | |
void catalan(mpz_t cn, int n) { | |
if (n == 0) { | |
mpz_set_ui(cn, 1); | |
return; | |
} | |
else { | |
mpz_t akm1, anmk; // a_{k-1}, a_{n-k} | |
mpz_init(akm1); | |
mpz_init(anmk); | |
for (int k = 1; k <= n; k++) { | |
catalan(akm1, k-1); | |
catalan(anmk, n-k); | |
mpz_addmul(cn, akm1, anmk); | |
mpz_set_ui(akm1, 0); | |
mpz_set_ui(anmk, 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment