Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering 1:17 by perfect square pairs

Tags:

r

There was an interesting question on R-help:

"Take the numbers one up to 17. Can you write them out in a line so that every pair of numbers that are next to each other, adds up to give a square number?"

My solution is below and not particularly special. I'm curious about a more elegant and/or robust solution. Maybe a solution that can take an arbitrary string of numbers and order them like this if possible?

sq.test <- function(a, b) {
  ## test for number pairs that sum to squares.
  sqrt(sum(a, b)) == floor(sqrt(sum(a, b)))
}

ok.pairs <- function(n, vec) {
  ## given n as a member of vec,
  ## which other members of vec satisfiy sq.test
  vec <- vec[vec!=n]
  vec[sapply(vec, sq.test, b=n)]
}

grow.seq <- function(y) {
  ## given a starting point (y) and a pairs list (pl)
  ## grow the squaring sequence.
  ly <- length(y)
  if(ly == y[1]) return(y)

  ## this line is the one that breaks down on other number sets...
  y <- c(y, max(pl[[y[ly]]][!pl[[y[ly]]] %in% y]))
  y <- grow.seq(y)

  return(y)
}


## start vector
x <- 1:17

## get list of possible pairs
pl <- lapply(x, ok.pairs, vec=x)

## pick start at max since few combinations there.
y <- max(x)
grow.seq(y)
like image 777
Justin Avatar asked Apr 14 '12 01:04

Justin


People also ask

How do you find the perfect square of a number?

To check the perfectness of your square, you can simply calculate the square root of a given number. If the square root is an integer, your number is the perfect square. Let's calculate the squares of the following numbers: 49 and 53 . √49 = 7 - 7 is an integer → number 49 is a perfect square.

What are pair numbers?

A set of two numbers or objects linked in some way is said to be a pair. The pair and is usually denoted ( , ), and is generally considered to be ordered, making it a 2-tuple. In certain circumstances, pairs are also called brothers or twins.

How many perfect squares are there between 1 and N?

They are 4, 9, 16, 25, 36, 49, 64 and 81. However, there are ten perfect squares from 1 to 10. They are 1, 4, 9, 16, 25, 36, 49, 64, 81 and 100.

How many perfect squares can below 500?

Hence, the perfect squares between 1 and 500 are 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441 and 484.


1 Answers

You can use outer to compute the allowable pairs. The resulting matrix is the adjacency matrix of a graph, and you just want a Hamiltonian path on it.

# Allowable pairs form a graph
p <- outer(
  1:17, 1:17, 
  function(u,v) round(sqrt(u + v),6) == floor(sqrt(u+v)) ) 
)
rownames(p) <- colnames(p) <- 1:17
image(p, col=c(0,1)) 

# Read the solution on the plot
library(igraph)
g <- graph.adjacency(p, "undirected")
V(g)$label <- V(g)$name 
plot(g, layout=layout.fruchterman.reingold)

Hamiltonian path

like image 81
Vincent Zoonekynd Avatar answered Oct 26 '22 13:10

Vincent Zoonekynd