Skip to content

Instantly share code, notes, and snippets.

@klen
Created March 14, 2014 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klen/9545489 to your computer and use it in GitHub Desktop.
Save klen/9545489 to your computer and use it in GitHub Desktop.
#!/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