This file contains hidden or 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
| # set up prime sieve to n | |
| n=10000 | |
| myPrimes = [True]*(n+1) | |
| last = 0 | |
| for i in range (2,n): | |
| if myPrimes[i] == True: | |
| j = 2*i | |
| while j<=n: | |
| myPrimes[j]=False |
This file contains hidden or 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
| # http://projecteuler.net/problem=61 | |
| # this should run in 500ms | |
| import itertools | |
| # these are the formulas for the six polygonal numbers | |
| def fn(n): | |
| return [n*(n+1)/2,n*n,n*(3*n-1)/2,n*(2*n-1),n*(5*n-3)/2,n*(3*n-2)] | |
| # this is what we'll use for the cyclic pattern matching |
This file contains hidden or 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
| # http://projecteuler.net/problem=62 | |
| def cube(n): | |
| return n*n*n | |
| def hash(n): | |
| string = str(n) | |
| numbers = [] | |
| for i in range(0,len(string)): | |
| numbers.append(string[i:i+1]) |
This file contains hidden or 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
| # pretty straightforward- but i'll leave the math here to you | |
| count = 0 | |
| for a in range (1,10): | |
| for b in range (1,30): | |
| if len(str(a**b))==b: | |
| count+=1 | |
| print count |
This file contains hidden or 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 math | |
| squares=[] | |
| for i in range (1,10000): | |
| squares.append(i*i) | |
| def CF_of_sqrt(n): | |
| # modified this from http://eli.thegreenplace.net/2009/06/19/project-euler-problem-66-and-continued-fractions/ | |
| # who got it from http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/cfINTRO.html#sqrtalgsalg | |
| if n in squares: |
This file contains hidden or 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
| # pattern in convergents of e | |
| # 1 + 2 * 1 = 3 | |
| # 2 + 3 * 2 = 8 | |
| # 3 + 8 * 1 = 11 | |
| # new term = two terms ago + one term ago * i | |
| def make_e(n): | |
| CF = [2] | |
| c = 1 | |
| while len(CF)<n: |
This file contains hidden or 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
| # http://projecteuler.net/problem=66 | |
| # this is kind of a bear- it combines all the continued fraction stuff | |
| # but it runs in 147ms | |
| import math | |
| squares=[] | |
| for i in range (1,101): | |
| squares.append(i*i) |
This file contains hidden or 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
| # http://projecteuler.net/problem=67 | |
| # i wrote problem 18 in javascript- http://projecteuler.net/index.php?section=problems&id=18, https://gist.github.com/1380694, so i rewrote it here in python | |
| file = open('triangle.txt','r').readlines() | |
| # this opens the file, cleans it of the line breaks, and then imports it into a 2D array | |
| for i in range (0,len(file)): | |
| file[i]=file[i][0:-2] | |
| array=[] | |
| for j in range (0,len(file)): |
This file contains hidden or 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
| # http://projecteuler.net/problem=69 | |
| # first set up a special sieve- factors[j] is the totient for each number | |
| factors = [1]*1000001 | |
| n=1000000 | |
| myPrimes = [True]*(n+1) | |
| last = 0 | |
| for i in range (2,n): | |
| if myPrimes[i] == True: | |
| j = 2*i |
This file contains hidden or 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
| factors = [1]*10000001 | |
| for i in range (0,len(factors)): | |
| factors[i]=i | |
| # set up prime sieve to n | |
| n=10000000 | |
| myPrimes = [True]*(n+1) | |
| last = 0 | |
| for i in range (2,n): |