Created
October 1, 2013 17:42
-
-
Save jnschulze/6782291 to your computer and use it in GitHub Desktop.
Collatz using libgmp.
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 <stdint.h> | |
#include <gmp.h> | |
int main() | |
{ | |
//const char* startValue = "45411764953495434350345765765756765750345456565445435436536546545324213324233"; | |
const char* startValue = "999349349859230394049534503485093452390984540385348304894503459235843908542390482950283945048902849230583496038920385934087"; | |
// bisection counter | |
uint64_t bisections = 0; | |
// multiplication counter | |
uint64_t multiplications = 0; | |
mpz_t x; | |
mpz_init_set_str(x, startValue, 10); | |
//while(x != 1) | |
while(mpz_cmp_ui(x, 1) != 0) | |
{ | |
//if(x % 2 == 0) | |
if(mpz_even_p(x)) | |
{ | |
//x = x/2; | |
mpz_cdiv_q_ui(x, x, 2); | |
bisections++; | |
} | |
else | |
{ | |
//x = x*3 + 1; | |
mpz_mul_ui(x, x, 3); | |
mpz_add_ui(x, x, 1); | |
multiplications++; | |
} | |
} | |
//printf("Finished. Start value was %lld, iterations: %lld, bisections: %lld, multiplications: %lld\n", y, bisections + multiplications, bisections, multiplications); | |
printf("Finished. Start value was %s, iterations: %lld, bisections: %lld, multiplications: %lld\n", startValue, bisections + multiplications, bisections, multiplications); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment