From 69ec3783775b9d3f3962603d390590c9844bf0f1 Mon Sep 17 00:00:00 2001 From: steven-y-e Date: Mon, 31 Jul 2023 16:03:10 -0400 Subject: [PATCH] feat(solution): problem #14 --- solutions/014/longestcollatzsequence.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 solutions/014/longestcollatzsequence.py diff --git a/solutions/014/longestcollatzsequence.py b/solutions/014/longestcollatzsequence.py new file mode 100644 index 0000000..67caee8 --- /dev/null +++ b/solutions/014/longestcollatzsequence.py @@ -0,0 +1,19 @@ +def collatz_to_one(n): + chainLength = 1 + while (n > 1): + if n % 2 == 0: + n = n/2 + else: + n = 3 * n + 1 + chainLength += 1 + return chainLength + +longestChain = 0 + +for i in range(1000000): + currentChain = collatz_to_one(i) + if currentChain > longestChain: + longestChain = currentChain + print("current longest chain: " + str(longestChain) + " (from " + str(i) + ")") + +print("done") \ No newline at end of file