Last active
October 8, 2015 15:18
-
-
Save Koitaro/3350803 to your computer and use it in GitHub Desktop.
R: Project Euler 40-49
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(pracma) | |
| xs <- colSums(t(perms(1:7)) * (10^(6:0))) | |
| for (x in xs) if(isprime(x)) {answer <- x; break} else next | |
| answer | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| words <- scan("words.txt", what="character", sep=",", na.strings="") | |
| words <- strsplit(words, "") | |
| f <- function(x, y) x + which(LETTERS %in% y) | |
| nums <- Vectorize(function(c) Reduce(f, c, 0))(words) | |
| tri <- 1:(ceiling(sqrt(max(nums)*2))) | |
| tri <- (tri*(tri+1))%/%2 | |
| length(nums[nums %in% tri]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| candidate <- function(d) { | |
| len <- length(d) | |
| if (len < 4) return(TRUE) | |
| if (len == 4) return((100*d[2]+10*d[3]+d[4]) %% 2 == 0) | |
| if (len == 5) return((100*d[3]+10*d[4]+d[5]) %% 3 == 0) | |
| if (len == 6) return((100*d[4]+10*d[5]+d[6]) %% 5 == 0) | |
| if (len == 7) return((100*d[5]+10*d[6]+d[7]) %% 7 == 0) | |
| if (len == 8) return((100*d[6]+10*d[7]+d[8]) %% 11 == 0) | |
| if (len == 9) return((100*d[7]+10*d[8]+d[9]) %% 13 == 0) | |
| if (len == 10) return((100*d[8]+10*d[9]+d[10]) %% 17 == 0) | |
| } | |
| nexts <- function(d) { | |
| n <- (n <- (0:9))[!n %in% d] | |
| sapply(n, function(n) list(c(d, n))) | |
| } | |
| res <- numeric(0) | |
| s <- sapply(1:9, list) | |
| while (length(s) > 0) { | |
| top <- s[[1]]; s <- s[-1] | |
| if (!candidate(top)) next | |
| if (length(top) == 10) {res <- c(res, list(top)); next} | |
| s <- c(nexts(top), s) | |
| } | |
| sum(sapply(res, function(d) sum(d*10^((length(d)-1):0)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| m <- 165; n <- 143; p <- (h <- 40755) | |
| while (p != h || p == 40755) { | |
| if (p < h) {m <- m+1; p <- m*(3*m-1)%/%2} | |
| else {n <- n+1; h <- n*(2*n-1)} | |
| } | |
| p |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(pracma) | |
| primes <- 3 | |
| isAnswer <- function(n) { | |
| x <- 1:(sqrt(n/2)) | |
| x <- n-(2*x*x) | |
| !any(x %in% primes) | |
| } | |
| n <- 3 | |
| repeat { | |
| n <- n+2 | |
| if (isprime(n)) {primes <- c(primes, n); next} | |
| if (isAnswer(n)) break | |
| } | |
| n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| max <- 1e6 | |
| x <- numeric(max) | |
| n <- 2 | |
| repeat { | |
| if (n > max) break | |
| if (x[n] == 0) { | |
| m <- n * 2:(max %/% n) | |
| x[m] <- x[m] + 1 | |
| } | |
| if (n > 4 && all(x[(n-3):n] == 4)) {answer <- n-3; break} | |
| n <- n+1 | |
| } | |
| answer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(gmp) | |
| sum(pow.bigz(1:1000,1:1000))%%1e10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(pracma) | |
| library(hash) | |
| p <- (p <- primes(9999))[p > 1000] | |
| digits <- apply(t(matrix( | |
| c(p%/%1000, p%/%100%%10, p%/%10%%10, p%%10), nr=length(p) | |
| )), 2, sort) | |
| d <- colSums(digits * (10^(3:0))) | |
| f <- function(xs) { | |
| for (x in xs) { | |
| if (x == 1487) next | |
| y <- (y <- xs[xs > x])[(2*y-x) %in% y] | |
| if (length(y) > 0) return(c(x, y, 2*y-x)) | |
| } | |
| } | |
| h <- invert(hash(p,d)) | |
| for (k in keys(h)) { | |
| v <- strtoi(h[[k]]) | |
| if (length(v) < 3) next | |
| if (length(x <- f(v)) > 0) {answer <- x; break} | |
| } | |
| answer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment