Studying the math, we learn that every hexagonal number is also triangular.
Therefore we only need to identify the next hexagonal number that is also pentagonal.
I have a helperfunction for that:
source("helpers.R")
is_pentagonal
## function(x){
## if(x<1) return(FALSE)
## s <- (sqrt(24*x +1)+1)/6
## s == floor(s) # We probably need some way of controlling the precision
## }
Generate hexanogals, and continue until one is pentagonal. We can start at 144.
I also have a helper function for generating hexagonals:
hexagonal
## function(x){
## 2*x^2 - x
## }
found <- FALSE
n <- 144
while(!found){
if(is_pentagonal(hexagonal(n))){
answer <- hexagonal(n)
found <- TRUE
}else{
n <- n + 1
}
}