6. Sum Square Difference


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.


Array Approach - R
> # 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
Array Approach - Python
> import numpy as np
+ seq = np.arange(0,101) 
+ z = seq.cumsum()[100]**2
+ y = sum(seq**2)
+ z-y
25164150
Loop Approach - R
> SumSqDiff <- function(x){
+   z <- 0
+   a <- 0
+ 
+       for (i in 1:x){
+         y <- i^2 # square each value from 1 to x
+         z <-  z + y # sum of each squared value
+         a <-  a+i # sum of all values from 1 to x
+       }
+ 
+   a^2 - z # result
+ }
> 
> SumSqDiff(10)
> SumSqDiff(100)
[1] 2640
[1] 25164150
Loop Approach - Python
> def SumSqDiff(x):
+     z = 0
+     a = 0
+     for i in range(1,x+1):
+         y = i**2
+         z = z+y
+         a = a+i 
+     return a**2 - z
+     
+ print(SumSqDiff(10))
2640
> print(SumSqDiff(100))
25164150

7. 10,001st Prime


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?


Using nextPrime() function - R
  • Loop through prime numbers
> 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
Loop Approach - R
  • Loop through odd numbers and count primes.
  • One of the traits of numbers is that, if they have a factor pair, one of the factors must be equal to or less than the square root.
  • Test 2 plus all odd numbers up to the square root
  • Use !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
Loop Approach - Python
> y = 7
+ a = 3
+ arr1 = [2]
+ 
+ while a<10001:
+     if not any(y % np.concatenate((arr1, np.arange(3,int(y ** 0.5) + 1,2))) == 0):
+         a = a + 1
+     y = y + 2
+     
+ y-2
104743

8. Largest Product in a Series


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?


Loop Approach - R
  • Import as string.
  • Split into individual strings.
  • Convert into integers and remove NA values.
  • NA values result from \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
  • Loop through each array of size 13.
> 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
> z[w:(w+12)] #values
 [1] 5 5 7 6 6 8 9 6 6 4 8 9 5
Loop Approach - Python
> 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
> y = list(x) #individual strings
+ while '\n' in y: y.remove('\n') #remove \n
+ len(y)
1000
> z = [int(i) for i in y] #convert to integers
+ zz = np.asarray(z) # turn list into array
> b = 0
+ 
+ for i in range(len(zz)-12):
+     a = np.prod(zz[i:(i+13)], dtype=np.int64)
+     if a > b:
+         b = a
+ b
23514624000

9. Special Pythagorean Triplet


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\).


Loop Approach - R
  • Min \(c\) value can be \(335\). \(\space\space335+333+332 = 1000.\)
  • Max \(c\) value can be \(997\). \(\space\space997+2+1 = 1000.\)
  • If \(c>500\) max \(b\) value is \(1000-c-1\).
  • If \(c\leq500\) max \(b\) value is \(c-1\).
  • \(a\) value is \(1000-b-c\)
  • If \(a<b\) and \(a^2+b^2=c^2\) record value.
> 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
Loop Approach - Python
> for i in range(335,998):
+     c = i
+     
+     if i>500:
+         b = range(2,(1000-c))
+     else:
+         b = range(2,c)
+     
+     for i in b:
+         a = 1000 - i - c
+         if a < i and a**2 + i**2 == c**2:
+             x = a
+             y = i
+             z = c
+ 
+ print(x)
200
> print(y)
375
> print(z)
425
> print(x*y*z)
31875000

10. Summation of Primes


The sum of the primes below \(10\) is \(2 + 3 + 5 + 7 = 17\).

Find the sum of all the primes below \(2,000,000\).


Using isPrime() from the numbers package - R
> #check 2 and all odd numbers through 2,000,000
> x <- c(2,seq(3,2000000,2))
> sum(x[numbers::isPrime(x)])
[1] 142913828922
Using List Comprehnsion - Python
> n = 2000000
+ primes_list = [2] + [i for i in range(3, n, 2) if
+                      not any(not i % x for x
+                              in range(3, int(i ** 0.5) + 1, 2))]
+ print(sum(primes_list))
142913828922