Skip to content

Instantly share code, notes, and snippets.

@ebartrum
ebartrum / gist:cba4fe5cebec90f85380
Last active August 29, 2015 14:12
463 in progress
#Define the function f by recursion:
def f(n):
if n==1:
return 1
if n==3:
return 3
if n%2==0:
return f(n/2)
if (n-1)%4==0:
m=(n-1)/4
@ebartrum
ebartrum / Euler 33
Last active August 29, 2015 14:12
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:
#Define a function pyth(a,b,c) which returns true if a^2+b^2=c^2
def pyth(a,b,c):
if a**2+b**2==c**2:
return True
else:
return False
#first check (1,x,y)
@ebartrum
ebartrum / Euler8
Created December 27, 2014 13:03
A solution to the 8th Project Euler problem
x="73167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
@ebartrum
ebartrum / Euler10
Last active August 29, 2015 14:12
A solution to the 10th Project Euler problem
#Define a function which checks if any members of x divide y (If so return True, otherwise False) but only checks up to the squareroot of y (This greatly improves efficiency).
#In other words if the function finds a member of x that is >= the sqrt of y it stops looking and returns the current state (ans)
def divis(x,y):
ans=False
for z in x:
if y%z==0:
ans=True
break
if z>=y**0.5+1:
break
@ebartrum
ebartrum / Euler7
Last active August 29, 2015 14:11
A solution to the 7th Project Euler problem
import time
#Define a function which checks if any members of x divide y (If so return True, otherwise False) but only checks up to the squareroot of y (This greatly improves efficiency).
#In other words if the function finds a member of x that is >= the sqrt of y it stops looking and returns the current state (ans)
def divis(x,y):
ans=False
for z in x:
if y%z==0:
ans=True
break
@ebartrum
ebartrum / Euler 28
Last active August 29, 2015 14:11
A solution to the 28th Project Euler problem
ans=1
n=1001
y=range(3,n+1,2)
#The solution comes from the observation that the sum of 4 nxn corner squares is the following poly (for odd n>1)
def poly(x):
ans=4*x**2-6*x+6
return ans
for o in y:
ans+=poly(o)
print ans
@ebartrum
ebartrum / Euler5
Last active August 29, 2015 14:10
A solution to the 5th Euler Challenge
"""I wish to create a function lcm(x) which gives the lowest common multiple of an array of numbers x"""
# I will need the factor function
def factor(x):
array=[]
i=1
while (i<=x):
if x%i==0:
array.append(i)
@ebartrum
ebartrum / Euler4
Last active August 29, 2015 14:10
A solution to the 4th Euler Challenge
"""A palindromic number reads the same both ways. Find the
largest palindrome made from the product of two 3-digit numbers."""
#Define a 'surround' function that will be used to construct palindromes
def surround(x,y):
x=str(x)
y=str(y)
return str((y+x+y))
#start with an initial array of all palidromes of length 1 or 2
@ebartrum
ebartrum / Euler3
Created November 30, 2014 21:59
A solution to the 3rd Euler Challenge
"""The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?"""
#I want to define prime(x) which gives x's prime factors
#Define factor(x)
def factor(x):
array=[]