Skip to content

Instantly share code, notes, and snippets.

@Koitaro
Created August 18, 2012 10:03
Show Gist options
  • Select an option

  • Save Koitaro/3385787 to your computer and use it in GitHub Desktop.

Select an option

Save Koitaro/3385787 to your computer and use it in GitHub Desktop.
R: Project Euler 60-69
library(gmp)
library(hash)
h <- hash()
n <- 1
maxDigits <- Inf
repeat {
v <- as.character(pow.bigz(n, 3))
s <- strsplit(v, "")[[1]]
k <- paste(sort(s), collapse="")
if (length(s) > maxDigits) break
if (length(h[[k]]) == 4) maxDigits <- length(s)
h[[k]] <- c(h[[k]], v)
n <- n + 1
}
answer <- numeric(0)
for (v in values(h)) {
if (length(v) == 5) answer <- append(answer, as.bigz(v[1]))
}
min(answer)
f <- function(m) {
n <- 1; answer <- 0
while (floor(log10(m^n)) + 1 == n) {answer <- answer + 1; n <- n + 1}
answer
}
sum(sapply(1:9, f))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment