This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x="73167176531330624919225119674426574742355349194934\ | |
96983520312774506326239578318016984801869478851843\ | |
85861560789112949495459501737958331952853208805511\ | |
12540698747158523863050715693290963295227443043557\ | |
66896648950445244523161731856403098711121722383113\ | |
62229893423380308135336276614282806444486645238749\ | |
30358907296290491560440772390713810515859307960866\ | |
70172427121883998797908792274921901699720888093776\ | |
65727333001053367881220235421809751254540594752243\ | |
52584907711670556013604839586446706324415722155397\ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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=[] |
NewerOlder