The post The Parallelogram Law Proven Analytically appeared first on Dreamshire.
]]>Analytic proofs of geometric theorems can often be demonstrated more clearly by the use of coordinates as opposed to synthetic proofs which use deduction from axioms and previously derived theorems.
Prove analytically that the sum of the squares of the four sides of a parallelogram is equal to the sum of the squares of the
diagonals.
In ABCD, let d_{1} and d_{2} be the diagonals AC and BD, respectively. Construct a coordinate system so that A is the origin,
B lies on the positive x axis, and C and D lies above the x axis. See the diagram below. Assume
that AB is equal to DC and AD is equal to BC. We must prove that:
Let x be the x coordinate of B, let C have coordinates (x + x_{0}, y) and D have the coordinates (x_{0}, y). If x_{0} is 0 then the parallelogram is a rectangle.
Firstly, using the distance formula, let’s solve for the length of the two diagonals, d_{1} and d_{2}.
Now let’s sum the squares of the diagonals:
Secondly, again using the distance formula, let’s solve for the length of the two sides AB and AD.
Finally, we want to sum the squares of the sides and multiply by 2 (4 sides total, 2 of each length).
Thus, we have proven that the sum of the squares of the four sides of a parallelogram is equal to the sum of the squares of the diagonals.
The post The Parallelogram Law Proven Analytically appeared first on Dreamshire.
]]>The post How to crack the Ultra Hardware push button lock from Amazon appeared first on Dreamshire.
]]>As Scott points out, this rugged, sturdy lock is secure in looks only. Their claim of thousands of combinations are in fact only 252 and can be compromised in about 5 minutes. Here’s his analysis:
I thought I’d warn people that, despite its sturdy feel, it uses relatively weak security. Using it on the gate to the backyard is probably fine, as a deterrent, but using it to protect valuables is a bad idea.
The packaging mistakenly states there are 100,000 possible combinations, while in reality there are many fewer. There are only ten buttons, which can each be depressed once or not. That means there are a total of 2 to the tenth possible combinations, which is only 1024. Worse, since the lock only supports 5 digit combinations, that reduces the total number of possible combinations to 252. That means that if someone tried 30 combinations per minute (2 seconds each), they would necessarily solve it within 8 and a half minutes, and likely within 5 minutes.
Also, the combination is set at the factory and cannot be changed, which isn’t a huge deal, but might irritate some, as it prevents you from changing the combination periodically.
After someone commented erroneously that it indeed has 100,000 combinations, Scott continued to explain:
“What you’re saying *would* be true *if*:
In other words:
It’s like picking lottery numbers — each number can be picked only once, and the sequence doesn’t matter, which intuitively reduces the number of viable combinations. Perhaps surprisingly, it massively reduces them.
Those two facts change the way you have to do the math. Instead of a “permutation with repetition,” (or sometimes called “permutation with replacement”), as you suggested, it’s really a “combination with no repetition,” which you can read about on Wikipedia or other math sources covering “permutation vs. combination.” So, instead of 100,000 possible permutations, there really are just 252 combinations.
The equation changes from: 10 to the 5th power, instead to: 10 factorial divided by all of 5 factorial times 5 factorial, which looks something like this:
{0,1,2,3,4} {0,1,2,3,5} {0,1,2,3,6} {0,1,2,3,7} {0,1,2,3,8} {0,1,2,3,9} {0,1,2,4,5} {0,1,2,4,6} {0,1,2,4,7} {0,1,2,4,8} {0,1,2,4,9} {0,1,2,5,6} {0,1,2,5,7} {0,1,2,5,8} {0,1,2,5,9} {0,1,2,6,7} {0,1,2,6,8} {0,1,2,6,9} {0,1,2,7,8} {0,1,2,7,9} {0,1,2,8,9} {0,1,3,4,5} {0,1,3,4,6} {0,1,3,4,7} {0,1,3,4,8} {0,1,3,4,9} {0,1,3,5,6} {0,1,3,5,7} {0,1,3,5,8} {0,1,3,5,9} {0,1,3,6,7} {0,1,3,6,8} {0,1,3,6,9} {0,1,3,7,8} {0,1,3,7,9} {0,1,3,8,9} {0,1,4,5,6} {0,1,4,5,7} {0,1,4,5,8} {0,1,4,5,9} {0,1,4,6,7} {0,1,4,6,8} {0,1,4,6,9} {0,1,4,7,8} {0,1,4,7,9} {0,1,4,8,9} {0,1,5,6,7} {0,1,5,6,8} {0,1,5,6,9} {0,1,5,7,8} {0,1,5,7,9} {0,1,5,8,9} {0,1,6,7,8} {0,1,6,7,9} {0,1,6,8,9} {0,1,7,8,9} {0,2,3,4,5} {0,2,3,4,6} {0,2,3,4,7} {0,2,3,4,8} {0,2,3,4,9} {0,2,3,5,6} {0,2,3,5,7} {0,2,3,5,8} {0,2,3,5,9} {0,2,3,6,7} {0,2,3,6,8} {0,2,3,6,9} {0,2,3,7,8} {0,2,3,7,9} {0,2,3,8,9} {0,2,4,5,6} {0,2,4,5,7} {0,2,4,5,8} {0,2,4,5,9} {0,2,4,6,7} {0,2,4,6,8} {0,2,4,6,9} {0,2,4,7,8} {0,2,4,7,9} {0,2,4,8,9} {0,2,5,6,7} {0,2,5,6,8} {0,2,5,6,9} {0,2,5,7,8} {0,2,5,7,9} {0,2,5,8,9} {0,2,6,7,8} {0,2,6,7,9} {0,2,6,8,9} {0,2,7,8,9} {0,3,4,5,6} {0,3,4,5,7} {0,3,4,5,8} {0,3,4,5,9} {0,3,4,6,7} {0,3,4,6,8} {0,3,4,6,9} {0,3,4,7,8} {0,3,4,7,9} {0,3,4,8,9} {0,3,5,6,7} {0,3,5,6,8} {0,3,5,6,9} {0,3,5,7,8} {0,3,5,7,9} {0,3,5,8,9} {0,3,6,7,8} {0,3,6,7,9} {0,3,6,8,9} {0,3,7,8,9} {0,4,5,6,7} {0,4,5,6,8} {0,4,5,6,9} {0,4,5,7,8} {0,4,5,7,9} {0,4,5,8,9} {0,4,6,7,8} {0,4,6,7,9} {0,4,6,8,9} {0,4,7,8,9} {0,5,6,7,8} {0,5,6,7,9} {0,5,6,8,9} {0,5,7,8,9} {0,6,7,8,9} {1,2,3,4,5} {1,2,3,4,6} {1,2,3,4,7} {1,2,3,4,8} {1,2,3,4,9} {1,2,3,5,6} {1,2,3,5,7} {1,2,3,5,8} {1,2,3,5,9} {1,2,3,6,7} {1,2,3,6,8} {1,2,3,6,9} {1,2,3,7,8} {1,2,3,7,9} {1,2,3,8,9} {1,2,4,5,6} {1,2,4,5,7} {1,2,4,5,8} {1,2,4,5,9} {1,2,4,6,7} {1,2,4,6,8} {1,2,4,6,9} {1,2,4,7,8} {1,2,4,7,9} {1,2,4,8,9} {1,2,5,6,7} {1,2,5,6,8} {1,2,5,6,9} {1,2,5,7,8} {1,2,5,7,9} {1,2,5,8,9} {1,2,6,7,8} {1,2,6,7,9} {1,2,6,8,9} {1,2,7,8,9} {1,3,4,5,6} {1,3,4,5,7} {1,3,4,5,8} {1,3,4,5,9} {1,3,4,6,7} {1,3,4,6,8} {1,3,4,6,9} {1,3,4,7,8} {1,3,4,7,9} {1,3,4,8,9} {1,3,5,6,7} {1,3,5,6,8} {1,3,5,6,9} {1,3,5,7,8} {1,3,5,7,9} {1,3,5,8,9} {1,3,6,7,8} {1,3,6,7,9} {1,3,6,8,9} {1,3,7,8,9} {1,4,5,6,7} {1,4,5,6,8} {1,4,5,6,9} {1,4,5,7,8} {1,4,5,7,9} {1,4,5,8,9} {1,4,6,7,8} {1,4,6,7,9} {1,4,6,8,9} {1,4,7,8,9} {1,5,6,7,8} {1,5,6,7,9} {1,5,6,8,9} {1,5,7,8,9} {1,6,7,8,9} {2,3,4,5,6} {2,3,4,5,7} {2,3,4,5,8} {2,3,4,5,9} {2,3,4,6,7} {2,3,4,6,8} {2,3,4,6,9} {2,3,4,7,8} {2,3,4,7,9} {2,3,4,8,9} {2,3,5,6,7} {2,3,5,6,8} {2,3,5,6,9} {2,3,5,7,8} {2,3,5,7,9} {2,3,5,8,9} {2,3,6,7,8} {2,3,6,7,9} {2,3,6,8,9} {2,3,7,8,9} {2,4,5,6,7} {2,4,5,6,8} {2,4,5,6,9} {2,4,5,7,8} {2,4,5,7,9} {2,4,5,8,9} {2,4,6,7,8} {2,4,6,7,9} {2,4,6,8,9} {2,4,7,8,9} {2,5,6,7,8} {2,5,6,7,9} {2,5,6,8,9} {2,5,7,8,9} {2,6,7,8,9} {3,4,5,6,7} {3,4,5,6,8} {3,4,5,6,9} {3,4,5,7,8} {3,4,5,7,9} {3,4,5,8,9} {3,4,6,7,8} {3,4,6,7,9} {3,4,6,8,9} {3,4,7,8,9} {3,5,6,7,8} {3,5,6,7,9} {3,5,6,8,9} {3,5,7,8,9} {3,6,7,8,9} {4,5,6,7,8} {4,5,6,7,9} {4,5,6,8,9} {4,5,7,8,9} {4,6,7,8,9} {5,6,7,8,9}
Ultra Hardware push button lock from Amazon
The post How to crack the Ultra Hardware push button lock from Amazon appeared first on Dreamshire.
]]>The post Facebook hiring sample test – Missing Term in an Arithmetic Progression appeared first on Dreamshire.
]]>An Arithmetic Progression(AP) is defined as one in which there is a constant difference between the consecutive terms of a given series of numbers. You are provided with consecutive elements of an Arithmetic Progression. There is however one hitch: Exactly one term from the original series is missing from the set of numbers which have been given to you. The rest of the given series is the same as the original AP. Find the missing term.
The first line contains an Integer N, which is the number of terms which will be provided as input.
This is followed by N consecutive Integers, with a space between each pair of integers. All of these are on one line, and they are in AP (other than the point where an integer is missing).
You are provided with 5 integers. As you can can observe, they have been picked from a series, in which the starting term is 1 and the common difference is 2. The only aberration, i.e. the missing term (7), occurs between 5 and 9. This is the missing element which you need to find.
A_{1} and A_{n} are included and valid.
n = int(raw_input()) a = map(int, raw_input().split()) #n+1 because one element is missing from the series; a is a zero based list sx = (n+1) * (a[0] + a[n1]) / 2 #The missing element is the difference between the expected sum and the actual sum print sx  sum(a) 
The post Facebook hiring sample test – Missing Term in an Arithmetic Progression appeared first on Dreamshire.
]]>The post Project Euler 91 appeared first on Dreamshire.
]]>The points P (x_{1}, y_{1}) and Q (x_{2}, y_{2}) are plotted at integer coordinates and are joined to the origin, O(0,0), to form ΔOPQ.
There are exactly fourteen triangles containing a right angle that can be formed when each coordinate lies between 0 and 2 inclusive; that is,
0 ≤ x_{1}, y_{1}, x_{2}, y_{2} ≤ 2.
Given that 0 ≤ x_{1}, y_{1}, x_{2}, y_{2} ≤ 50, how many right triangles can be formed?
There is a hyper series here…somewhere.
Data Table 1. Number of triangles from 0 ≤ x_{1}, y_{1}, x_{2}, y_{2} ≤ {1..10}.
N  # Triangles 

