Skip to content

Instantly share code, notes, and snippets.

@ebartrum
Last active August 29, 2015 14:12
Show Gist options
  • Save ebartrum/2c1196d674a3465b6f07 to your computer and use it in GitHub Desktop.
Save ebartrum/2c1196d674a3465b6f07 to your computer and use it in GitHub Desktop.
A solution to the 33rd Project Euler problem
#Define a function to return true if we have a digit cancelling fraction
"""The function will take 2 inputs: [a,b],[c,d].
It will return true if ab/cd is a digit cancelling fraction."""
#Define a function to return a common element of x,y
def commonElt(x,y):
for z in x:
if z in y:
return z
else:
return False
def dcfTest(x,y):
if commonElt(x,y)==False:
return False
else:
c=commonElt(x,y)
w=int(str(x[0])+str(x[1]))
z=int(str(y[0])+str(y[1]))
# w/z is our fraction.
x.remove(c)
y.remove(c)
if w*y[0]==z*x[0]:
return True
else:
return False
for a in range(1,10):
for b in range(1,10):
for c in range(1,10):
for d in range(1,10):
if [a,b]!=[c,d] and dcfTest([a,b],[c,d]) and int(str(a)+str(b))<int(str(c)+str(d)):
print str(a)+str(b)+"/"+str(c)+str(d)+" is a dcf"
"""This prints:
16/64 is a dcf
19/95 is a dcf
26/65 is a dcf
49/98 is a dcf """
"""answer: (1/4)*(1/5)*(2/5)*(1/2)=(1/100)"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment