Last active
December 9, 2017 13:10
-
-
Save dlozeve/8729159eb9aa949129ac8f88fdead6b9 to your computer and use it in GitHub Desktop.
Find the number N_max from the interval [60000, 1000000] that has the highest strength among all numbers in the interval
This file contains 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
#!/usr/bin/env python3 | |
from functools import reduce | |
from multiprocessing import Pool | |
from operator import itemgetter | |
def factors(n): | |
return set(reduce(list.__add__, | |
([i, n//i] for i in range(1, int(n**0.5) + 1) | |
if not n % i))) | |
def strength(N): | |
s = sum(factors(N)) | |
return(N, s/N) | |
if __name__ == "__main__": | |
with Pool(4) as p: | |
numbers = range(60000, 1000000) | |
strengths = p.map(strength, numbers) | |
(N, s) = max(strengths, key=itemgetter(1)) | |
print(f"N = {N}") | |
print(f"Factors = {factors(N)}") | |
print(f"Strength = {s}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment