Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions CollatzNumber.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
How this sequence works ??
Basically, it pick a number and than check if this number is
even or odd.
If the number is even then we divide it by 2,
if it's odd just multiply by 3 and add 1 more.
After a while the sequence becomes continuous in 1,
this is why ...
And of course only numbers > 0 will work.

Some INPUT/OUTPUT example:
-Imput: Contain the number to make the Collatz Sequence of it

-Output: Contain a single line output that contains all the Collatz sequence

TESTCASE:
Enter a number to calcule collatz: 34
The collatz sequence is: 34 17 52 26 13 40 20 10 5 16 8 4 2 1...
*/

#include <stdio.h>

int v[1000000];
int i = 0;

int makeCollatz(int n){
if(n <= 0){
return 0;
}
else if(n == 1){
return 1;
}
else{
v[i] = n;
i++;
if(n % 2 == 0){
n /= 2;
}
else{
n = 3 * n + 1;
}
}
return makeCollatz(n);
}

int main(void){
int number, aux;

printf("Enter a number to calcule collatz: \n");
scanf("%d", &number);
aux = makeCollatz(number);
if(aux == 0){
printf("Error\n");
}
else if(aux == 1){
v[i] = 1;
i++;
}

printf("The collatz sequence is: \n");
printf("%d", v[0]);
for(int j = 1; j < i; j++){
printf(" %d", v[j]);
}
printf("...\n");

return 0;
}