Skip to content

Instantly share code, notes, and snippets.

@Koitaro
Last active October 8, 2015 11:18
Show Gist options
  • Select an option

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

Select an option

Save Koitaro/3324082 to your computer and use it in GitHub Desktop.
R: Project Euler 20-29
library(gmp)
n <- factorialZ(100)
sum(n %/% pow.bigz(10, (floor(log10(n)):0)) %% 10)
max <- 10000
x <- rep(1, max)
for (i in 2:(max/2)) {
j <- 2:(max/i) * i
x[j] <- x[j] + i
}
y <- (1:max)[x[x] == 1:max]
y <- y[y != x[y]]
sum(y[!is.na(y)])
name <- sort(scan("names.txt", what="character", sep=",", na.strings = ""))
name <- strsplit(name, "")
f <- function(x, y) x + which(LETTERS %in% y)
value <- Vectorize(function(s) Reduce(f, s, 0))
sum(value(name) * 1:length(name))
max <- 28123
sumDivisors <- rep(1, max)
for (i in 2:(max/2)) {
j <- 2:(max/i) * i
sumDivisors[j] <- sumDivisors[j] + i
}
nums <- 1:max
abundants <- nums[sumDivisors > nums]
for (n in abundants) {
nums[abundants[abundants <= max-n] + n] <- 0
}
sum(nums)
n <- 1e6; d <- 0:9; answer <- 0
for (f in factorial(9:0)) {
x <- (n-1)%/%f
n <- n - x*f
answer <- 10*answer + d[x+1]
d <- d[-(x+1)]
}
answer
library(gmp)
n <- 1; limit <- pow.bigz(10,999)
while (fibnum(n) < limit) n <- n+1
n
f <- function(n) {
x <- 10^(floor(log10(n))+1)
v <- numeric(0)
repeat {
y <- x %% n
if (y == 0) return(0)
if (y %in% v) return(length(v))
v <- c(v, y)
x <- 10*y
}
}
which.max(sapply(1:999, f))
library(gmp)
a <- seq(-999, 1000, by=2)
b <- primes(1000)
f <- Vectorize(function(a, b) {
if (a+b < 1) return(0)
n <- 0
while (isprime(n^2+n*a+b)) n <- n+1
n
})
x <- outer(a, b, f)
ind <- which(x == max(x), arr.ind=TRUE)
a[ind[1]] * b[ind[2]]
n <- seq(3, 1001, by=2)
sum(4*(n^2-n+1)) + 1
library(gmp)
length(unique(as.character(as.vector(outer(2:100, 2:100, pow.bigz)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment