forked from till-tomorrow/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collatzConjecture.c
35 lines (30 loc) · 887 Bytes
/
collatzConjecture.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
int collatz(int n);
int main()
{
puts("The Collatz conjecture is a conjecture in mathematics.\nTake any positive integer n. If n is even, divide it by 2 to get n / 2.\nIf n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process indefinitly.\nThe conjecture is that you will always reach at the end the following numbers \"4 2 1\".");
puts("Now please enter the n number (integer > 0) you wish and this program will give you the number of steps to reach the conjecture's final numbers");
int n;
scanf("%d", &n);
int numberOfSteps = collatz(n);
printf("With n=%d, we need %d steps to reach the Collatz conjecture\n", n, numberOfSteps);
return 0;
}
int collatz(int n)
{
int steps = 0;
while(n != 1)
{
if(n % 2 == 0)
{
n /= 2;
}
else
{
n = (n*3) + 1;
}
steps++;
printf("step %d: %d\n", steps, n);
}
return steps;
}