 Home More Samples
```
///////////////////////////////////////////////////////////////////////////////
// Magic circle
///////////////////////////////////////////////////////////////////////////////
// Enigma 1417 Bob Walker, New Scientist magazine, November 11, 2006.
///////////////////////////////////////////////////////////////////////////////
//
//         0
//       1   2
//     8       7
//     4       5
//       6   3
//         9
//
// Joe placed 10 numbered counters in a circle (as shown) and asked Penny to
// pick a number. Then he asked her to count clockwise that number of counters
// to find a new counter. For example, if she picked 7 the new counter would
// be 1. Repeating the process with the number shown on each new counter she
// would finish up at zero.
//
// But if she picked one particular number first she would reach zero via
// all the other counters. Penny found that leaving some of the counters in
// place, including 0, 1 and 2, there was another arrangement of the counters
// that had this same property if she started at the same number.
//
// Beginning with zero, what was the clockwise order of Penny's numbers?
//
//
///////////////////////////////////////////////////////////////////////////////
//
// To solve the problem, run the code below by issuing the query:
//
//      all MagicCircle(counter_1,sequence1,counter_2,sequence2)
//
//
///////////////////////////////////////////////////////////////////////////////
//
// Solution:
//
// counter_1 = [0,2,7,5,3,9,6,4,8,1]
// sequence1 = [9,3,4,2,5,8,6,7,1,0]
// counter_2 = [0,2,6,4,7,9,3,5,8,1]
// sequence2 = [9,7,2,4,5,6,8,3,1,0]
// ___ Solution: 1 ___ [00:00:00] __ [Backtracks: 4847] ____
//
// Number of solutions: 1   Number of backtracks: 5254
// Elapsed time: 00:00:00
//
///////////////////////////////////////////////////////////////////////////////

local Arr10 = [0..9]->>[0..9]  //Array of 10 different integers 0..9

pred MagicCircle(ac::Arr10,as::Arr10,bc::Arr10,bs::Arr10) iff
ac = [0,2,7,5,3,9,6,4,8,1] &

// Given the array "ac", calculate the order "as" of the
// numbers as they were picked.
SingleStepAll(ac,as) &

// Create different arrangement, preserving the numbers 0,2,1
bc = [0,2,_,_,_,_,_,_,_,1] & bc <> ac &

// Penny picked the number "as(0)" first,
bs(0) = as(0) &

// Given the array "bc", calculate the order "bs" of the
// numbers as they were picked.
SingleStepAll(bc,bs)

///////////////////////////////////////////////////////////////
// Given an array "a" of numbers 0..9,
// return in "as" the order all ten numbers were picked were
// picked in 10 steps, with the last one being the number 0
local pred SingleStepAll(a::Arr10,as::Arr10) iff
a(x0) = y0 & x1 = (x0 + y0) mod 10 &
a(x1) = y1 & x2 = (x1 + y1) mod 10 &
a(x2) = y2 & x3 = (x2 + y2) mod 10 &
a(x3) = y3 & x4 = (x3 + y3) mod 10 &
a(x4) = y4 & x5 = (x4 + y4) mod 10 &
a(x5) = y5 & x6 = (x5 + y5) mod 10 &
a(x6) = y6 & x7 = (x6 + y6) mod 10 &
a(x7) = y7 & x8 = (x7 + y7) mod 10 &
a(x8) = y8 & x9 = (x8 + y8) mod 10 &
a(x9) = 0 & // the last number reached is zero

// This is the order we picked the numbers
as = [y0,y1,y2,y3,y4,y5,y6,y7,y8,0]

```