12,352,019 members (32,170 online)
Technical Blog
alternative version

5.9K views
4 bookmarked
Posted

# Jail Break Quiz

, 8 Aug 2012 CPOL
 Rate this:
A Java program to solve the jail break quiz.

Everything was quiet and peaceful today, until Google Reader brought to my attention this puzzle from plus.maths.org.

So, I said, why not writing a little Java program to resolve this? Here is the code:

```public class Puzzle1 {
static public final int MAX = 100;

static public void printDoors(boolean[] doorIsOpen) {
int count = 0;
for (int i = 0; i < doorIsOpen.length; i++) {
if (doorIsOpen[i]) {
System.out.print("o");
count++;
}
else System.out.print("c");
}
System.out.println(" =" + count + " open doors");
}

static public void main(String[] args) {
boolean[] doorIsOpen = new boolean[MAX];

for (int i = 1; i <= MAX; i++) {
for (int j = i; j <= MAX; j+=i) {
doorIsOpen[j-1] = !doorIsOpen[j-1];
}
printDoors(doorIsOpen);
}
}
}```

The result was "10 prisoners have escaped", as per the last line printed by the program:

`occoccccoccccccoccccccccoccccccccccoccccccccccccoccccccccccccccoccccccccccccccccocccccccccccccccccco =10 open doors`

The most interesting part of the puzzle, though, is the "why" one. Have a look at the sequence; 1st position (cell in this case) contains "o" (from "open"), 4 - "o", 9 - "o", 16 - "o", 25 - "o", 36 - "o", 49 - "o", 64 - "o", 81 - "o", 100 - "o". Basically, every number of type q2≤100 corresponds to an "o". Now, this is weird ... but not for a long time.

Let's stick with an easier example (10 rather than 100):

 o o o o o o o o o o o c o c o c o c o c o c c c o o o c c c o c c o o o o o c c o c c o c o o o c o o c c o c c o o c o o c c o c c c o c o o c c o c c c c c o o c c o c c c c o o o c c o c c c c o c
• 1st cell (column from the left) is visited one time and remains opened and unvisited after that. Also number 1 has one divisor {1}.
• 2nd cell is visited two times (one "o" and one "c") and remains closed and unvisited after that. Also number 2 has two divisors {1, 2}.
• 3rd cell is visited two times (one "o" and one "c") and remains closed and unvisited after that. Also number 3 has two divisors {1, 3}.
• 4th cell is visited three times (one "o", one "c" and one "o") and remains opened and unvisited after that. Also number 4 has three divisors {1, 2, 4}.
• ...

It turns out that the number of visits is equal to the number of divisors the number, corresponding to the cell (column from the left), has. Here is the formula and this statement is easy to prove (e.g. using induction).

• Now, if the number of divisors is even (we have a whole number of pairs {"o", "c"}), because the sequence always starts with "o" then it must end with "c".
• However, if the number of divisors is odd (we have a whole number of pairs {"o", "c"} plus one extra {"o"}), the sequence ends with "o".

So far so good, but when the number of divisors is an odd number? According to the formula mentioned above the number

τ(n)=(e1+1)⋅(e2+1)⋅(e3+1)⋅...⋅(ek+1)

is odd when each of these numbers e1, e2, e3,...,ek is even, but this means that n is of type q2. Q.E.D.

## Share

 Software Developer (Senior) Snappli Ltd. United Kingdom
My name is Ruslan Ciurca. Currently I am a Software Developer at snappli.com.

## You may also be interested in...

 First Prev Next
 Python version PySams16-Aug-12 1:15 PySams 16-Aug-12 1:15
 Last Visit: 31-Dec-99 18:00     Last Update: 27-Jun-16 1:17 Refresh 1