1  3 
2  14 
3  33 
4  62 
5  101 
6  148 
7  207 
8  276 
9  353 
10  448 
Now, we can remove 3n^{2} known triangles that have an edge on either axis.
Data Table 2. Number of triangles as above but with 3n^{2} contributing to sum
N  3N^{2}  From some other source  # Triangles 

1  3  0  3 
2  12  2  14 
3  27  6  33 
4  48  14  62 
5  75  26  101 
6  108  40  148 
7  147  60  207 
8  192  84  276 
9  243  110  353 
10  300  148  448 
Data Table 3. Number of triangles as above but with N^{2}/2 contributing to sum
N  3N^{2}  N^{2}/2  From some other source  # Triangles 

1  3  0  0  3 
2  12  2  0  14 
3  27  4  2  33 
4  48  8  6  62 
5  75  12  14  101 
6  108  18  22  148 
7  147  24  36  207 
8  192  32  52  276 
9  243  40  70  353 
10  300  50  98  448 
Well, brute force until I figure out the series, which seems trivial but so far elusive.
Runs < 0.004 seconds in Python.
from Euler import gcd n = 50 t = 0 for x in range(1, n+1): for y in range(1, n): m = gcd(x,y) t += min(x*m/y, m*(ny)/x) print 'Answer to PE91 =', t*2 + n*n*3 
The post Project Euler 91 appeared first on Dreamshire.
]]>The post Project Euler 301 Solution appeared first on Dreamshire.
]]>Nim is a game played with heaps of stones, where two players take it in turn to remove any number of stones from any heap until no stones remain.
We’ll consider the threeheap normalplay version of Nim, which works as follows:
 At the start of the game there are three heaps of stones.
 On his turn the player removes any positive number of stones from any single heap.
 The first player unable to move (because no stones remain) loses.
If (n_{1},n_{2},n_{3}) indicates a Nim position consisting of heaps of size n_{1}, n_{2} and n_{3} then there is a simple function X(n_{1},n_{2},n_{3}) — that you may look up or attempt to deduce for yourself — that returns:
For example X(1,2,3) = 0 because, no matter what the current player does, his opponent can respond with a move that leaves two heaps of equal size, at which point every move by the current player can be mirrored by his opponent until no stones remain; so the current player loses. To illustrate:
 current player moves to (1,2,1)
 opponent moves to (1,0,1)
 current player moves to (0,0,1)
 opponent moves to (0,0,0), and so wins.
For how many positive integers n ≤ 2^{30} does X(n,2n,3n) = 0 ?
Classic use of the Fibonacci sequence. Just find the Fibonacci Number F_{L+2}, where L is the 2′s exponent.
Runs < 0.001 seconds in Python.
from Euler import fibonacci L = 30 #Limit is expressed as 2^L print "Answer to PE301 =", fibonacci(L+2) 
The post Project Euler 301 Solution appeared first on Dreamshire.
]]>The post Panmagic (Pandiagonal) and magic square generation, testing appeared first on Dreamshire.
]]>import string def make_magic_square(n): if n < 1 or n == 2: return False if n % 2 == 1: return make_odd_magic_square(n) else: if n % 4 == 0: return make_even_4n_magic_square(n) elif (n2) % 4 == 0: return make_even_4np2_magic_square(n) else: return False def make_panmagic_square(n): if n < 4: return False if n % 2 == 0: return make_even_panmagic_square(n) else: return make_odd_panmagic_square(n) def make_odd_magic_square(n): """ Source: http://blogs.mathworks.com/cleve/2012/11/05/magicsquarespart2algorithms/ This method is called the Siam Build I and J matrices as follows: 1 1 1 1 1 1 2 3 4 5 2 2 2 2 2 1 2 3 4 5 I=> 3 3 3 3 3 J=> 1 2 3 4 5 4 4 4 4 4 1 2 3 4 5 5 5 5 5 5 1 2 3 4 5 """ if n < 1 or n%2 == 0: return False #only allow odd squares 2n1, n>0 J = [range(1, n+1)] * n I = transpose(J) """ Using these indices, we generate two more matrices A and B A = mod(I + J + (n3)/2, n) B = mod(I + 2*J  2, n) 3 4 0 1 2 1 3 0 2 4 4 0 1 2 3 2 4 1 3 0 A=> 0 1 2 3 4 B=> 3 0 2 4 1 1 2 3 4 0 4 1 3 0 2 2 3 4 0 1 0 2 4 1 3 """ A = [[(I[i][j] + J[i][j] + (n3)/2) % n for i in range(n)] for j in range(n)] B = [[(I[i][j] + 2*J[i][j]  2) % n for j in range(n)] for i in range(n)] """ Both A and B are fledgling magic squares. They have equal row, column and diagonal sums. But their elements are not the integers from 1 to n^2. Each has duplicated elements between 0 and n1. The final statement: M = n*A + B + 1 produces a matrix whose elements are integers between 1 and n^2 and which has equal row, column and diagonal sums. What is not obvious, is that there are no duplicates and includes all of the integers between 1 and n^2 making it a magic square. """ return [[n*A[i][j] + B[i][j] + 1 for j in range(n)] for i in range(n)] def make_even_4np2_magic_square(n): """ Source: http://www.math.wichita.edu/~richardson/mathematics/ magic%20squares/evenordermagicsquares.html The algorithm starts by partitioning the square into 4 blocks of n/2 each; one for each quadrant. We fill those blocks with a known odd sized magic square, A, as: A in Q II, A + (n/2)^2 in Q III, A + 2*(n/2)^2 in Q I, and A + 3*(n/2)^2 in Q IV. This maintains the proper column totals, but not the row or diagonal totals. Correcting row totals is achieved by swaping selective columns (0, 1, 9) of the upper 10x10 matrix with the lower half. The columns affected are 0, 1, 2, ..., k and n1, n2, ... nk. (k=(n2)/4) Example (zero based): n=6 columns 0 n=10 columns 0, 1, 9 n=14 columns 0, 1, 2, 12, 13 n=18 columns 0, 1, 2, 3, 15, 16, 17 The final step is to switch 2 values in certain columns to correct the diagonals and make a magic square. (nxk1, [k1, k]) <> (nx+k, [k1, k]) Example (zero based): n=6 (1, 0) <> (4, 0), (1, 1) <> (4, 1) n=10 (2, 1) <> (7, 1), (2, 2) <> (7, 2) n=14 (3, 2) <> (10, 2), (3, 3) <> (10, 3) n=18 (4, 3) <> (13, 3), (4, 4) <> (13, 4) Fill in 4 Quads with 5x5 M.S. Swap upper and lower column sections 17 24 1 8 15 67 74 51 58 65 92 99 1 8 15 67 74 51 58 40 23 5 7 14 16 73 55 57 64 66 98 80 7 14 16 73 55 57 64 41 4 6 13 20 22 54 56 63 70 72 79 81 13 20 22 54 56 63 70 47 10 12 19 21 3 60 62 69 71 53 85 87 19 21 3 60 62 69 71 28 11 18 25 2 9 61 68 75 52 59 86 93 25 2 9 61 68 75 52 34 92 99 76 83 90 42 49 26 33 40 17 24 76 83 90 42 49 26 33 65 98 80 82 89 91 48 30 32 39 41 23 5 82 89 91 48 30 32 39 66 79 81 88 95 97 29 31 38 45 47 4 6 88 95 97 29 31 38 45 72 85 87 94 96 78 35 37 44 46 28 10 12 94 96 78 35 37 44 46 53 86 93 100 77 84 36 43 50 27 34 11 18 100 77 84 36 43 50 27 59 You will see our typical 5 x 5 magic square in Q II (top left corner) The finished M.S after the 2 switches 92 99 1 8 15 67 74 51 58 40 98 80 7 14 16 73 55 57 64 41 79 6 88 20 22 54 56 63 70 47 85 87 19 21 3 60 62 69 71 28 86 93 25 2 9 61 68 75 52 34 17 24 76 83 90 42 49 26 33 65 23 5 82 89 91 48 30 32 39 66 4 81 13 95 97 29 31 38 45 72 10 12 94 96 78 35 37 44 46 53 11 18 100 77 84 36 43 50 27 59 """ nx, k = n/2, (n2)/4 if n < 6 or nx % 2 == 0: return False #only allow even squares 4n+2, n>1 #make an odd nx x nx magic square A = make_odd_magic_square(nx) #fill in each quadrant with an augmentation of A according to algorithm I = A + sc_add(A, 3*nx*nx) J = sc_add(A, 2*nx*nx) + sc_add(A, nx*nx) #create initial square by concatenating I and J  column sums are "magic" B = [I[i]+J[i] for i in range(n)] #swap upper and lower halves of specific columns to make row sums "magic" for j in range(k) + range(n1, nk, 1): for i in range(nx): B[i][j], B[i+nx][j] = B[i+nx][j], B[i][j] #switch middle values for 2 columns to make diagonals (and square) magic B[nxk1][k1], B[nx+k][k1] = B[nx+k][k1], B[nxk1][k1] B[nxk1][k], B[nx+k][k] = B[nx+k][k], B[nxk1][k] return B def make_even_4n_magic_square(n): """ Source: http://mathworld.wolfram.com/MagicSquare.html An elegant method for constructing magic squares of doubly even order n=4m is to draw Xs through each 4 x 4 subsquare and fill all squares in sequence. Then replace each entry A[i][j] on a crossedoff diagonal by (n^2+1)  A[i][j] or, equivalently, reverse the order of the crossedout entries. Here is a mask of the crossed out matrix: 1 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 """ if n < 4 or n%4: return False #only allow even squares 4n, n>0 c, cms, A = 1, n*n + 1, [[0]*n for i in range(n)] for i in range(n): for j in range(n): A[i][j] = cmsc if i%4 == j%4 or (i+j)%4 == (n1)%4 else c c += 1 return A def make_even_panmagic_square(n): """ Description and algorithm from Wikipedia https://en.wikipedia.org/wiki/Pandiagonal_magic_square A pandiagonal magic square remains pandiagonally magic not only under rotation or reflection, but also if a row or column is moved from one side of the square to the opposite side. As such, an n x n pandiagonal magic square can be regarded as having 8n^2 orientations. No (4n+2)x(4n+2) panmagic square exists. """ if n < 4 or n%4: return False #only allow 4n even squares nx = n/2 x = range(1, n+1) """ Integrating a few steps into one, compose a matrix as follows: (example for n=2 (8x8)) [1,2,3,4,8,7,6,5] [8,7,6,5,1,2,3,4] ... ... repeat pattern until full, then rotate 90 degrees """ A = (x[:nx]+x[:nx1:1], x[:nx1:1]+x[:nx]) * nx B = rotate_ccw(A) """ Build the final square by multiplying the B square by n, adding the A square and subtract n from each cell. Example: A + n x B  n """ return [[a + n*b  n for a, b in zip(r1, r2)] for r1, r2 in zip(A, B)] def make_odd_panmagic_square(n): #order 6n +/ 1: 5, 7, 11, 13, 17, 19, 23, ... if n > 4 and (n%6 == 1 or n%6 == 5): A = [[(j*2 + i) % n + 1 for j in range(n)] for i in range(n)] #order 6n + 3: 9, 15, 21, 27, ... elif n > 8 and n%6 == 3: q, dir = n / 3, 1 B = [[1,2,3], [5,6,4], [9,7,8]] for i in range(10, n, 3): B.append(range(i, i+3)[::dir]) dir = dir A = [[0]*n for i in range(n)] for i in range(q): for j in range(3): t = B[i % q][j % 3] for k in range(n): A[(i+k) % n][(j + 3*k) % n] = t else: return False T = transpose(A) return [[a + n*b  n for a, b in zip(r1, r2)] for r1, r2 in zip(A, T)] def check_magic_square(matrix, seq=True, mc=0): """ seq: Boolean used to restrict the square to having distinct numbers from 1..n^2 mc: Integer to specify a magic constant A (normal) magic square is a square array(1) of numbers consisting of the distinct positive integers 1, 2, ..., n^2(2) arranged such that the sum of the n numbers in any horizontal(3), vertical(4), or main diagonal(5) line is always the same number (Kraitchik 1942, p. 142; Andrews 1960, p. 1; Gardner 1961, p. 130; Madachy 1979, p. 84; Benson and Jacoby 1981, p. 3; Ball and Coxeter 1987, p. 193), known as the magic constant(6) A square that fails to be magic only because one or both of the main diagonal sums do not equal the magic constant is called a semimagic square(5) It is an unsolved problem to determine the number of magic squares of an arbitrary order, but the number of distinct magic squares (excluding those obtained by rotation and reflection) of order n=1, 2, 3, ... are 1, 0, 1, 880, 275305224, ... (Sloane's A006052; Madachy 1979, p. 87).(6) """ global err_message #(6) check for invalid size and shape (nonsquare) n = square_size(matrix) if (n < 1 or n == 2): err_message = "The array must be square and can't be <1 or 2." return False #(1) quick check for a square array if (not isinstance(matrix[0], (list, tuple)) or any(len(row) != n for row in matrix)): err_message = "Must be a square, i.e. the same number of rows and columns." return False #(6) calculate the magic constant and working variables if not mc: mc = n * (n*n + 1) / 2 d1, d2, r, c, a = 0, 0, [0]*n, [0]*n, set() for i in range(n): d1, d2 = d1+matrix[i][i], d2+matrix[i][ni1] for j in range(n): r[i] += matrix[i][j] c[j] += matrix[i][j] a.add(matrix[i][j]) #(2) check distinct positive integers 1, 2, ..., n^2 if seq == True and not all(x in a for x in range(1, n*n+1)): err_message = "The numbers 1 through " + str(n*n) + \ " must appear once and only once in the array." return False #(3) check that all horizontal lines equal the magic constant row_sum = set(r) if (len(row_sum) != 1 or mc not in row_sum): err_message = "All the rows didn't add to the magic constant, " + str(mc) return False #(4) check that all vertical lines equal the magic constant col_sum = set(c) if (len(col_sum) != 1 or mc not in col_sum): err_message = "All the columns didn't add to the magic constant, " + str(mc) return False #(5) check that both diagonal lines equal the magic constant if (d1 != mc or d2 != mc): err_message = "One or both diagonals didn't add to the magic constant, " \ + str(mc) + "\nThis is a semimagic square." return False return True def check_panmagic_square(A): """ If all diagonals (including those obtained by wrapping around) of a magic square sum to the magic constant, the square is said to be a panmagic square (also called a diabolic square or pandiagonal square). There are no panmagic squares of order 3 or 4n+2. """ n = square_size(A) if n < 1 or n == 3 or (n2)%4 == 0: return False if check_magic_square(A): #make sure A is magic before panmagic test mc = n * (n*n + 1) / 2 #magic constant for i in range(n): s1 = sum(A[(ij) % n][j] for j in range(n)) s2 = sum(A[(i+j) % n][j] for j in range(n)) if s1 != mc or s2 != mc: return False return True return False def check_associative_square(A): """ Reference: https://en.wikipedia.org/wiki/Associative_magic_square An associative magic square is a magic square for which every pair of numbers symmetrically opposite to the center sum up to the same value. There are no associative magic squares of singlyeven order. """ n = square_size(A) if n < 1 or n%4 != 0 and n%2 == 0: return False if check_magic_square(A): #make sure A is magic before associative test amc = n*n + 1 #associative magic constant odd_square = n % 2 if all(A[i][j] + A[ni1][nj1] == amc for i in range(n) for j in range(n/2 + odd_square)): return True return False def check_bimagic_square(A): """ If replacing each number in A with its square produces another magic square, then the square is said to be a bimagic square (or doubly magic square). """ if check_magic_square(A): #make sure A is magic before bimagic test n = len(A) np = n*n mc = n * (np + 1) * (2*np + 1) / 6 #magic constant if check_magic_square([[x*x for x in row] for row in A], False, mc): return True return False """ Basic matrix operations in native Python. Most of the time we simulate these operations by accessing the matrix in a different order. """ def square_size(A): try: n = len(A) except: return 0 if (not isinstance(A[0], (list, tuple)) or any(len(row) != n for row in A)): return 0 return n def transpose(A): """ reflect A over its main diagonal (which runs from topleft to bottomright) Example: 1, 2, 3 1, 4, 7 4, 5, 6 => 2, 5, 8 7, 8, 9 3, 6, 9 """ return [list(a) for a in zip(*A)] def rotate_cw(A): """ rotatet A clockwise (rightturn, 90 degrees) Example: 1, 2, 3 7, 4, 1 4, 5, 6 => 8, 5, 2 7, 8, 9 9, 6, 3 """ return [list(a) for a in zip(*A[::1])] def rotate_ccw(A): """ rotate A counterclockwise (leftturn, 90 degrees) Example: 1, 2, 3 3, 6, 9 4, 5, 6 => 2, 5, 8 7, 8, 9 1, 4, 7 """ return [list(a) for a in zip(*A)[::1]] def flip(matrix): """ flip A over horizontally Example: 1, 2, 3 7, 8, 9 4, 5, 6 => 4, 5, 6 7, 8, 9 1, 2, 3 """ return matrix[::1] def sc_add(A, n): """ Scalar add n to matrix A """ return [[x+n for x in row] for row in A] def print_matrix(matrix): """ Source: David Robinson from http://stackoverflow.com/questions/8747500/pythonsprettyprintingofmatrix """ max_lens = [max([len(str(r[i])) for r in matrix]) for i in range(len(matrix[0]))] print "\n".join(["".join([string.rjust(str(e), l + 2) for e, l in zip(r, max_lens)]) for r in matrix]) def demo(A): if check_magic_square(A): print_matrix(A) print "this is a magic square" else: print err_message return False if check_panmagic_square(A): print "**panmagic" if check_associative_square(A): print "**associative" if check_bimagic_square(A): print "**bimagic" print def swap_col(A, n, m): l = len(A) for i in range(l): A[i][n], A[i][m] = A[i][m], A[i][n] return A def swap_row(A, n, m): l = len(A) for i in range(l): A[n][i], A[m][i] = A[m][i], A[n][i] return A """ n = 4*1 print "4n (doubly even) pandiagonal or panmagic square, n=",n/4 M = make_even_panmagic_square(n) for x in range(1): demo(M) N = rotate_cw(M) demo(rotate_cw(M)) demo(rotate_cw(rotate_cw(M))) demo(rotate_ccw(M)) demo(flip(M)) demo(flip(rotate_ccw(M))) demo(flip(rotate_cw(M))) demo(flip(rotate_cw(rotate_cw(M)))) M=swap_row(N, 0,1) M=swap_row(M, 2,3) """ n = 7 print "Odd magic square, n=",n M = make_magic_square(n) demo(M) print "Odd panmagic square, 6n+1, 6n+5" n = 7 M=make_panmagic_square(n) demo(M) n = 11 M=make_panmagic_square(n) demo(M) print "Odd panmagic square, 6n+3" n = 9 M=make_panmagic_square(n) demo(M) n = 4*2 print "4n (doubly even), magic square, n=",n/4 M = make_magic_square(n) demo(M) n = 4*2 print "4n (doubly even) panmagic square, n=",n/4 M = make_panmagic_square(n) demo(M) print "8 x 8 bimagic square test check" T =[[56, 34, 8, 57, 18, 47, 9, 31], [33, 20, 54, 48, 7, 29, 59, 10], [26, 43, 13, 23, 64, 38, 4, 49], [19, 5, 35, 30, 53, 12, 46, 60], [15, 25, 63, 2, 41, 24, 50, 40], [ 6, 55, 17, 11, 36, 58, 32, 45], [61, 16, 42, 52, 27, 1, 39, 22], [44, 62, 28, 37, 14, 51, 21, 3]] demo(T) n=4*2 + 2 print "Even, 4n + 2, magic square, n=",(n2)/4 M = make_magic_square(n) demo(M) print "Even, 4n, square associative test" T = [[2,13,16,3], [11,8,5,10], [7,12,9,6], [14,1,4,15]] demo(T) print "Odd square associative test" T = [[25,22,5,10,3], [7,12,11,17,18], [2,6,13,20,24], [8,9,15,14,19], [23,16,21,4,1]] demo(T) 
The post Panmagic (Pandiagonal) and magic square generation, testing appeared first on Dreamshire.
]]>The post Guess That Thingy iPhone / iPad Puzzle Game Answers appeared first on Dreamshire.
]]>For the Places Pack, here are the answers to all fifty.
WHITE HOUSE

