Can you arrange numbers 1 to 16 in a circle such that the sum of adjacent two numbers is a perfect square? If yes, how and if not, why not? Can you write a program to solve this problem?
+7
A:
Hint for you. Using this set of numbers, there is only one possible sum which includes 16 that makes a perfect square (16+9 = 25), so the answer is no.
James McLeod
2010-08-30 03:24:24
Errm.... 1+3, 4+5, 4+12, 1+8....
Andrew Shepherd
2010-08-30 03:26:55
Yeah, I know - I forgot the key point about including 16...
James McLeod
2010-08-30 03:28:39
I think what James meant was that since it's a circle of numbers, any number in the circle will have to make a perfect square with the number to the left AND to the right of it. Effectively, each number will be used twice in addition. But 16 only has one possible combination to make a perfect square, and that's (16+9). Therefore, there's no possible combination that completes the circle. (Even Forrest's answer above is incorrect in this respect).
Richard Neil Ilagan
2010-08-30 03:30:49
@James: Oh, sorry, I get it now. Because it's a circle, sixteen would be adjacent to two numbers, and therefore it would have to be included in two sums. And you've pointed out that it can only be in one sum, so therefore the answer is no. Ah-ha.
Andrew Shepherd
2010-08-30 03:32:45
But let's claim I was being deliberately obscure with the hint so I wouldn't take all the fun out of the problem.
James McLeod
2010-08-30 03:33:36
+3
A:
[1, 15, 10, 6, 3, 13, 12, 4, 5, 11, 14, 2, 7, 9, 16]
And yes, I can write a program to solve it.
EDIT: Just realised this isn't a circle ... this is the only linear solution, so my answer is:
No, not possible.
Forrest
2010-08-30 03:26:51
A circle has no beginning and no end. Starting this sequence at, say, the 12, and you run into 16 + 1 = 17.
James McLeod
2010-08-30 03:31:35
Forrest, this arrangement is not a circle, never the less if we were to write a program for this what would be the algorithm (apart from hit and trial
techbeast
2010-08-30 03:35:16
Starting with any of the numbers (I started with 1), enumerate all the possibilities for the next number (range from 1 to 16 filtered with whether it makes a perfect square if summed with the last element), and recursively try each of these.
Forrest
2010-08-30 03:56:42
+1
A:
This might be wrong, but I can't seem to find possible cycles with this code:
/**
* @author BjørnS
* @created 30. aug. 2010
*/
public class PerfectSquares {
/**
* @param args
*/
public static void main(String[] args) {
List<Integer> options = Lists.newArrayList();
for (int i = 1; i <= 16; i++) {
options.add(i);
}
List<Integer> start = start(options);
if (start == null) {
System.out.println("Unsolvable unless this code is wrong.");
} else {
System.out.println("My answer is: " + start);
}
}
private static List<Integer> start(List<Integer> options) {
for (Integer i : options) {
List<Integer> li = Lists.newArrayList(options);
li.remove(i);
List<Integer> ws = Lists.newArrayList(i);
List<Integer> answer = findAnswer(ws, li);
if (answer != null) {
return answer;
}
ws = null;
li = null;
}
return null;
}
private static List<Integer> findAnswer(List<Integer> workingSet, List<Integer> options) {
Integer last = workingSet.get(workingSet.size() - 1);
if (options.size() == 1) {
Integer first = workingSet.get(0);
Integer option = options.get(0);
if (isPerfectSquare(first, option) && isPerfectSquare(last, option)) {
workingSet.add(option);
System.out.println("I think it is:" + workingSet);
return workingSet;
}
return null;
}
for (Integer i : options) {
if (isPerfectSquare(last, i)) {
List<Integer> li = Lists.newArrayList(options);
li.remove(i);
List<Integer> ws = Lists.newArrayList(workingSet);
ws.add(i);
System.out.println("trying " + ws);
List<Integer> answer = findAnswer(ws, li);
if (answer != null) {
System.out.println("Potential answer:" + answer);
return ws;
}
li = null;
ws = null;
}
}
return null;
}
private static boolean isPerfectSquare(Integer a, Integer b) {
return Math.pow(Math.floor(Math.sqrt(a + b)), 2) == (a + b);
}
}
BjornS
2010-08-30 11:33:32
A:
If you consider the graph with vertices 1, 2, ..., 16, and edges (a, b) if a + b is square, then the problem is the equivalent to finding a hamiltonian path on this graph.
Finding hamiltonian paths is in general NP, which suggests you're not going to do better than a search.
Paul Hankin
2010-08-31 05:07:30