Skip to content

Instantly share code, notes, and snippets.

@jakedobkin
jakedobkin / gist:1575590
Created January 7, 2012 18:39
Project Euler 94
# http://projecteuler.net/problem=94
# we're going to solve this using diophantine equations
# first convert area formula to -3a**2-2a+4c**2+1=0 and -3a**2+2a+4c**2+1=0
# then run that through this solver http://www.alpertron.com.ar/QUAD.HTM to get 2 recursive equations
limit = 1000000000
sum = 0
# a-1 case
a = 1
@jakedobkin
jakedobkin / gist:1562076
Created January 4, 2012 20:51
Euler 100
# http://projecteuler.net/problem=100
# algabraic rearrangement to 2b**2-2b-t**2+t = 0 (where b is blue balls, t is total balls)
# this is a diophantine equation, which can be solved recursively- to get formulas for next b,t:
# http://www.alpertron.com.ar/QUAD.HTM, put in coeeficients above - got that from Dreamshire
b = 85
t = 120
while t < 10**12:
# i learned that this allows you to set both at once so you don't have to use an intermediate variable
@jakedobkin
jakedobkin / gist:1558222
Created January 4, 2012 02:52
Euler 85
# http://projecteuler.net/problem=85
# notes- formula to count boxes is just x * x+1 / 2 * y * y+1 /2
# this is triangle number x * triangle number y
def triangle(n):
return n*(n+1)/2
# i found the ranges by trial and error, starting at 100 each
min = 2000000
for a in range (0,37):
@jakedobkin
jakedobkin / gist:1556019
Created January 3, 2012 17:42
Problem 95 in Progress- Full Divisor Set
# http://projecteuler.net/problem=95
# brute force sieve
n=1000000
sequences = {}
divisorSum = [1]*(n+1)
for i in range (2,n/2+1):
j = 2*i
while j<=n:
divisorSum[j]+=i
j = j+i
@jakedobkin
jakedobkin / gist:1548446
Created January 1, 2012 21:53
Euler 87
# http://projecteuler.net/problem=87
# set up prime sieve to 50MM**.25
n=7100
myPrimes = [True]*(n+1)
last = 0
for i in range (2,n):
if myPrimes[i] == True:
j = 2*i
while j<=n:
@jakedobkin
jakedobkin / gist:1546271
Created January 1, 2012 04:41
Project Euler 97
# this is the simplest way- show all but the last 10 digits
print str(28433*pow(2,7830457)+1)[2357197:]
# which is equivalent to
print (28433 * 2**7830457 + 1) % 10000000000
# which is equivalent to
print (28433 * pow(2, 7830457) + 1) % 10**10
# which is equivalent to
# http://projecteuler.net/problem=99
from math import log
file = open('base_exp.txt','r').readlines()
# i thought maybe if we worked the box in two sections- from the top down and from the bottom up
# meeting in the middle, where the target cell was- but this doesn't appear to work either
array = []
for i in range (0,len(file)):
@jakedobkin
jakedobkin / gist:1545655
Created December 31, 2011 23:38
Euler 92
# http://projecteuler.net/problem=92
# this version takes advantage of the fact that all numbers up to 10MM have roundfadds < 600
# so first compute those, then check them for each number from 1 to 10MM
# that shaves it from 6 minutes to 120s
def fadd(n):
sum = 0
for i in range (0,len(str(n))):
sum += int(str(n)[i:i+1])**2
return sum
@jakedobkin
jakedobkin / gist:1534868
Created December 29, 2011 16:34
Euler 82 - Doesn't Work Yet
file = open('matrix2.txt','r').readlines()
# i thought maybe if we worked the box in two sections- from the top down and from the bottom up
# meeting in the middle, where the target cell was- but this doesn't appear to work either
array = []
for i in range (0,len(file)):
line = file[i].split(',')
array.append(line)
n = len(array)
@jakedobkin
jakedobkin / gist:1525993
Created December 28, 2011 03:13
Project Euler 81
file = open('matrix.txt','r').readlines()
array = []
for i in range (0,len(file)):
line = file[i].split(',')
array.append(line)
#adding algorithm works backward from next to last row to 0 row
for r in range (79,-1,-1):
for s in range (79,-1,-1):