PARIS

GRAND CANYON

ST LOUIS

BRAZIL

NEW YORK

GREECE

CHINA

VENICE

SYDNEY

LONDON

TOKYO

PISA TOWER

CANADA

GOLDEN GATE

NEW ORLEANS

AYERS ROCK

MOUNT RUSHMORE

FRANCE

DEATH VALLEY

GIZA

HOOVER DAM

SOUTH AFRICA

JORDAN

MEXICO – NOT A CITY

UNITED STATES

CASA MILA

GERMANY

EMPIRE STATE

JAKARTA TOWER (MONAS)

BELIZE

INDIA

DUBAI

RUSSIA

MALAYSIA

TAIPEI

MOUNT MCKINLEY BADLY CROPPED

DEAD SEA

BRYCE CANYON

VIENNA

SEOUL

NIAGARA FALLS

HUNGARY

GENEVA

SPAIN

BUENOS AIRES

LOS ANGELES

SWISS ALPS

HONOLULU

HOUSTON

The post Guess That Thingy iPhone / iPad Puzzle Game Answers appeared first on Dreamshire.
]]>The post Hollow Words Answers and Solutions – iPhone App appeared first on Dreamshire.
]]>Here are the Hollow Words answers for the popular iPhone/iPad game created by James Porter, who is known for other games like Brain Chain and What’s the Movie. These types of puzzles have been around for centuries. They are a type of rebus and have been taught in schools to enhance word association. Hollow Words is a fun and easy wordplay game with a few obtuse puzzles that require some study.
In Hollow Words you see a visual representation of popular phrases and select letters from a set to complete the puzzle. You can, for a virtual price, have nonessential letters removed, get a clue or buy the answer.
Here are the Hollow Words answers to all the rebus puzzle sets 18.
Tip: One tip that seems to help solve a lot of puzzles without having to use a clue is to determine one word you’re sure is in the puzzle and place it anywhere by tapping on the letters. The intention is to remove letters from the bottom letter tray and study which letters remain. You can usually unscramble these letters to find a missing word or phrase. After you figured out the answer, recall all the letters and place them in the proper order.
Tip: If you’re really stuck, you can use the remove letters clue to remove all nonessential letters and run the remainder through an anagram finder such as the Internet Anagram Server
Source: Hollow Words answers by C0smicPenguin
An example of an old rebus that uses pictograms as well as English words to encrypt a message.
New York Clipper, August 21, 1869, p. 159. “Answer to rebus.”
Yankee Robinson’s
Consolidated Show, largest on earth
over 400 men and horses
will be at every principal place in New York
anybody who decyphers this rebus
will receive a ticket by forking over 50 cts.
The post Hollow Words Answers and Solutions – iPhone App appeared first on Dreamshire.
]]>The post Project Euler 96 appeared first on Dreamshire.
]]>Su Doku (Japanese meaning number place) is the name given to a popular puzzle concept. Its origin is unclear, but credit must be attributed to Leonhard Euler who invented a similar, and much more difficult, puzzle idea called Latin Squares. The objective of Su Doku puzzles, however, is to replace the blanks (or zeros) in a 9 by 9 grid in such that each row, column, and 3 by 3 box contains each of the digits 1 to 9. Below is an example of a typical starting puzzle grid and its solution grid.


