summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorSanto Cariotti <sancn@live.com>2017-05-16 17:20:29 +0200
committerSanto Cariotti <sancn@live.com>2017-05-16 17:20:29 +0200
commit9a41a4e2ac2d040e23417689aeab499ad915a9c3 (patch)
tree2b15ea45456ddf62f33c8451f28761e3cb82dc5b /python
parent18022d73c1c3f86651e55b4ecf22d6f357c53d75 (diff)
modified somme.cpp and added somme.py
Diffstat (limited to 'python')
-rw-r--r--python/somme.py42
1 files changed, 42 insertions, 0 deletions
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