Saturday, December 24, 2022
HomePuzzlesarithmetic - How Many Squares on the Peg Solitaire

# arithmetic – How Many Squares on the Peg Solitaire

For reference, let the pegs be numbered as proven under.

``````      00 01 02
03 04 05
06 07 08 09 10 11 12
13 14 15    16 17 18
19 20 21 22 23 24 25
26 27 28
29 30 31
``````

Initially, I neglected the presence of the {3 11 20 28} sq. in M Oehm’s reply, so assuming some squares had been neglected, determined to put in writing a program to seek out all of them. To my shock, I acquired precisely the identical variety of squares as in that reply; however upon studying it once more, noticed the {3 11 20 28} sq. listed pretty clearly within the (3,1) class. Anyhow, right here is a few brute-force python code that takes about 1/16 second to listing all of the squares.

``````#!/usr/bin/env python
# Re: https://puzzling.stackexchange.com/questions/51773/how-many-squares-on-the-peg-solitaire
# In this program, given a structure of pegs, we depend the variety of
# squares fashioned through the use of any 4 of these pegs as corners.
import time

# List the x, y values of all 32 pegs, treating one of many corners as
# 0,0 and one other nook as (6,6).
pegX = [2,3,4, 2,3,4, 0,1,2,3,4,5,6, 0,1,2,4,5,6, 0,1,2,3,4,5,6, 2,3,4, 2,3,4]
pegY = [0,0,0, 1,1,1, 2,2,2,2,2,2,2, 3,3,3,3,3,3, 4,4,4,4,4,4,4, 5,5,5, 6,6,6]
nPeg = len(pegX)

def dist2(m,n):
return (pegX[m]-pegX[n])**2 + (pegY[m]-pegY[n])**2

# Test if factors p,q,r,s make one nook of a sq.
def stest(p,q,r,s):
dq = dist2(p,q)
dr = dist2(p,r)
ds = dist2(p,s)
dhi = max(dq, dr, ds)
dlo = min(dq, dr, ds)
if (dhi != 2*dlo): return False
nlo = (dq==dlo) + (dr==dlo) + (ds==dlo)
if nlo != 2: return False
return True

# Cycle by all p,q,r,s combos, sustaining p<q<r<s
p, q, r, s = 0, 1, 2, 2        # Initial p,q,r,s tuple, -1
nSqu = 0
t0 = time.time()
whereas 1:
s += 1
if s >= nPeg:
r += 1
if r >= nPeg-1:
q += 1
if q >= nPeg-2:
p += 1
if p >= nPeg-3:
break
q = p+1
r = q+1
s = r+1
# If three corners work, the fourth is compelled
if stest(p,q,r,s) and stest(q,p,r,s) and stest(r,p,q,s):
nSqu += 1
print 'S#{:3}  {:3}{:3}{:3}{:3}'.format(nSqu, p,q,r,s)

t1 = time.time()
print 'Elapsed seconds: ', t1-t0
``````

RELATED ARTICLES