Problem 203

completed January 23, 2012

Comments

This one was pretty easy. I took some definition snippets from a previous script. I wrote the nCk function, though I bet Python already has it.

Code

from math import factorial

def isprime(n):
	if n == 2:
		return True
	elif n < 2 or n%2 == 0:
		return False
	else:
		for i in range(3, int(n**0.5 + 1), 2):
			if n%i == 0: return False
		return True

def nextprime(p):
	if p == 2:
		return 3
	else:
		while True:
			p += 2
			if isprime(p) == True:
				return p

def squarefree(n):
	p = 2
	while n > 1:
		i = 1
	 	while n%(p**i) == 0:
			i += 1
		if i == 2:
			n /= p**(i-1)
		elif i > 2:
			return False
		p = nextprime(p)
	return True

def C(n, k):
	return (factorial(n)/(factorial(k)*factorial(n-k)))


total = 0
tested = set()
for n in xrange(51):
	for k in xrange(n+1):
		this = C(n, k)
		if this not in tested:
			tested.add(this)
			if squarefree(this) == True:
				total += this

print total