aboutsummaryrefslogtreecommitdiffstats
path: root/collatz.c
diff options
context:
space:
mode:
authormjf <mjf@localhost.localdomain>2020-02-04 12:11:30 -0500
committermjf <mjf@localhost.localdomain>2020-02-04 12:11:30 -0500
commit6aa02212cd6dfbb492fa1f70b60a4fe3d48892e5 (patch)
tree1b655714c9709ee8fce333fcb6a6be3f5533b2d3 /collatz.c
parent8e431d287c0e89041506da4c4da57e3e3d657d72 (diff)
downloadProject_Euler_Solutions-6aa02212cd6dfbb492fa1f70b60a4fe3d48892e5.tar.gz
cleanup and added more c examples:
Diffstat (limited to 'collatz.c')
-rw-r--r--collatz.c18
1 files changed, 5 insertions, 13 deletions
diff --git a/collatz.c b/collatz.c
index 5bbb783..ec77af6 100644
--- a/collatz.c
+++ b/collatz.c
@@ -1,19 +1,13 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
-#include <string.h>
+// In C, it's immediately obvious my python solution is inefficient
-// Temporary storage chain
-//long int *chain;
-// The longest sequence found so far
-//long int *seq;
-
-// Size of the above arrays
+// For keeping track of the length of sequences
+// c is a temporary counter, s changes when c reaches a new maximum
long int c = 0;
long int s = 0;
long int collatz(long int seed){
- //chain = (long int *) realloc (chain, c * sizeof(long int));
- //chain[c - 1] == seed;
c++;
if(seed == 1)
return 0;
@@ -21,19 +15,17 @@ long int collatz(long int seed){
seed = seed / 2;
else
seed = 3 * seed + 1;
- //collatz(seed);
+ collatz(seed);
}
int main(){
for(long int i = 1; i < pow(10, 6); i++){
c = 0;
- //chain = (long int *) malloc(c * sizeof(long int));
collatz(i);
- printf("%d \n", i);
if(c > s){
s = c;
}
}
- printf("Has %d numbers \n", s);
+ printf("The longest sequence has %d numbers \n", s);
return 0;
}