commit c550d42f95ae65c1020ca262e14beb9304221036
parent 3121127719093ec2cbc92bf6633a0b0ba6ae1377
Author: therealFIGBERT <figbertwelner@gmail.com>
Date: Sat, 8 Feb 2020 14:16:39 -0800
:sparkles: Add solution to problem 14
Diffstat:
1 file changed, 26 insertions(+), 0 deletions(-)
diff --git a/problem014.py b/problem014.py
@@ -0,0 +1,26 @@
+# problemName = "Longest Collatz sequence"
+# problemNum = 14
+# solutionBy = "FIGBERT"
+# language = "Python"
+# dateCompleted = "06/02/2020"
+
+def collatz(num):
+ sequence = 1
+ while num > 1:
+ if num % 2 == 0:
+ num = num//2
+ else:
+ num = (3*num) + 1
+ sequence += 1
+
+ return sequence
+
+if __name__ == "__main__":
+ answer = [999999, 999999, collatz(999999)]
+ while answer[0] >= 1:
+ potential = (answer[0] - 1, collatz(answer[0] - 1))
+ if potential[1] > answer[2]:
+ answer[1] = potential[0]
+ answer[2] = potential[1]
+ answer[0] = answer[0] - 1
+ print(f"The starting number, under one million, that produces the longest chain from the given iterative sequence is {answer[1]}")