diff --git a/README.markdown b/README.markdown index cc43ed9..47b2b8a 100644 --- a/README.markdown +++ b/README.markdown @@ -39,6 +39,7 @@ Should be used in order to help future reuse of code :) 24 - What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? - too long
25 - What is the first term in the Fibonacci sequence to contain 1000 digits? - 0.741
27 - Find a quadratic formula that produces the maximum number of primes for consecutive values of n. - too long
+28 - What is the sum of both diagonals in a 1001 by 1001 spiral? - < 1 sec
29 - How many distinct terms are in the sequence generated by ab for 2 a 100 and 2 b 100? - < 1 sec
30 - Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. - < 3 sec
34 - Find the sum of all numbers which are equal to the sum of the factorial of their digits. - 30 sec
@@ -52,7 +53,6 @@ Should be used in order to help future reuse of code :) **In progress: ** 26 - Find the value of d < 1000 for which 1/d contains the longest recurring cycle.
-28 - What is the sum of both diagonals in a 1001 by 1001 spiral?
35 - How many circular primes are there below one million?
39 - If p is the perimeter of a right angle triangle, {a, b, c}, which value, for p <= 1000, has the most solutions?
diff --git a/e_28.py b/e_28.py index 7828518..8fbce55 100644 --- a/e_28.py +++ b/e_28.py @@ -17,14 +17,30 @@ 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? ''' -def sum_power(): +def sum_diag(max_lines): """ - Finds + Returns the sum of both diagonals of the square of max_lines size """ + dsum = 1 # sum of diagonals + cpt = 1 # number of lines processed + val = 1 # value of the current place in the square + inc = 0 # the increment between number for one line + + while cpt < max_lines: + cpt += 2 + inc += 2 + + for corner in range(4): + val += inc + dsum += val - - return 1 + return dsum if __name__ == '__main__': - # The major problem in there is to find the upper limit. - print "Answer : %d " % (sum_power()) + # The major problem in there is to find the logic + #n = 1 gives 1 + #n = 3 gives 3, 5, 7, 9 => +2 + #n = 5 gives 13, 17, 21, 25 => +4 + #n = 7 gives 31, 37, 43, 49 => +6 + #n = 9 gives 57, . . . + print "Answer : %d " % (sum_diag(1001))