Commits

Panagiotis Mavrogiorgos  committed 1db7ae7

Added problem 1 solution

  • Participants

Comments (0)

Files changed (1)

File problem_1.py

+# -*- coding: utf-8 -*-
+"""
+Created on Wed Apr 13 17:07:03 2011
+
+@author: pmav99
+
+Add all the natural numbers below one thousand that are multiples of 3 or 5.
+Answer = 233168
+
+Functions "simple" and "sets" work for a lots of numbers. Not only two of them.
+Functions "functional" and "sum_of_multiples" work
+for two fixed numbers (vars a and b)
+"""
+##------------------------------------------------------------------------------
+
+from __future__ import division
+import sys
+import timeit
+
+sys.setrecursionlimit(20000)
+
+def simple(numbers, limit):
+    tot = 0
+    for i in range(min(numbers), limit):
+        for elem in numbers:
+            if i % elem == 0:
+                tot += i
+                break
+    return tot
+
+def sets(numbers, limit):
+    a = []
+    for number in numbers:
+        a.extend(range(number, limit, number))
+    return sum(list(set(a)))
+
+def functional(a, b, limit):
+    return sum(x for x in range(min(a,b), limit) if x % a == 0 or x % b == 0)
+
+def sum_of_multiples(a, b, limit):
+    return sum(range(a, limit, a)) + sum(range(b, limit, b)) - sum(range(a * b, limit, a * b))
+
+def recursion(a, b, limit, r):
+    if limit == 0:
+        r = 0
+        return r
+    elif (limit % a == 0) or (limit % b == 0):
+        r = recursion(a, b, limit - 1, r)
+        r += limit
+    else:
+        r = recursion(a, b, limit - 1, r)
+    return r
+
+def main():
+    times = 1000 # for time comparison
+    nums = (3,5)
+    limit = 1000
+    a = 3
+    b = 5
+
+    print(50 * "-")
+    print("System information")
+    print(sys.version)
+
+    print(50 * "-")
+    print("Check solutions")
+    print(50 * "-")
+    print(simple(nums, limit))
+    print(sets(nums, limit))
+    print(functional(a, b, limit))
+    print(sum_of_multiples(a, b, limit))
+    print(recursion(a, b, 999, 0))
+
+    # time comparison
+
+    print(50 * "-")
+    print("Time comparison")
+    print(50 * "-")
+    func = simple
+    t = timeit.Timer("{0}({1}, {2})".format(func.__name__, str(repr(nums)), limit),
+                     "from __main__ import {0}".format(func.__name__))
+    print("{0:30} => {1}".format(func.__name__, t.timeit(times)))
+
+    func = sets
+    t = timeit.Timer("{0}({1}, {2})".format(func.__name__, str(repr(nums)), limit),
+                     "from __main__ import {0}".format(func.__name__))
+    print("{0:30} => {1}".format(func.__name__, t.timeit(times)))
+
+    func = functional
+    t = timeit.Timer("{0}({1}, {2}, {3})".format(func.__name__, a, b, limit),
+                     "from __main__ import {0}".format(func.__name__))
+    print("{0:30} => {1}".format(func.__name__, t.timeit(times)))
+
+    func = sum_of_multiples
+    t = timeit.Timer("{0}({1}, {2}, {3})".format(func.__name__, a, b, limit),
+                     "from __main__ import {0}".format(func.__name__))
+    print("{0:30} => {1}".format(func.__name__, t.timeit(times)))
+
+    func = recursion
+    t = timeit.Timer("{0}({1}, {2}, {3}, {4})".format(func.__name__, a, b, limit, 0),
+                     "from __main__ import {0}".format(func.__name__))
+    print("{0:30} => {1}".format(func.__name__, t.timeit(times)))
+    print(50 * "-")
+
+if __name__ == "__main__":
+    main()