Longest Collatz sequence
2014-03-01 17:33
288 查看
问题:
The following iterative sequence is defined for the set of positive integers:
n
n/2
(n is even)
n
3n +
1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
13
40
20
10
5
16
8
4
2
1
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is 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 are allowed to go above one million.
代码:
大牛的代码:
。。。。好像跟我的都不差不多,艹了。。。
The following iterative sequence is defined for the set of positive integers:
n
n/2
(n is even)
n
3n +
1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
13
40
20
10
5
16
8
4
2
1
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is 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 are allowed to go above one million.
代码:
import urllib ###############主程序################### known={1:1} findnext={2:1} def Getnext (n): if n in findnext: return findnext else: nextnum=0 if n%2: nextnum=3*n+1 else: nextnum=n/2 findnext =nextnum return nextnum def maxfact (x,y): if x[0]>y[0]: return x else: return y def fact (n): if n in known: return known else: temp=0 nextnum=0 """ con_action=[(lambda x: not x%2,lambda x:x/2), (lambda x: x%2,lambda x:3*x+1)] for indice,(con,action) in enumerate(con_action) : if con(n): nextnum= action(n) break """ nextnum=0 if n%2: nextnum=3*n+1 else: nextnum=n/2 temp+=1 temp+=fact(nextnum) known =temp return temp if __name__=="__main__": li=(1,fact(1)) """ lishow=[0]*2000000 lindex=0 endindex=int(1000000-1)/3 for rindex in xrange(1000000,endindex,-1): if not lishow[rindex]: thisvalue=fact(rindex) if thisvalue>li[1]: li=(rindex,thisvalue) s_index=rindex while s_index !=1: tempvalue=lishow[s_index] if tempvalue==1: break lishow[s_index]=1 s_=Getnext(lindex) print li """ li=reduce(lambda x,y:x if x[0]>y[0] else y , [(fact(x),x) for x in xrange(1,1000000) ]) #li.sort(reverse=True) #print known[known.keys()[-2]] print li时间是4S-5S,好慢,艹了。。。
大牛的代码:
。。。。好像跟我的都不差不多,艹了。。。
相关文章推荐
- 欧拉计划(14)Longest Collatz sequence
- Project Euler 014 Longest Collatz sequence
- Longest Collatz sequence
- PE 014 Longest Collatz sequence
- projecteuler---->problem=14----Longest Collatz sequence
- (Problem 14)Longest Collatz sequence
- 欧拉项目第14题 Longest Collatz sequence
- Problem 14 Longest Collatz sequence(暴力)
- Problem 14:Longest Collatz sequence
- 14 Longest Collatz sequence - Project Euler
- (Problem 14)Longest Collatz sequence
- Problem 14:Longest Collatz sequence
- project euler 14 Longest Collatz sequence
- (Problem 14)Longest Collatz sequence
- projecteuler---->problem=14----Longest Collatz sequence
- Project Euler Problem 14 Longest Collatz sequence
- Project Euler: Problem 14 Longest Collatz sequence
- leetcode 最长连续序列 longest consecutive sequence
- LeetCode -- Longest Consecutive Sequence
- 【leetcode 128】Longest Consecutive Sequence