Euler program (python) problem 14

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.