The sum of the squares of the first ten natural numbers is,
\[1^2+2^2+\dotsc+10^2=385\]
The square of the sum of the first ten natural numbers is,
\[(1+2+\dotsc+10)^2=55^2=3025\]
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is \(3025-385=2640\).
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
> # Vector of 1 to 100
> x <- c(1:100)
> # Square the cumulative sum. Index 100 is the final value.
> z <- cumsum(x)[100]^2
> # Square each value and get the sum.
> y <- sum(x^2)
> # Calculate the difference.
> z-y [1] 25164150
25164150
By listing the first six prime numbers: \(2, 3, 5, 7, 11,\) and \(13,\) we can see that the \(6^{th}\) prime is \(13\).
What is the \(10,001^{st}\) prime number?
nextPrime() function - R> library(numbers)
> y = 0
> a = 0
>
> while (a<10001){
+ y = numbers::nextPrime(y) #the next prime
+ a = a+1 # number of primes
+
+ }
> y #answer[1] 104743
!any() to determine if it is prime.> y <- 7 #start by evaluating 7
> a <- 3 # count start = 3 (2,3, and 5)
>
> while (a<10001){
+
+ if(!any(y%%c(2,seq(3,as.integer(y^0.5)+1,2))==0)){
+ a = a+1 #prime count
+ }
+
+ y = y+2 #loop through odd numbers
+ }
>
> y-2[1] 104743
The four adjacent digits in the \(1,000\)-digit number that have the greatest product are \(9 × 9 × 8 × 9 = 5,832\).
73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450
Find the thirteen adjacent digits in the \(1,000\)-digit number that have the greatest product. What is the value of this product?
\n strings (new line).> x <- "73167176531330624919225119674426574742355349194934
+ 96983520312774506326239578318016984801869478851843
+ 85861560789112949495459501737958331952853208805511
+ 12540698747158523863050715693290963295227443043557
+ 66896648950445244523161731856403098711121722383113
+ 62229893423380308135336276614282806444486645238749
+ 30358907296290491560440772390713810515859307960866
+ 70172427121883998797908792274921901699720888093776
+ 65727333001053367881220235421809751254540594752243
+ 52584907711670556013604839586446706324415722155397
+ 53697817977846174064955149290862569321978468622482
+ 83972241375657056057490261407972968652414535100474
+ 82166370484403199890008895243450658541227588666881
+ 16427171479924442928230863465674813919123162824586
+ 17866458359124566529476545682848912883142607690042
+ 24219022671055626321111109370544217506941658960408
+ 07198403850962455444362981230987879927244284909188
+ 84580156166097919133875499200524063689912560717606
+ 05886116467109405077541002256983155200055935729725
+ 71636269561882670428252483600823257530420752963450"
>
> library(stringr)
>
> # length 1019 including \n values
> str_length(x) [1] 1019
> #split into array of individual strings
> y <- strsplit(x,"")
>
> # convert to integers. \n will convert to NA
> z <- as.integer(y[[1]])
>
> # filter out NA values
> z <- z[!is.na(z)]
>
> # clean length is 1000 integers
> length(z)[1] 1000
> b <- 0 #starting value
>
> for (i in 1:(length(z)-12)){
+ a <- prod(z[i:(i+12)])
+
+ if (a>b){
+ b <- a
+ w <- i
+ }
+
+ }
>
> b #answer[1] 23514624000
[1] 5 5 7 6 6 8 9 6 6 4 8 9 5
> x = """73167176531330624919225119674426574742355349194934
+ 96983520312774506326239578318016984801869478851843
+ 85861560789112949495459501737958331952853208805511
+ 12540698747158523863050715693290963295227443043557
+ 66896648950445244523161731856403098711121722383113
+ 62229893423380308135336276614282806444486645238749
+ 30358907296290491560440772390713810515859307960866
+ 70172427121883998797908792274921901699720888093776
+ 65727333001053367881220235421809751254540594752243
+ 52584907711670556013604839586446706324415722155397
+ 53697817977846174064955149290862569321978468622482
+ 83972241375657056057490261407972968652414535100474
+ 82166370484403199890008895243450658541227588666881
+ 16427171479924442928230863465674813919123162824586
+ 17866458359124566529476545682848912883142607690042
+ 24219022671055626321111109370544217506941658960408
+ 07198403850962455444362981230987879927244284909188
+ 84580156166097919133875499200524063689912560717606
+ 05886116467109405077541002256983155200055935729725
+ 71636269561882670428252483600823257530420752963450"""
+
+ len(list(x))1019
1000
> b = 0
+
+ for i in range(len(zz)-12):
+ a = np.prod(zz[i:(i+13)], dtype=np.int64)
+ if a > b:
+ b = a
+ b23514624000
A Pythagorean triplet is a set of three natural numbers, \(a < b < c\), for which,
\[a^2 + b^2 = c^2\] For example, \(3^2 + 4^2 = 9 + 16 = 25 = 5^2\).
There exists exactly one Pythagorean triplet for which \(a + b + c = 1,000\).
Find the product \(abc\).
> for (i in c(335:997)){
+
+ c <- i
+
+ if (i>500){
+ b <- c(2:(1000-c-1))
+ }else{
+ b <- c(2:(c-1))
+ }
+
+ for (i in b){
+ a <- 1000 - i - c
+
+ if(a<i && a^2 + i^2 == c^2){
+ x <- a
+ y <- i
+ z <- c
+ }
+ }
+
+ }
>
> x
> y
> z
>
> x*y*z[1] 200
[1] 375
[1] 425
[1] 31875000
The sum of the primes below \(10\) is \(2 + 3 + 5 + 7 = 17\).
Find the sum of all the primes below \(2,000,000\).