Skip to content

Commit 3f67e42

Browse files
authored
Merge pull request ashutosh97#165 from andrecoco/master
Added CollatzConjecture
2 parents ed11e5e + a30538d commit 3f67e42

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Collatz Conjecture
2+
3+
The Collatz Conjecture says that if you take any number and follow the following steps, you will never be stuck in a loop.
4+
5+
If the number is even -> number = number/2;
6+
If the number is odd -> number = 3*number + 1;
7+
8+
If you continue to do this, at some point your number will be equal to 1.
9+
10+
Example: number = 26
11+
1 Step - number = 26/2 = 13
12+
2 Step - number = 13*3 + 1 = 40
13+
3 Step - number = 40/2 = 20
14+
4 Step - number = 20/2 = 10
15+
5 Step - number = 10/2 = 5
16+
6 Step - number = 3*5 + 1 = 16
17+
7 Step - number = 16/2 = 8
18+
8 Step - number = 8/2 = 4
19+
9 Step - number = 4/2 = 2
20+
10 Step - number = 2/2 = 1
21+
22+
For the number 26, 10 steps are needed, create a solution that calculates the number of steps for a given N number,
23+
and the biggest value the number had during the steps.
24+

CollatzConjecture/solution.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
4+
int main(){
5+
long int N = 26; //CALCULATING FOR N = 26
6+
long int biggestValue = 0;
7+
long int steps = 0;
8+
9+
while(N != 1){
10+
if(N%2 == 0){
11+
N = N/2;
12+
}else{
13+
N = N*3 + 1;
14+
}
15+
if(N>biggestValue){
16+
biggestValue = N;
17+
}
18+
steps++;
19+
}
20+
21+
printf("Number of Steps: %ld BiggestValue: %ld", steps, biggestValue);
22+
23+
}

0 commit comments

Comments
 (0)