I thought this was kinda cool!  Here's my solution, its not robust or
probably efficient....

I'd to hear improvements or other solutions!

Justin


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)



On Fri, Apr 13, 2012 at 2:34 PM, Ted Harding <Ted.Harding@wlandres.net>wrote:

> Greetings all!
> A recent news item got me thinking that a problem stated
> therein could provide a teasing little exercise in R
> programming.
>
> http://www.bbc.co.uk/news/uk-england-cambridgeshire-17680326
>
>  Cambridge University hosts first European 'maths Olympiad'
>  for girls
>
>  The first European girls-only "mathematical Olympiad"
>  competition is being hosted by Cambridge University.
>  [...]
>  Olympiad co-director, Dr Ceri Fiddes, said competition questions
>  encouraged "clever thinking rather than regurgitating a taught
>  syllabus".
>  [...]
>  "A lot of Olympiad questions in the competition are about
>  proving things," Dr Fiddes said.
>
>  "If you have a puzzle, it's not good enough to give one answer.
>  You have to prove that it's the only possible answer."
>  [...]
>  "In the Olympiad it's about starting with a problem that anybody
>  could understand, then coming up with that clever idea that
>  enables you to solve it," she said.
>
>  "For example, 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?"
>
> Well, that's the challenge: Write (from scratch) an R program
> that solves this problem. And make it neat.
>
> NOTE: If there should happen to be some R package that can solve
> this kind of problem already, without you having to think much,
> then its use is illegitimate! (I.e. will be deemed "regurgitation").
>
> Over to you.
>
> With best wishes,
> Ted.
>
> -------------------------------------------------
> E-Mail: (Ted Harding) <Ted.Harding@wlandres.net>
> Date: 13-Apr-2012  Time: 22:33:43
> This message was sent by XFMail
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]

