Skip to content

Instantly share code, notes, and snippets.

@Taneb
Created July 22, 2014 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Taneb/92c2273815931be07d97 to your computer and use it in GitHub Desktop.
Save Taneb/92c2273815931be07d97 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <gmp.h>
void combine(mpz_t rop, const mpz_t op1, const mpz_t op2) {
mpz_t temp1;
mpz_init(temp1);
mpz_add(temp1, op1, op2);
mpz_add_ui(rop, temp1, 1);
mpz_mul(rop, rop, temp1);
mpz_clear(temp1);
mpz_fdiv_q_2exp(rop, rop, 1);
mpz_add(rop, rop, op2);
}
void split(mpz_t rop1, mpz_t rop2, const mpz_t op) {
mpz_t temp1;
mpz_init(temp1);
mpz_mul_ui(temp1, op, 8);
mpz_add_ui(temp1, temp1, 1);
mpz_sqrt(temp1, temp1);
mpz_sub_ui(temp1, temp1, 1);
mpz_fdiv_q_2exp(temp1, temp1, 1);
mpz_add_ui(rop1, temp1, 1);
mpz_mul(rop1, rop1, temp1);
mpz_fdiv_q_2exp(rop1, rop1, 1);
mpz_sub(rop2, op, rop1);
mpz_sub(rop1, temp1, rop2);
mpz_clear(temp1);
}
void step(mpz_t op) {
if (mpz_cmp_ui(op, 1) == 0)
return;
else if (mpz_cmp_ui(op, 2) == 0)
return;
else if (mpz_cmp_ui(op, 3) == 0)
return;
else if (mpz_cmp_ui(op, 5) == 0)
return;
mpz_t a, b, c, x, y, z;
mpz_inits(a, b, y, z, 0);
split(a, z, op);
split(b, y, a);
if (mpz_cmp_ui(b, 2) == 0) {
mpz_set(op, y);
mpz_clears(a, b, y, z, 0);
return;
}
mpz_inits(c, x, 0);
split(c, x, b);
if (mpz_cmp_ui(c,1) == 0) {
mpz_clear(c);
combine(a,x,z);
mpz_clear(x);
combine(b,y,z);
mpz_clears(y,z,0);
combine(op,a,b);
mpz_clears(a,b,0);
}
else {
mpz_clears(b,c,x,y,0);
step(a);
combine(op,a,z);
mpz_clears(a,z,0);
};
}
unsigned int run(mpz_t c) {
int e = 0;
mpz_t x,y;
mpz_inits(x,y,0);
while(1){
if(mpz_cmp_ui(c,5) == 0)
break;
split(x,y,c);
if(mpz_cmp_ui(x,3) == 0) {
e++;
mpz_set(c, y);
}
else
step(c);
}
mpz_clears(x,y,0);
return e;
}
int main() {
mpz_t c;
mpz_init_set_str(c, "197537424253964944058276662781718995981872066092661901379452935710992423278459230955300507815647000374430485844066558990542639456176162325793034701348961008200754230657532083774009604765", 10);
printf("%d\n", run(c));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment