我正在编写一个程序,计算数字范围(1 - 100,000,000)之间Collatz 猜想的最多循环,我需要它在 4 分钟内在 Linux 系统上运行
command gcc -O0 -m32 -Wall -Wextra -Werror -pedantic -o collatz collatz.c.
Run Code Online (Sandbox Code Playgroud)
关于如何改进算法的任何想法,因为在 10,000,000 次之后,程序需要很长时间才能完成,请记住,我不能使用多线程,也不能使用预先计算的数据。
欢迎所有答案,谢谢您的宝贵时间。
// Lets find the longest collatz
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
// This function finds how many times it took for a number to reach 1
unsigned long int collatz(uint32_t First_num) {
// steps measures how many times a number needs to reach 1
uint32_t steps = 1;
// This is the loop that applies …Run Code Online (Sandbox Code Playgroud)