3n+1 conjecture (Python implementation)

Source: Internet
Author: User

Karaz (Callatz) conjecture:

For any natural number n, if it is even, cut it in half, and if it is odd, cut it off by half (3n+1). This has been repeatedly cut down, and finally must be in a certain step to get n=1. Karaz at the 1950 World Mathematicians conference published this conjecture, the legend of Yale University teachers and students to mobilize, desperately want to prove this seemingly silly very naïve proposition, the result of students do not want to study, the only card (3n+1), so that some people say this is a conspiracy, Karaz is deliberately delaying the progress of teaching and research in the American mathematical world ...

Our topic today is not to prove Karaz conjecture, but to give no more than 1000 positive integers n, simply to count, how many steps (a few cuts) are needed to get n=1?

Input: Positive integer n,

Output: Number of steps

Code:

1n = Int (input ("Please enter a positive integer:"))2 #Initialize a counter to calculate the number of steps3num =04 #Cycle Conditions5  whilen! = 1:6     #N is even7     ifN%2 = =0:8n = N/29     Else:Ten         #N is an odd number Onen = (3*n+1)/2 A    #counter plus 1 -num + = 1 - Print("need {} step altogether". Format (num))

3n+1 conjecture (Python implementation)

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.