Add python's frozenset for comparison
This commit is contained in:
parent
d91c5731ce
commit
24fc9774a0
|
@ -159,6 +159,49 @@ def py_fibonacci():
|
||||||
yield b
|
yield b
|
||||||
a, b = b, a + 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__':
|
if __name__ == '__main__':
|
||||||
start_time = time.monotonic()
|
start_time = time.monotonic()
|
||||||
limit = J(2**128)
|
limit = J(2**128)
|
||||||
|
@ -168,6 +211,13 @@ if __name__ == '__main__':
|
||||||
#print(I(number), tuple(map(I, number)), str(number))
|
#print(I(number), tuple(map(I, number)), str(number))
|
||||||
print(time.monotonic() - start_time)
|
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()
|
start_time = time.monotonic()
|
||||||
limit = 2**128
|
limit = 2**128
|
||||||
for number in py_fibonacci():
|
for number in py_fibonacci():
|
||||||
|
|
Loading…
Reference in New Issue