From 9a41a4e2ac2d040e23417689aeab499ad915a9c3 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 16 May 2017 17:20:29 +0200 Subject: modified somme.cpp and added somme.py --- python/somme.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 python/somme.py (limited to 'python/somme.py') diff --git a/python/somme.py b/python/somme.py new file mode 100644 index 0000000..79e928c --- /dev/null +++ b/python/somme.py @@ -0,0 +1,42 @@ +def sequenza(n, pollatz, tCollatz = -1): + tot = 1 + + if pollatz: + m = 5 + else: + m = 3 + + pari = lambda n: ((n % 2) == 0) + + while n != 1: + if pari(n): + n /= 2 + else: + n = n*m+1 + + tot += 1 + + if tCollatz != -1 and tot > tCollatz: + break + + + return tot + + +with open('input.txt') as fin: + N = fin.readline().split(' ') + + +N[0] = int(N[0]) +N[1] = int(N[1]) + +tot = 0 + +for i in range(N[0], N[1]+1): + collatz = sequenza(i, False) + + if sequenza(i, True, collatz) < collatz: + tot += 1 + +with open('output.txt', 'w') as fout: + fout.write(str(tot)) \ No newline at end of file -- cgit v1.2.3-18-g5258