A well constructed Su Doku puzzle has a unique solution and can be solved by logic, although it may be necessary to employ "guess and test" methods in order to eliminate options (there is much contested opinion over this). The complexity of the search determines the difficulty of the puzzle; the example above is considered easy because it can be solved by straight forward direct deduction.
The 6K text file, sudoku.txt (right click and ‘Save Link/Target As…’), contains fifty different Su Doku puzzles ranging in difficulty, but all with unique solutions (the first puzzle in the file is the example above).
By solving all fifty puzzles find the sum of the 3digit numbers found in the top left corner of each solution grid; for example, 483 is the 3digit number found in the top left corner of the solution grid above.
Read an external file and parse into required format, solve Sudoku puzzles and add the first 3 digits: All in under 30 lines of Python.
Input file:
Each puzzle was converted from it’s native form into a string of 81 integers where zeros are blank squares. Any line with the word ‘Grid’ was ignored. The supplied data has been prevalidated, so sanity checks are not performed. Here’s an example for the puzzle listed in the problem description:
Grid 01
003020600
900305001
001806400
008102900
700000008
006708200
002609500
800203009
005010300
And here is what the solver expects:
003020600900305001001806400008102900700000008006708200002609500800203009005010300
For more information on this concise solution consult:
Shortest Python Sudoku solver
Runs < 6 seconds in PyPy.
from urllib import urlopen s = 0 def same_row(i,j): return (i/9 == j/9) def same_col(i,j): return (ij) % 9 == 0 def same_block(i,j): return (i/27 == j/27 and (i % 9)/3 == (j % 9)/3) def r(a): global s i = a.find('0') if i == 1: s+=int(a[0:3]) excluded_numbers = set() for j in range(81): if same_row(i,j) or same_col(i,j) or same_block(i,j): excluded_numbers.add(a[j]) for m in '123456789': if m not in excluded_numbers: r(a[:i]+m+a[i+1:]) file = urlopen('http://projecteuler.net/project/sudoku.txt','r').readlines() fx = ''.join([line[:9] for line in file if not 'Grid' in line]) fx = [fx[i:(i+81)] for i in range(0,len(fx),81)] [r(p) for p in fx] print "Answer to PE96 =", s 
grid 13 took the longest.
Using PyPy instead of Python27 saved 85 seconds (18 times faster)
50 lines of 81 integer starting grids: pe96.prob
50 lines of 81 integer solutions: pe96.sol
The post Project Euler 96 appeared first on Dreamshire.
]]>The post Project Euler 90 appeared first on Dreamshire.
]]>Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes sidebyside in different positions we can form a variety of 2digit numbers.
For example, the square number 64 could be formed:
In fact, by carefully choosing the digits on both cubes it is possible to display all of the square numbers below onehundred: 01, 04, 09, 16, 25, 36, 49, 64, and 81.
For example, one way this can be achieved is by placing {0, 5, 6, 7, 8, 9} on one cube and {1, 2, 3, 4, 8, 9} on the other cube.
However, for this problem we shall allow the 6 or 9 to be turned upsidedown so that an arrangement like {0, 5, 6, 7, 8, 9} and {1, 2, 3, 4, 6, 7} allows for all nine square numbers to be displayed; otherwise it would be impossible to obtain 09.
In determining a distinct arrangement we are interested in the digits on each cube, not the order.
{1, 2, 3, 4, 5, 6} is equivalent to {3, 6, 4, 1, 2, 5}
{1, 2, 3, 4, 5, 6} is distinct from {1, 2, 3, 4, 5, 9}
But because we are allowing 6 and 9 to be reversed, the two distinct sets in the last example both represent the extended set {1, 2, 3, 4, 5, 6, 9} for the purpose of forming 2digit numbers.
How many distinct arrangements of the two cubes allow for all of the square numbers to be displayed?
Let’s reword this problem a bit:
Take two cubes and inscribe each side of each cube with six numbers from the set {09} in such a way that when they are placed sidebyside they show every perfect square from the set {01, 04, 09, 16, 25, 36, 49, 64, 81}.
As an example, one possibility could be {0, 5, 6, 7, 8, 9} on one cube and {1, 2, 3, 4, 8, 9} on the other. The cubes can be positioned in any way that forms the perfect square. Also, a cube with the combination {9, 8, 7, 0, 5, 6} is the same as (NOT distinct from) another cube with the same numbers but in a different order {0, 5, 6, 7, 8, 9}.
To make the problem a bit more challenging, let’s say that you can turn a cube upside down so the 6 could serve as a 9. This makes the numbers to inscribe on the cube as {0, 1, 2, 3, 4, 5, 6, 7, 8, 6}.
How many distinct arrangements of the two cubes allow for all of the square numbers to be displayed?
To solve this problem we are going to use:
Stepbystep analysis:
Firstly, we create a list of desired outcomes that include (0,1), (0,4), (0,6)^{*}, (1,6), (2,5), (3,6), (4,6)^{*}, and (8,1). Note that (6,4) is missing because the cubes can be set sidebyside in either way so (4,6) is the same as (6,4).
^{*}(the 6 can be flipped for a 9)
Secondly, we build a list of tuples (which didn’t need to be tuples, but they looked cleaner in source) for all the possibilities of 10 numbers chosen 6 at a time. This is _{10}C_{6} or 210 possible combinations.
As this may be confusing, here is a simplified version to look at using 4 numbers chosen 2 at a time (_{4}C_{2} or 6 possible combinations):
>>> from itertools import combinations >>> cube = list(combinations([0,1,2,3], 2)) >>> cube [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] >>> 
Notice that the combination function produces a list of tuples for each iteration.
We’ll hold off on the valid function for a moment and focus on the double list compression.
The enumerate function is a cleaner and more efficient way of replacing:
for n in range(len(my_list)): print n, my_list[n] 
with:
for n, item in enumerate(my_list): print n, item 
So, enumerate returns both the index and the element. This is useful for comparing every item in a list to every other item in the list. Here’s an example continuing from above:
for i,c1 in enumerate(cube): print 'first loop',i,c1 for c2 in cube[:i]: print " second loop", i,c2 
Output:
first loop 0 (0, 1) first loop 1 (0, 2) second loop 1 (0, 1) first loop 2 (0, 3) second loop 2 (0, 1) second loop 2 (0, 2) first loop 3 (1, 2) second loop 3 (0, 1) second loop 3 (0, 2) second loop 3 (0, 3) first loop 4 (1, 3) second loop 4 (0, 1) second loop 4 (0, 2) second loop 4 (0, 3) second loop 4 (1, 2) first loop 5 (2, 3) second loop 5 (0, 1) second loop 5 (0, 2) second loop 5 (0, 3) second loop 5 (1, 2) second loop 5 (1, 3)
Thirdly, the valid function simply returns True for those cases that a square can be made from a perspective set of two cubes in either direction. The all function will return True only if all the generated list items are True.
Lastly, all that’s left to do is count valid combinations. Duplicates are ignored inherently by only checking a possible cube against all other combinations that come before it. That’s why we use cube[:i] in the second loop and not just cube. Which is also why enumerate is so nice, because it gives us the index, i.
Runs < 1 second in Python.
from itertools import combinations squares = [(0,1), (0,4), (0,6), (1,6), (2,5), (3,6), (4,6), (8,1)] cube = list(combinations([0,1,2,3,4,5,6,7,8,6], 6)) def valid(c1, c2): return all(x in c1 and y in c2 or x in c2 and y in c1 for x, y in squares) print "Answer to PE90 =", sum(1 for i,c1 in enumerate(cube) for c2 in cube[:i] if valid(c1, c2)) 
As an exercise, one may ask how many combinations are there if the 6/9 reversal was NOT allowed, but instead, just the digits {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}?
By changing these two lines:
squares = [(0,1), (0,4), (0,9), (1,6), (2,5), (3,6), (4,9), (6,4), (8,1)] dice = list(combinations([0,1,2,3,4,5,6,7,8,9], 6)) 
the answer would be: 205
The post Project Euler 90 appeared first on Dreamshire.
]]>