Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save Koitaro/3318488 to your computer and use it in GitHub Desktop.
R: Project Euler 1-9
f <- function(n) {
x <- 999%/%n
x * (x + 1) / 2 * n
}
f(3) + f(5) - f(15)
library(gmp)
answer <- 0
f <- fibnum(n <- 1)
while (f < 4000000) {
if (f%%2 == 0) answer <- answer + f
f <- fibnum(n <- n+1)
}
answer
library(gmp)
max(factorize(600851475143))
# code A
answer <- 0
for (a in 999:100) for (b in a:100) {
x <- a*b
if (x <= answer) break
d <- x %/% (10^(floor(log10(x)):0)) %% 10
if (all(d == rev(d))) answer <- x
}
answer
# code B
palindrome <- function(n) {
x <- n %/% (10^(floor(log10(n)):0)) %% 10
all(x == rev(x))
}
m <- matrix(c(999, 999, 999*999), 3)
repeat {
max <- max(m[3,])
top <- m[,which(m[3,] == max)]
if (palindrome(top[3])) {print(top[3]); break}
a <- top[1]-1
b <- top[2]-1
v <- append(m[,which(m[3,] != max)], c(top[1], b, top[1] * b))
if (top[1] == top[2]) v <- append(v, c(a, a, a^2))
m <- matrix(v, 3)
}
library(gmp)
Reduce(lcm.bigz, 1:20)
n <- 100
x <- n*(n+1)/2
y <- n*(n+1)*(2*n+1)/6
x*x-y
library(pracma)
n <- 10001
primes(n*log(n)+n*log(log(n)))[n]
s <- c(
"73167176531330624919225119674426574742355349194934",
"96983520312774506326239578318016984801869478851843",
"85861560789112949495459501737958331952853208805511",
"12540698747158523863050715693290963295227443043557",
"66896648950445244523161731856403098711121722383113",
"62229893423380308135336276614282806444486645238749",
"30358907296290491560440772390713810515859307960866",
"70172427121883998797908792274921901699720888093776",
"65727333001053367881220235421809751254540594752243",
"52584907711670556013604839586446706324415722155397",
"53697817977846174064955149290862569321978468622482",
"83972241375657056057490261407972968652414535100474",
"82166370484403199890008895243450658541227588666881",
"16427171479924442928230863465674813919123162824586",
"17866458359124566529476545682848912883142607690042",
"24219022671055626321111109370544217506941658960408",
"07198403850962455444362981230987879927244284909188",
"84580156166097919133875499200524063689912560717606",
"05886116467109405077541002256983155200055935729725",
"71636269561882670428252483600823257530420752963450"
)
xs <- as.numeric(unlist(strsplit(paste(s, collapse=""), "")))
prod5 <- function(n) prod(xs[n:(n+4)])
max(sapply(1:996, prod5))
a <- 1:331
b <- 2:499
f <- function(a, b) a < b & a^2 + b^2 == (1000-a-b)^2
g <- function(a, b) a*b*(1000-a-b)
outer(a, b, g)[outer(a, b, f)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment