Longest Collatz Sequenceproblem 14
The following iterative sequence is defined for the set of positive integers:
n→ n /2 (is n even)
n→3 n + 1 (is n odd)
Using the rule above and starting with, we generate the following sequence:
13→40→20→10→5→16→8→4→2→1
It can be seen the sequence (starting at and finishing at 1) contains terms. Although it had not been proved yet (Collatz problem), it was thought that all starting numbers finish at 1.
Which starting number, under one million, produces the longest chain?
Note: Once The chain starts the terms is allowed to go above one million.
def func1 (x):
If x%2==0:
Return X/2
Else
Return 3*x+1
dict={}
def func2 (x):
X=FUNC1 (x)
If x==1:
Return 2
Rest=dict.get (x)
If Rest!=none:
Return rest+1
Else
RESULT=FUNC2 (x) +1
Dict[x]=result
return result
Value=0
K=0
For I in Range (1,1000000):
TEMP=FUNC2 (i)
If temp>k:
K=temp
Value=i
Print (value)
Time:3s
Euler program (python) problem 14