Created
August 18, 2012 22:32
-
-
Save theonewolf/3390179 to your computer and use it in GitHub Desktop.
Project Euler
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
| #!/usr/bin/env python | |
| import sys | |
| def sieve_of_erastothenes(till): | |
| primelist = [True]*(till+1) | |
| primelist[0] = primelist[1] = False | |
| nextprime = (i for i,v in enumerate(primelist) if v) | |
| while (True): | |
| try: | |
| prime = nextprime.next() | |
| except StopIteration: | |
| return primelist | |
| for i in xrange(prime*prime,till+1,prime): | |
| primelist[i] = False | |
| def primelist_to_ints(primelist): | |
| return [i for i,v in enumerate(primelist) if v] | |
| if __name__ == '__main__': | |
| till = int(sys.argv[1]) | |
| print sum(primelist_to_ints(sieve_of_erastothenes(till))) |
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
| #!/usr/bin/env python | |
| # down, right, or diagonally | |
| grid = open('grid').read().strip() | |
| grid = [x.split(' ') for x in grid.split('\n')] | |
| cur_set = [01,01,01,01] | |
| cur_max = sum(cur_set) | |
| for i in xrange(len(grid)): # y | |
| for j in xrange(len(grid[i])): #x | |
| try: | |
| down = [int(grid[i][j]), | |
| int(grid[i+1][j]), | |
| int(grid[i+2][j]), | |
| int(grid[i+3][j])] | |
| except IndexError: | |
| down = [01,01,01,01] | |
| try: | |
| right = [int(grid[i][j]), | |
| int(grid[i][j+1]), | |
| int(grid[i][j+2]), | |
| int(grid[i][j+3])] | |
| except IndexError: | |
| right = [01,01,01,01] | |
| try: | |
| diag1 = [int(grid[i][j]), | |
| int(grid[i+1][j+1]), | |
| int(grid[i+2][j+2]), | |
| int(grid[i+3][j+3])] | |
| except IndexError: | |
| diag = [01,01,01,01] | |
| try: | |
| diag2 = [int(grid[i][j]), | |
| int(grid[i+1][j-1]), | |
| int(grid[i+2][j-2]), | |
| int(grid[i+3][j-3])] | |
| except IndexError: | |
| diag2 = [01,01,01,01] | |
| if (sum(down) > cur_max): | |
| cur_set = down[:] | |
| cur_max = sum(cur_set) | |
| if (sum(right) > cur_max): | |
| cur_set = right[:] | |
| cur_max = sum(cur_set) | |
| if (sum(diag1) > cur_max): | |
| cur_set = diag1[:] | |
| cur_max = sum(cur_set) | |
| if (sum(diag2) > cur_max): | |
| cur_set = diag2[:] | |
| cur_max = sum(cur_set) | |
| print 'cur_set: %s' % cur_set | |
| print 'cur_max: %d' % (cur_set[0]*cur_set[1]*cur_set[2]*cur_set[3]) |
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
| #!/usr/bin/env python | |
| f = open('numbers') | |
| maxprod = [0,0,0,0,0] | |
| maxsum = sum(maxprod) | |
| currprod = [0,0,0,0,0] | |
| currsum = sum(maxprod) | |
| for line in f: | |
| for i in line.strip(): | |
| i = int(i) | |
| currsum -= currprod.pop(0) | |
| currprod.append(i) | |
| currsum += i | |
| if currsum > maxsum: | |
| maxprod = currprod[:] | |
| maxsum = currsum | |
| print maxprod | |
| prod = 1 | |
| for i in maxprod: | |
| prod *= i | |
| print prod |
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
| #!/usr/bin/env python | |
| import math; print sum([int(i) for i in str(math.factorial(100))]) |
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
| #!/usr/bin/env python | |
| aset = [a for a in xrange(3,501,1)] | |
| bset = [b for b in xrange(4,501,1)] | |
| for a in aset: | |
| for b in bset: | |
| if math.sqrt(a*a + b*b).is_integer() and a+b+math.sqrt(a*a+b*b) == 1000: | |
| print '%d**2 + %d**2 == %d**2; %d' % (a,b,math.sqrt(a*a+b*b),a*b*math.sqrt(a*a+b*b)) | |
| exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment