# Project Euler

## Multiples of 3 and 5

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# Start the array numbers = (1...1000) puts "Multples of 3 and 5 from array:" puts "#{numbers}" puts # Select only the multiples of 3 & 5 divi = numbers.select{|x| x%3 == 0 || x%5 == 0} puts "The numbes to add are:" puts "#{divi}" puts sum = divi.inject(0) { |sum, i| sum+i } puts "The total is: #{sum}" # Ouptput Multples of 3 and 5 from array: 1...1000 The numbes to add are: [3, 5, 6, 9, 10, 12, 15, ........ 995, 996, 999] The total is: 233168 |

## Even Fibonacci numbers

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.

this was fun, my first attempt was no successful, basically I create a code that would work for a small number of the series, and it work but when I plug the 4,000,000 well it kill the console, literally after few minutes it returns “kill” I guess that was the brute force approach and I pay dearly, the following code is the brute force and DO NOT plug it on your computer or you will pay for it, but it would be nice if you see it.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
numbers = [1, 2] 4000000.times do numbers.push(numbers.last + numbers[(numbers.length)-2]) end even_numbers = numbers.select{|x| x%2 == 0} sum = even_numbers.inject(0) { |sum, i| sum+i } puts "The total is: #{sum}" # Output Killed |

well that is what I got for creating arrays with millions of entries, jajaja,

now the second attempt, this time I had to do some research and took me time, but after some failures the following code is the one that work

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
addition = 0 num_1 = 1 num_2 = 2 while num_2 < 4000000 if num_2.even? == true addition += num_2 end holder = num_1 + num_2 num_1 = num_2 num_2 = holder end puts "The sum is: #{addition}" # Output The sum is: 4613732 |

## Largest prime factor

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

This is one of those programs that need to be improve because I don’t think the code is efficient, it works, gets the job done but I will re-think this one latter and come with a better solution, in the mean time here is a working solution.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
def prime_numbers(n) $primes = [] i = 2 while $primes.length < n do divisors = (2..9).to_a.select { |x| x != i } $primes << i if divisors.all? { |x| i % x != 0 } i += 1 end $primes end prime_numbers(10000) # puts "#{$primes}" number = 600851475143 run = 0 prime_factors = [] while number > 1 if number % $primes[run] == 0 valor = $primes[run] puts "div by #{$primes[run]}" puts number = number / $primes[run] prime_factors.push(valor) # puts "#{prime_factors}" puts end if run == 10000 puts "Break Out - Looped" break end run += 1 end puts "Prime Factors are:" puts "#{prime_factors}" puts puts " Largest Prime Factor is: #{prime_factors.last}" # Output div by 71 8462696833 div by 839 10086647 div by 1471 6857 div by 6857 1 Prime Factors are: [71, 839, 1471, 6857] Largest Prime Factor is: 6857 |

## Largest palindrome product

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# Palindrome candidates = [] run2 = 999 999.times do run1 = 999 999.times do number = run1 * run2 s1 = number.to_s s2 = s1.reverse if s1 == s2 candidates.push(s1.to_i) end run1 -= 1 end run2 -= 1 end puts "The largest palindrome made from the product of two 3-digit numbers is:" puts candidates.max # Output The largest palindrome made from the product of two 3-digit numbers is: 906609 |