From 24fc9774a04effb2e71d44313e18c757d4b429ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sun, 29 Apr 2018 16:26:56 +0300 Subject: [PATCH] Add python's frozenset for comparison --- arithmetic.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/arithmetic.py b/arithmetic.py index d56ecd8..d506585 100644 --- a/arithmetic.py +++ b/arithmetic.py @@ -159,6 +159,49 @@ def py_fibonacci(): yield b a, b = b, a + b +def pyset_J(x): + powers = powers_of_two(x) + return frozenset(pyset_J(i) for i in powers) + +def pyset_lesser(x, y): + only_x = x.difference(y) + only_y = y.difference(x) + + if only_x == frozenset() and only_y != frozenset(): + return True + elif only_y == frozenset(): + return False + else: + x_max_power = functools.reduce(lambda a, b: b if pyset_lesser(a,b) else a, only_x) + y_max_power = functools.reduce(lambda a, b: b if pyset_lesser(a,b) else a, only_y) + return pyset_lesser(x_max_power, y_max_power) + +def pyset_lshift(x, y): + return frozenset(pyset_add(i, y) for i in x) + +def pyset_double(x): + return pyset_lshift(x, pyset_J(1)) + +def pyset_add(x, y): + only_x = x.difference(y) + only_y = y.difference(x) + one = only_x.union(only_y) + both = x.intersection(y) + + if both == frozenset(): + return one + else: + return pyset_add(one, pyset_double(both)) + +def pyset_fibonacci(): + a = pyset_J(0) + b = pyset_J(1) + + yield a + while True: + yield b + a, b = b, pyset_add(a, b) + if __name__ == '__main__': start_time = time.monotonic() limit = J(2**128) @@ -168,6 +211,13 @@ if __name__ == '__main__': #print(I(number), tuple(map(I, number)), str(number)) print(time.monotonic() - start_time) + start_time = time.monotonic() + limit = pyset_J(2**128) + for number in pyset_fibonacci(): + if pyset_lesser(limit, number): + break + print(time.monotonic() - start_time) + start_time = time.monotonic() limit = 2**128 for number in py_fibonacci():