Thou Shalt Not Modify A List During Iteration

January 24, 2012

Some good coding tips from this blog….. http://unspecified.wordpress.com/2009/02/12/thou-shalt-not-modify-a-list-during-iteration/

Read the full article →

Project Euler 134 – Investigating consecutive primes and the Extended Euclidean Algorithm

January 22, 2012

http://projecteuler.net/problem=134 My code took 2.5 seconds. In order to optimize this code, I had to use the Extended Euclidean Algorithm that I read about in Wikipedia. Brute force won’t work, so this optimization speeds up the code substantially. In essence, you are solving for a linear congruence of the form ax = b mod n. [...]

Read the full article →

Project Euler 98 – Investigating words and anagrams that are square numbers

January 20, 2012

http://projecteuler.net/problem=98 A very hacky solution, I admit, but I got it to work. #euler 98 import time import math t0 = time.time() f = open(‘words.txt’, ‘r’) for line in f: a = line.split(‘,’) maxLength = 0 for i in range(len(a)): a[i] = a[i].strip(‘”‘) if len(a[i]) > maxLength: maxLength = len(a[i]) print “longest word has “, [...]

Read the full article →

Project Euler 101 – Building polynomial functions based on first k terms of a sequence

January 17, 2012

http://projecteuler.net/problem=101 #euler 101 # let’s use numpy for our linear algebra matrix operations import math from numpy import * # we’ll use the array data structure for vectors, matrix # multiplication, and inverses #tenth polynomial generating function (and the test cube function) def polyTerm(n): return int(1 – n + n*n – math.pow(n,3) + math.pow(n,4) – [...]

Read the full article →

Project Euler 102 notes – Triangle interiors

January 12, 2012

While I coded up my solution to Project Euler 102 using the article I read on Wolfram Alpha, I was trying to derive the steps they took to solve for a, b in the article – and I found it much more intuitive to just get to a, b by simply inverting the 2 x [...]

Read the full article →

Project Euler 102 – How many triangles contain the origin?

January 11, 2012

http://projecteuler.net/problem=102 I implemented the formula from this Wolfram Alpha article on “Triangle Interiors”. # euler 102 import math def dtn(a,b): # 2 by 2 determinant return a[0]*b[1] – b[0]*a[1] def points(p, p1, p2, p3): # take 4 points, check to see if p is in triangle p1p2p3 # v = v0 + av1 + bv2 [...]

Read the full article →

Machine Learning course – Statement of Accomplishment

January 10, 2012

Got my statement of accomplishment by email for the Machine Learning online course I took in the fall from Stanford’s Professor Andrew Ng. Psyched!! Machine Learning (Online Class) – Statement of Accomplishment(function() { var scribd = document.createElement(“script”); scribd.type = “text/javascript”; scribd.async = true; scribd.src = “http://www.scribd.com/javascripts/embed_code/inject.js”; var s = document.getElementsByTagName(“script”)[0]; s.parentNode.insertBefore(scribd, s); })();

Read the full article →

Project Euler 85 – Investigating number of rectangles in a rectangular grid

January 8, 2012

http://projecteuler.net/problem=85 Two good blogs had walkthroughs to this problem – Yacoby’s Wanderings and Key Zero Conversation # euler 85 import math def squares(row, col): return row * (row+1) * col * (col+1) / 4 topLim = 2000000 dist = 2000000 answer = () for r in range(1, 100): for c in range(1, 100): temp = [...]

Read the full article →

Project Euler 99 – Which base exponent pair has the largest value?

January 8, 2012

http://projecteuler.net/problem=99 # euler 99 #hint: use log identity # log(a^x) = x log (a) import math f = open(‘base_exp.txt’, ‘r’) largest = 0 biggestLine = 0 counter = 1 for line in f: temp = line.split(‘,’) base = int(temp[0]) exp = int(temp[1]) thelog = exp * math.log(base) if thelog > largest: largest = thelog biggestLine [...]

Read the full article →

Project Euler 100 – Finding the number of discs for which the probability of taking two is 50 percent

January 7, 2012

http://projecteuler.net/problem=100 This was a crazy difficult problem for me. With brute force, you could find some of the possible solutions, but they wanted you to find the first possible solution with more than 10 ^12 discs, so simple brute force won’t work. I thought this would be a simple probability question – but the problem [...]

Read the full article →