@@ -0,0 +1,1341 @@
+#!/usr/bin/env python%0A#-*-coding:utf-8-*-%0A%0A'''%0AStarting with 1 and spiralling anticlockwise in the following way,%0Aa square spiral with side length 7 is formed.%0A%0A37 36 35 34 33 32 31%0A38 17 16 15 14 13 30%0A39 18  5  4  3 12 29%0A40 19  6  1  2 11 28%0A41 20  7  8  9 10 27%0A42 21 22 23 24 25 26%0A43 44 45 46 47 48 49%0A%0AIt is interesting to note that the odd squares lie along the bottom right diagonal,%0Abut what is more interesting is that 8 out of the 13 numbers%0Alying along both diagonals are prime; that is, a ratio of 8/13 %E2%89%88 62%25.%0A%0AIf one complete new layer is wrapped around the spiral above,%0Aa square spiral with side length 9 will be formed. If this process is continued,%0Awhat is the side length of the square spiral for which%0Athe ratio of primes along both diagonals first falls below 10%25?%0A'''%0A%0Aimport math%0Aimport timeit%0A%0A%0Adef loop(p):%0A    total, primes, i = 1, 0, 1%0A    while i == 1 or float(primes)/total %3E= p:%0A        i += 2%0A        diagonals = %5Bi**2, i**2-(i-1), i**2-(i-1)*2, i**2-(i-1)*3%5D%0A        primes += sum(%5B1 for j in diagonals if is_prime(j)%5D)%0A        total += 4%0A    return i%0A%0A%0Adef is_prime(n):%0A    for i in range(2, int(math.sqrt(n))+1):%0A        if n %25 i == 0:%0A            return False%0A    return True%0A%0A%0Aif __name__ == '__main__':%0A    print loop(0.1)%0A    print timeit.Timer('problem_058.loop(0.1)', 'import problem_058').timeit(1)%0A

add problem 058