Question: C Programming Help (Collatz Conjecture) Hey so explaining everthing: I have a sequence following the simple rule: - If the current term is even: the
C Programming Help (Collatz Conjecture) Hey so explaining everthing: I have a sequence following the simple rule:
- If the current term is even: the next term is half the current term.
- If the current term is odd: the next term is three times the current term, plus 1.
we continue this until we reach one and count each term used.
For example: For he number 17, we have:
17 52 26 13 40 20 10 5 16 8 4 2 1
Hence, the count is 13.
What I specifically need assistance with is that I need to find the smallest starting value (number) which has a count over 1234.
ie:
- 1 has count 1
- 2 has count 2
- 3 has count 8
- 6 has count 9
- 7 has count 17
- 9 has count 20
- 18 has count 21
- 25 has count 24
- 27 has count 112
- 54 has count 113
- 73 has count 116
- 97 has count 119
This is the code I have which I think works but takes extremely long to process. How can I do this much more quickly?
#include
int main (void) {
unsigned long long int num1 = 1; while (num1 <= 99999999999999999) { unsigned long long int num = num1; int i = 0; int count = 1; while (num != 1) { if (num % 2 == 0) { num = (num/2); // printf("%d ", num); count++; } else { num = ((num*3) + 1); // printf("%d ", num); count++; } i++; } if (count > 1234) { printf("%llu ", num1); break; }
num1++; } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
