Created
March 14, 2014 10:39
-
-
Save klen/9545489 to your computer and use it in GitHub Desktop.
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 python | |
import doctest | |
import itertools as it | |
import math | |
def divisor_generator(num): | |
""" | |
>>> list(divisor_generator(100)) | |
[1, 2, 4, 5, 10, 20, 25, 50] | |
""" | |
large_divisors = [] | |
yield 1 | |
for i in range(2, int(math.sqrt(num) + 1)): | |
if num % i is 0: | |
yield i | |
if i is not num / i: | |
large_divisors.insert(0, num / i) | |
for divisor in large_divisors: | |
yield divisor | |
def powerset(xs): | |
""" | |
>>> list(powerset([1, 2, 3])) | |
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)] | |
""" | |
return it.chain.from_iterable( | |
it.combinations(xs, n) for n in range(len(xs) + 1)) | |
def check_pitch(num): | |
""" | |
>>> check_pitch(35) | |
False | |
>>> check_pitch(70) | |
True | |
""" | |
divisors = list(divisor_generator(num)) | |
if sum(divisors) <= num: | |
return False | |
subsums = set(map(sum, powerset(divisors))) | |
if not num in subsums: | |
return True | |
def pitch(mx): | |
""" | |
>>> pitch(553) | |
70 | |
""" | |
for num in range(1, mx + 1): | |
if check_pitch(num): | |
return num | |
if __name__ == "__main__": | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment