I found this problem on Quora. I had fun solving it in J. Are there more
efficient ways?

The first 8 natural numbers (1 to 8) are positioned on the vertices of an
octagon. Is it possible to position the numbers such that the sum of any 3
adjacent vertices is greater than 11? Greater than 13?

Using the J programming language, brute force approach:

Wikipedia: "J (programming language)"

J primitives: NuVoc - J Wiki <https://code.jsoftware.com/wiki/NuVoc>

Generate all permutations of the integers 1–8, & store them in n. Count the
permutations & display the count:

$n=.|:>:perm 8

8 40320

So n contains the 40,320 possible permutations of the integers 1–8,
representing all the possible permutations of the numbered vertices of an
octagon.

Now sum consecutive sets of three integers from each set of 8 integers with
wraparound, which will create sets of 8 sums, and store the sums in s3.
Count the sums & display the count:

$s3=.+/"1[3,\"1|:n,2{.n

40320 8

So s3 has 40,320 sets of 8 sums, where each set of 8 sums represents the 8
sums of 3 adjacent vertices on each unique octagon.

Now mark all the octagons have all their 3 consecutive vertex sums greater
than 11, sum the marks, and list the sum:

+/m=.*./"1[11<"1 s3

320

So 320 of the 40,320 possible octagons have all their 3 consecutive vertex
sums greater than 11:

Use the mark vector to extract & display the first & last few sets of
octagon vertices whose 3 consecutive vertex sums are greater than 11:

{m#|:n

│1 4 7 2 5 6 3 8│1 4 7 2 6 5 3 8│1 5 6 2 4 7 3 8│1 5 6 2 7 3 4 8│1 5 6 2 7
4 3 8│1 5 6 2 8 3 4 7│1 5 6 3 7 2 4 8│1 5 6 3 8 2 4 7│1 5 6 4 2 7 3 8│1 5 6
4 7 2 3 8│….

…..│8 3 5 6 2 7 4 1│8 3 6 4 2 7 5 1│8 3 6 5 1 7 4 2│8 3 6 5 2 7 4 1│8 3 7 2
4 6 5 1│8 3 7 4 2 6 5 1│8 4 2 7 3 5 6 1│8 4 2 7 3 6 5 1│8 4 3 7 2 5 6 1│8 4
3 7 2 6 5 1│

How many octagons have all their 3 consecutive vertex sums greater than 13?

+/*./"1[13<"1 s3

0

There are no octagons whose 3 consecutive vertex sums are all greater than
13.

How many octagons have all their 3 consecutive vertex sums greater than 10?

+/*./"1[10<"1 s3

3456

So 3,456 octagons of the 40,320 possible octagons have all their 3
consecutive vertex sums greater than 10.

Skip Cave
Cave Consulting LLC
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to