Skip to content

Instantly share code, notes, and snippets.

@davingee
Created February 9, 2016 09:29
Show Gist options
  • Save davingee/d9d89e518d7ea8fb53c6 to your computer and use it in GitHub Desktop.
Save davingee/d9d89e518d7ea8fb53c6 to your computer and use it in GitHub Desktop.
require 'active_support/core_ext/enumerable'
require 'pry'
class ProjectEuler
attr_accessor :hash, :array
def initialize( args = {} )
self.hash = {}
self.array = []
end
def three_and_five
# 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..999 ).each do |n|
next unless n % 3 == 0 || n % 5 == 0
array.push n
end
array.sum
end
def 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.
array = [ 1, 2 ]
past = 1
present = 2
loop_through = true
while loop_through do
array.push( past + present )
past = present
present = array.last
loop_through = false if ( past + present ) > 4000000
end
array.select{ |n| n % 2 == 0 }.sum
end
def largest_prime_factor
# The prime factors of 13195 are 5, 7, 13 and 29.
# What is the largest prime factor of the number 600851475143 ?
check_again = lambda{ |n| }
number = 600851475143
hash = {
number => { }
}
(2..( number / 2 ) ).each do |n|
hash[ number ][ number / n ] = {} if number % n == 0
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment