Title
The following iterative sequence is defined for the set of positive integers:
nn/2 (is n even)
n3 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.
Translation:
The following sequence of loops is composed of positive integers according to the following rules:
n→ n /2 (if n an even number)
n→3 n + 1 (if n an odd number)
If the sequence starts at 13, the following sequence is generated:
13→40→20→10→5→16→8→4→2→1
Obviously the above series have 10 numbers, although not proven (famous Collatz conjecture), but we think that no matter what the number starts, the series will end at 1. Therefore, once the sequence has produced 1, it is considered to be the end of the series.
The question this time is: according to the above rule, which number under 1 million can produce the longest series?
Please note: The resulting sequence may contain items that are more than 1 million in number.
Import Timedef f (n): if N%2==1 and n>1: return F (3*n+1) +1 elif n%2==0: return F (N/2) +1 return 1m , Value=0,0begin=time.time () for I in Range (1,1000000): tmp=f (i) if tmp>m: value=i M=tmpprint Time.time ()-beginprint M,value