Sunday, December 5, 2010

Puzzle 5

Profiting from my overwrought example 3


from bf7 import *
from sys import argv

if __name__ == "__main__":
if len(argv) == 1:
upto = 10
else:
upto = int(argv[1])
allprimes = primesupto(upto)

result = 1
for prime in allprimes:
factor = prime
while factor < upto:
result *= prime
factor *= prime
print result


For me it would have been less work to just write

print 2 * 2 * 2 * 2 * 3 * 3 * 5 * 7 * 11 * 13 * 17 * 19

Best solution, for those who can remember grade school:



def gcd(a, b):
while(b != 0):
a, b = b, a%b
return a

def lcm(a,b):
return a * b / gcd(a, b)

print reduce(lcm, range(2, 21))

No comments: