A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
A brute-force approach:
py_triplet <- function(m, n) {
a <- m^2- n^2
b <- 2 * m * n
c <- m^2 + n^2
c(a, b, c)
}
dtf <- expand.grid(v1 = 1:20, v2 = 1:20)
triples <- with(dtf, mapply(py_triplet, v1, v2))
idx <- which(apply(triples, 2, sum) == 1000)
prod(triples[, idx])
## [1] 31875000