Created
June 6, 2021 19:36
-
-
Save Nikolaj-K/19799cb9cd5b6a027faad61ee6b760c8 to your computer and use it in GitHub Desktop.
>Is there a way to generate pairs of numbers in ascending order of their product?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import reduce | |
from more_itertools import set_partitions | |
import operator | |
from sympy.ntheory import factorint | |
from time import sleep | |
def prod(nums): | |
return reduce(operator.mul, nums, 1) | |
def map_prod(partition): | |
return sorted(map(prod, partition)) | |
def naturals(): | |
n = 0 | |
while True: | |
yield n | |
n += 1 | |
for n in naturals(): | |
factors = [] | |
for k, v in factorint(n).items(): | |
factors += v * [k] | |
res = [] | |
for pair in map(map_prod, set_partitions(factors, 2)): | |
if pair not in res: # Avoid duplicates | |
res.append(pair) | |
print(n, res if res else "/") | |
sleep(.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment