Problem 45

Problem 45 - Project Euler
六角数なら三角数だから…とかやって計算量を減らすのかと思ったけど,減らしたところで0.5秒短くなるだけだった.

is.tri <- function(t){
  2*t == floor(sqrt(2*t))*(floor(sqrt(2*t))+1)
}
is.pen <- function(p){
  2*p == ceiling(sqrt(2/3*p))*(3*ceiling(sqrt(2/3*p))-1)
}
is.hex <- function(h){
  h/2 == ceiling(sqrt(h/2))*(ceiling(sqrt(h/2))-1/2)
}
hex <- function(n) n*(2*n-1)

cand <- hex(144)
n <- 144
f <- function(){
while(!(is.tri(cand) && is.pen(cand) && is.hex(cand))){
  cand <- cand + 4*n + 1
  n <- n+1
}