[This article was first published on geocacheR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I stumbled upon a nice little puzzle in Gloucestershire, England:

Arrange the numbers 1 to 16, so each two numbers next to each other, add up to a square number. (You can only use each number once).

Devastatingly succinct. But potentially tricky. I reached out, as I always do, to R to help narrow the possibilities:

library(tidyverse)
library(magrittr)
v <- 1:16
dafr <- crossing(a=v, b=v) %>%
filter(a < b) %>%
mutate(s = a + b) %>%
filter((a + b) %in% ((1:5)^2)) %>%
print()

Here made every combination of two different numbers, summed each pair and thrown out all the ones that don’t sum up to 1, 4, 9, 16, or 25. And there are only sixteen pairs remaining! We need fifteen, and each number must be used at most twice. Two numbers appear three times, so that is the rogue pair which can be discarded.

Furthermore, two numbers can only form a square-pair once each: 8 (which when added to 1 makes 9), and 16 (which makes 25 in combination with 9). They become our end members:

8 ... 16

And since their partners are known,

8 1 ... 9 16

Now it’s a purely mechanical task of picking out the remaining pair for each number, working towards the middle. Try it!