Sunday, September 22, 2013

Problem 45 - R

Believe it or not, there are still real programming languages that I have yet to use, and among them was R, a language specifically designed for mathematical/statistical analysis, and thus a language that is pretty well suited to Project Euler. Didn't have too many problems with this one - the biggest issue was having to realize that a "double" is a double-precision integer (like a long in C), because I needed those for this.
hex <- function(n) n*(2*n-1)

isPent <- function(p) {
    root <- as.double(sqrt(24*p + 1))
    (root * root == 24*p + 1) && ((root + 1) %% 6 == 0)
}

for (i in 144:30000) {
    h <- hex(i)
    if (isPent(h)) {
        print(h)
        break
    }
}

No comments:

Post a Comment