Files
contests/014/main.py

64 lines
1.1 KiB
Python

import os
import math
import numpy as np
# Given the following sequence, find the longest chain that ends with 1 and starts with some number under a million
# n -> n/2 | n is even
# n -> 3n + 1 | n is odd
def next(n):
if n % 2 == 0:
return int(n/2)
else:
return (3*n + 1)
def sequence(n):
res = [n]
while res[-1] != 1:
res.append(next(res[-1]))
return res
def getLongestSequence(n):
seqLengths = [0] * n
seqLengths[0] = 1
for x in range(1, n):
i = x + 1
l = 1
while True:
if i == 1:
break
elif i < n + 1 and seqLengths[i - 1] > 0:
l = l + seqLengths[i - 1] - 1
break
else:
l = l + 1
i = next(i)
seqLengths[x] = l
print(x + 1, l)
return seqLengths
def main():
print("Hello, this is Patrick")
t = getLongestSequence(1000000)
m = t[0]
j = 0
for i in range(len(t)):
if t[i] > m:
m = t[i]
j = i
print("Max", j + 1, m)
if __name__ == "__main__":
main()