Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 3 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on Jun 14, 2011
@Worros Problem 28 solution e11b16b
@Worros Problem 54 first pass
- About to change the algorithm
901197f
@Worros Problem 58 solution
Did homework and went back to an earlier problems comments
9835994
Showing with 126 additions and 0 deletions.
  1. +81 −0 problem-28/Sorrow/solution.py
  2. +45 −0 problem-58/Sorrow/solution.py
View
81 problem-28/Sorrow/solution.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import time
+import codecs
+
+start = time.time()
+
+# Problem 28
+
+# Starting with the number 1 and moving to the right in a clockwise vec a 5 by 5 spiral is formed as follows:
+#
+# 21 22 23 24 25
+# 20 7 8 9 10
+# 19 6 1 2 11
+# 18 5 4 3 12
+# 17 16 15 14 13
+#
+# It can be verified that the sum of the numbers on the diagonals is 101.
+#
+# What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
+
+VERBOSE = 0
+
+def print_spiral(s):
+ for l in s:
+ print l
+
+def generate_spiral(size):
+ size = size
+ MAX = size*size
+ spiral = []
+ for i in range(0,size):
+ spiral.append([0]*size)
+ center = (size-1)/2
+ spiral[center][center] = 1
+
+ number = 2
+ leng = 1
+ vec = 1
+ xdir = center
+ ydir = center
+ while True:
+ # Horizontal arm
+ for i in range(1, leng + 1):
+ if number > MAX: #Ugh
+ break
+ spiral[ydir][xdir + i*vec] = number
+ number += 1
+ xdir += leng*vec
+ if number > MAX:
+ break
+ if xdir == size:
+ break
+ # Vertical arm
+ for j in range(1, leng + 1):
+ spiral[ydir + j*vec][xdir] = number
+ number += 1
+ ydir += leng*vec
+ leng += 1
+ vec = -vec
+
+ if VERBOSE: print_spiral(spiral)
+
+ return spiral
+
+def sum_diag(spiral, size):
+ sum = -1
+ for i in xrange(0, size):
+ sum += spiral[i][i]
+ sum += spiral[i][size - 1 - i]
+ return sum
+
+size = 1001
+
+spiral = generate_spiral(size)
+
+print sum_diag(spiral, size)
+
+end = time.time() - start
+print "Time Taken: " + str(end) + "ms"
View
45 problem-58/Sorrow/solution.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import time
+import codecs
+
+start = time.time()
+
+# Problem 58
+
+VERBOSE = 1
+
+import gmpy
+
+sieve = {2:True}
+primes = [2]
+
+def is_prime(n):
+ if gmpy.is_prime(n) > 0:
+ return True
+ return False
+
+def generate_diags(n):
+ """n^2-n+1 n^2
+ n^2-2n+2 n^2-3n+3
+ Solution from problem 28 comments"""
+ return [n*n-n+1, n*n, n*n-(2*n)+2, n*n-(3*n)+3]
+
+primes = 0
+x = 3
+
+while True:
+ tmp = generate_diags(x)
+ for i in tmp:
+ if is_prime(i):
+ primes += 1
+ den = 2*x-1
+ if float(primes)/float(den) < 0.10:
+ print "Solution: %s" % x
+ break
+ x += 2
+
+
+end = time.time() - start
+print "Time Taken: " + str(end) + "ms"

No commit comments for this range

Something went wrong with that request. Please try again.