Skip to content

Instantly share code, notes, and snippets.

@flengyel
Created November 22, 2013 17:03
Show Gist options
  • Save flengyel/7603307 to your computer and use it in GitHub Desktop.
Save flengyel/7603307 to your computer and use it in GitHub Desktop.
Sum of 10 natural numbers is 2011, sum of their reciprocals is one. Uses fractions module for exact rational arithmetic, and map-reduce.
#!/usr/bin/env python
from fractions import Fraction
def verify(a):
return sum(a)== 2011 and sum(map ((lambda n: Fraction(1,n)), a)) == Fraction(1,1)
if __name__=='__main__':
x = [ [704, 4, 4, 4, 8, 704, 352, 176, 44, 11], [864, 3, 6, 8, 8, 8, 16, 18, 864, 216],
[912, 3, 4, 4, 19, 24, 912, 57, 38, 38], [920, 10, 5, 10, 23, 23, 920, 92, 4, 4],
[1080, 4, 4, 6, 9, 12, 12, 20, 432, 432], [1188, 3, 4, 6, 9, 9, 297, 297, 99, 99],
[1232, 2, 7, 11, 14, 14, 16, 22, 616, 77], [1260, 2, 6, 12, 21, 630, 20, 20, 20, 20],
[1440, 3, 5, 8, 12, 18, 480, 15, 15, 15], [1500, 6, 10, 10, 10, 10, 12, 375, 75, 3],
[1560, 3, 6, 10, 10, 10, 10, 12, 312, 78], [1584, 2, 4, 24, 33, 36, 16, 264, 24, 24],
[1680, 2, 4, 14, 14, 35, 35, 35, 96, 96], [1700, 2, 5, 10, 10, 34, 100, 50, 50, 50],
[1760, 2, 5, 11, 11, 40, 40, 32, 55, 55], [1800, 2, 6, 12, 18, 20, 20, 40, 18, 75],
[1820, 2, 10, 14, 26, 26, 28, 65, 10, 10], [1840, 4, 5, 8, 8, 10, 10, 23, 23, 80],
[1848, 4, 7, 8, 11, 22, 84, 9, 9, 9], [1872, 3, 8, 8, 8, 13, 13, 16, 18, 52] ]
print reduce ((lambda x, y: x and y), map( verify, x ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment