If 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 1000.
%% in R and % in Python.[1] 233168
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
repeat in R> x <- 1 # First Value
> y <- 2 # Second Value
> z <- 0 # Starting Point
> a <- 2 # Starting Sum
>
> repeat {
+
+ if (z>4000000) break
+
+ z = x + y
+ x = y
+ y = z
+ if (z%%2 == 0){
+ a = a + z
+ }
+ }
>
> a[1] 4613732
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
primeFactors() function already exists in R, I chose to not spend time replicating it.[1] 71 839 1471 6857
A 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.
> library(stringi)
>
> a <- 0
>
> for (i in 100:999){
+
+ for (x in 100:999){
+ y <- i * x
+ z <- stri_reverse(y)
+ if (y == z && y>a){
+ a <- y
+ }
+ }
+ }
>
> a[1] 906609
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
> #don't need to evaluate 1 2 4 5 10 20
> library(tictoc) # timer
>
> a = 1
> x = 0
> t = c(3,6,7,8,9,11,12,13,14,15,16,17,18,19)
>
> tic() # start timer
> while (a<20){
+ a = 6 # start at 6. Check 14 numbers.
+ x = x + 1 # Add 1 to loop
+ z = x * 20 # multiples of 20
+ for (i in t){
+ if (z %% i == 0){
+ a = a + 1 # success when 20
+ }
+ }
+ }
> z[1] 232792560
32.94 sec elapsed
> import time
+
+ a = 1
+ x = 0
+ t = [3,6,7,8,9,11,12,13,14,15,16,17,18,19]
+
+ start = time.time()
+
+ while a <20:
+ a = 6
+ x = x + 1
+ z = x * 20
+ for i in t:
+ if z % i == 0:
+ a = a + 1
+
+ print(z)232792560
Elapsed time in seconds: 17.222641706466675
Compute the prime factorization of each number from 1 to 20 and multiply the greatest power of each prime together:
The other numbers have lower powers and aren’t shown.
Solution:
\[2^4 \times 3^2 \times 5 \times 7 \times 11 \times 13 \times 17 \times 19 = 232,792,560\]
I used the primeFactors() function to create the following custom function. It will find the solution for any value.
> SmallestMultiple <- function(t){
+
+ x <- aggregate(data.frame(count = numbers::primeFactors(1)),
+ list(value = numbers::primeFactors(1)),
+ length)
+
+ # value count
+ # 1 1 1
+
+ for(i in 2:t){
+
+ y <- aggregate(data.frame(count = numbers::primeFactors(i)),
+ list(value = numbers::primeFactors(i)),
+ length)
+ x <- rbind(x,y)
+ }
+
+ # full table of prime factors for each number from 1 to t
+ # and their counts. ex. 20 = 2 2 5
+
+ # value count
+ # 1 1 1
+ # ...
+ # 26 2 2
+ # 27 5 1
+
+ aa <- aggregate(x$count,
+ list(value = x$value),
+ max)
+
+ # full table of prime numbers and the max count
+
+ # value x
+ # 1 1 1
+ # 2 2 4
+ # ...
+ # 8 17 1
+ # 9 19 1
+
+ # Solution
+ a <- prod(aa$value^aa$x)
+
+ if(t==1){
+ return(1)
+ } else {
+ return(a)
+ }
+
+ }> SmallestMultiple(1)
> SmallestMultiple(6)
> SmallestMultiple(8)
> SmallestMultiple(10)
> SmallestMultiple(20)
> SmallestMultiple(28)[1] 1
[1] 60
[1] 840
[1] 2520
[1] 232792560
[1] 80313433200
It is much faster than a loop.
[1] 232792560
0.04 sec elapsed