The 3n 1 problem in Python

Tuesday, May 27, 2014

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. More infos HERE.


And heres the algorithm in Python:
------------------------------------------------------


#ciclu
def ciclu(nr):
contor=0;
while nr!=1:
if nr%2==1:
nr=3*nr+1
else:
nr=nr/2
contor=contor+1
contor=contor+1
return contor
#parcurgere
def parcurgere(i,j):
n=0
maxim=ciclu(i)
for k in range (i+1,j):
n=ciclu(k)
if maxim<=n:
maxim=n
print(i,j,maxim)
#main
sw=1
while sw==1:
i=int(input())
j=int(input())
parcurgere(i,j)
print("Continue? 1=Yes 0=No")
sw=int(input())


------------------------------------------------------

Related Posts by Categories

0 comments:

Post a Comment