The series, \(1^{1} + 2^{2} + 3^{3} + ... + 10^{10} = 10405071317\).
Find the last ten digits of the series, \(1^{1} + 2^{2} + 3^{3} + ... + 1000^{1000}\).
# R
1000**1000
## [1] Inf
# To manage very large numbers you have to use gmp
library(gmp)
##
## Attaching package: 'gmp'
## The following objects are masked from 'package:base':
##
## %*%, apply, crossprod, matrix, tcrossprod
sum_n_power_last_digit <- function(n, last_digit){
sum_series = sum(as.bigz(seq(1, n))**(seq(1, n)))
sum_n = stringr::str_split(as.character(sum_series),'')[[1]]
last_digits = sum_n[(length(sum_n)-last_digit+1):length(sum_n)]
return(last_digits)
}
t = Sys.time()
sum_n_power_last_digit(1000,10)
## [1] "9" "1" "1" "0" "8" "4" "6" "7" "0" "0"
Sys.time() - t
## Time difference of 0.006052971 secsIf we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 10000.
get_sum_multiples_below1 <- function(below,multiple_1,multiple_2){
sum <- 0
for(i in 1:(below-1)){
if((i %% multiple_1 == 0)|(i %% multiple_2 == 0)){
sum <- sum + i
}
}
return(sum)
}
t <- Sys.time()
get_sum_multiples_below1(1000000,3,5)
## [1] 233333166668
Sys.time() - t
## Time difference of 0.261224 secs
get_sum_multiples_below2 <- function(below,multiple_1,multiple_2){
seq <- seq(1,(below-1))
multiples <- sapply(seq,FUN = function(x){if((x %% multiple_1 == 0) | (x %% multiple_2 == 0)){x}else{0} })
return(sum(multiples))
}
t <- Sys.time()
get_sum_multiples_below2(1000000,3,5)
## [1] 233333166668
Sys.time() - t
## Time difference of 0.8092461 secs
get_sum_multiples_below3 <- function(below,multiple_1,multiple_2){
seq <- seq(1,(below-1))
multiples <- seq[which((seq %% multiple_1 == 0) | (seq %% multiple_2 == 0))]
return(sum(multiples))
}
t <- Sys.time()
get_sum_multiples_below3(1000000,3,5)
## [1] 233333166668
Sys.time() - t
## Time difference of 0.008511066 secsBy listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
nth_prime <- function(nth){
primes = c(2,3)
i = 3
while(length(primes)<nth){
i <- i+1
is_prime = TRUE
for(j in 2:(round(sqrt(i))+1)){
if(i%%j == 0){
is_prime = FALSE
}
if(is_prime == FALSE){
break
}
}
if(is_prime == TRUE){
primes = c(primes,i)
}
}
return(primes[nth])
}
t <- Sys.time()
nth_prime(10001)
## [1] 104743
Sys.time() - t
## Time difference of 0.559047 secs
nth_prime2 <- function(nth){
primes = c(2,3)
i = 3
while(length(primes)<nth){
i <- i+1
ratio = i%%seq(2,round(sqrt(i))+1) == 0
if(!any(ratio)){
primes = c(primes,i)
}
}
return(primes[nth])
}
t <- Sys.time()
nth_prime2(10001)
## [1] 104743
Sys.time() - t
## Time difference of 0.5223539 secsA palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
vec_palindrome = function(seq){
vec1 = rep(seq,each = length(seq))
vec2 = rep(seq,length(seq))
product = vec1*vec2
product_inv = stringi::stri_reverse(product)
palindromes = product[which(product == product_inv)]
return(max(palindromes))
}
t = Sys.time()
three_digits = 100:999
vec_palindrome(three_digits)
## [1] 906609
Sys.time() - t
## Time difference of 0.289113 secs