Skip to content

Instantly share code, notes, and snippets.

@jakedobkin
jakedobkin / gist:1457748
Created December 11, 2011 02:16
Euler 50
# set up prime sieve to n- track running sum in separate sum array
total = 0
n=1000000
myPrimes = [True]*(n+1)
mySum = [0]*(n+1)
last = 0
for i in range (2,n):
if myPrimes[i] == True:
j = 2*i
while j<=n:
@jakedobkin
jakedobkin / gist:1469303
Created December 12, 2011 21:55
Problem 51: PRime Families
# set up prime sieve to n- we'll need up to 999999 here, since we're investigating 6 figure combos
total = 0
n=1000000
myPrimes = [True]*(n+1)
mySum = [0]*(n+1)
last = 0
for i in range (2,n):
if myPrimes[i] == True:
j = 2*i
while j<=n:
@jakedobkin
jakedobkin / gist:1469544
Created December 12, 2011 22:57
Euler 52- Permuted Series
# http://projecteuler.net/problem=52
# we'll use the python built-in permutation function
import itertools
# 166K = 1MM/6, so that's the largest number under 1MM that could have identical # of digits
for k in range (100000,166666):
my_set = set()
perms = itertools.permutations(str(k), len(str(k)))
for i in perms:
@jakedobkin
jakedobkin / gist:1469926
Created December 13, 2011 01:11
Euler 53: Calculating Combinations
# http://projecteuler.net/problem=53
# python has a lot of useful math functions!
import math
# it also has itertools, which can give us the exact combinations of a set
# but here we're just calculating numbers, so i'll use the math factorial function
count = 0
for n in range (2,101):
@jakedobkin
jakedobkin / gist:1482724
Created December 15, 2011 20:26
Euler 54- Poker Hand Grader
# arrays for grading hands
numbers = ["2","3","4","5","6","7","8","9","T","J","Q","K","A"]
plays = ["0P","1P","2P","3K","4$","5F","6F","7F","8F","9F"]
# some functions we'll use- the basic approach for the grade function
# is to give each hand a string grade- when sorted, the better hand should come out on top
def l2n(letter):
index = numbers.index(letter)+2
@jakedobkin
jakedobkin / gist:1487384
Created December 16, 2011 18:53
Euler 55: Lychrel numbers
# http://projecteuler.net/problem=56
# first, a few functions we'll use- i particularly like the reverse
def reverse(string):
string = string[::-1]
return string
def is_pali(string):
if string==reverse(string):
# http://projecteuler.net/problem=57
# python is well suited to doing this through simple brute force
max = 0
for a in range (1,100):
for b in range (1,100):
a2b = str(a**b)
sum = 0
for x in range (0,len(a2b)):
digit = int(a2b[x:x+1])
@jakedobkin
jakedobkin / gist:1487926
Created December 16, 2011 20:54
Euler 57: Continued Fractions
# http://projecteuler.net/problem=58
# i found this confusing, so i worked through dreamshire's analysis
# numerator is always = numerator + denominator * 2
# denominator is = numerator + denominator
# starting with numerator 3 and denominator 2
# which is round 1, so start at round 2
numerator = 3
denominator = 2
@jakedobkin
jakedobkin / gist:1491175
Created December 17, 2011 19:43
Problem 58: Spiral Primes
# http://projecteuler.net/problem=58
# this is the same spiral from http://projecteuler.net/problem=28
# doing individual prime tests is faster than using a seive for numbers this large
def is_prime(n):
if n == 2 or n == 3: return True
if n < 2 or n%2 == 0: return False
if n < 9: return True
if n%3 == 0: return False
r = int(n**.5)
@jakedobkin
jakedobkin / gist:1491723
Created December 17, 2011 23:06
Euler 59: Crytography
# http://projecteuler.net/problem=59
code = [79,59,12,2,79,35,8,28,20,2,3,68,8,9,68,45,0,12,9,67,68,4,7,5,23,27,1,21,79,85,78,79,85,71,38,10,71,27,12,2,79,6,2,8,13,9,1,13,9,8,68,19,7,1,71,56,11,21,11,68,6,3,22,2,14,0,30,79,1,31,6,23,19,10,0,73,79,44,2,79,19,6,28,68,16,6,16,15,79,35,8,11,72,71,14,10,3,79,12,2,79,19,6,28,68,32,0,0,73,79,86,71,39,1,71,24,5,20,79,13,9,79,16,15,10,68,5,10,3,14,1,10,14,1,3,71,24,13,19,7,68,32,0,0,73,79,87,71,39,1,71,12,22,2,14,16,2,11,68,2,25,1,21,22,16,15,6,10,0,79,16,15,10,22,2,79,13,20,65,68,41,0,16,15,6,10,0,79,1,31,6,23,19,28,68,19,7,5,19,79,12,2,79,0,14,11,10,64,27,68,10,14,15,2,65,68,83,79,40,14,9,1,71,6,16,20,10,8,1,79,19,6,28,68,14,1,68,15,6,9,75,79,5,9,11,68,19,7,13,20,79,8,14,9,1,71,8,13,17,10,23,71,3,13,0,7,16,71,27,11,71,10,18,2,29,29,8,1,1,73,79,81,71,59,12,2,79,8,14,8,12,19,79,23,15,6,10,2,28,68,19,7,22,8,26,3,15,79,16,15,10,68,3,14,22,12,1,1,20,28,72,71,14,10,3,79,16,15,10,68,3,14,22,12,1,1,20,28,68,4,14,10,71,1,1,17,10,22,71,10,28,19,6,10,0,26,13,20,7,68,14,27,74,