Python Solution for UVa Problem: 100 - The 3n + 1 problem
The 3n + 1 problem is a popular program from UVa online judge, I have solved it using python .
Problem Description
Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.
Consider the following algorithm:
1. input n
2. print n
3. if n = 1 then STOP
4. if n is odd then n ← 3n + 1
5. else n ← n/2
6. GOTO 2
Given the input 22, the following sequence of numbers will be printed
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
For More details about the problem visit : The 3n + 1 problem
@functools.lru_cache(None) To Know details about it visti: https://docs.python.org/3/library/functools.html
Sample Input
1 10 100 200 201 210 900 1000
Sample Output
1 10 20 100 200 125 201 210 89 900 1000 174
If you got any better solution let me know in the comment box bellow.
Comments
Leave a comment
You are not LoggedIn but you can comment as an anonymous user which requires manual approval. For better experience please Login .