c_cpp 使用libgmp进行Collatz。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 使用libgmp进行Collatz。相关的知识,希望对你有一定的参考价值。
#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);
}
以上是关于c_cpp 使用libgmp进行Collatz。的主要内容,如果未能解决你的问题,请参考以下文章
Erlang中的Collatz序列
Collatz C++ 代码的问题
python Collatz序列
最大化 Collatz 猜想程序 Python 的效率
python 最长的Collatz序列(Euler#14)
Collatz Conjecture Python - 超过 2 万亿的错误输出(仅限!)