Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions problem03.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Algorithm outline:
# 1) Find all primes below sqrt(number)
# 2) Remove nonfactors from this list of primes
# 3) Multiply prime factors together. If they exceed sqrt(number), stop.
# 4) If not, find another factor to test for primality.
# (note that we have found all potential factors in step #1)
# 5) If not prime, then keep repeating steps #3-4 for each new number
# until a prime is found.
def problem_03(number)
prime_list = find_primes_below(Math.sqrt(number).floor)
prime_factors = Array.new()

prime_list.each do |i|
if is_factor_of(number, i)
prime_factors.push(i)
end
end

partial_fact = array_product(prime_factors)
while partial_fact <= Math.sqrt(number)
remaining_fact = number/partial_fact
is_divisible_by = find_divisor(remaining_fact, prime_list)
unless is_divisible_by
prime_factors.push(remaining_fact)
end
partial_fact = array_product(prime_factors)
end

return prime_factors.max
end

# Returns an array of all primes below number, inclusive.
# Uses Sieve of Eratosthenes algorithm
def find_primes_below(number)
primes = Array.new(number + 1, true)
primes[0] = false
primes[1] = false
prime_count = primes.length - 2
index = 2
while index < primes.length
multiple = 2
number_not_prime = index * multiple
while number_not_prime < primes.length
if primes[number_not_prime] == true
primes[number_not_prime] = false
prime_count = prime_count - 1
end
multiple = multiple + 1
number_not_prime = index * multiple
end
index = index + 1
end

# Convert boolean array to integer array
prime_integers = Array.new(prime_count, nil)
count = 0
(0..primes.length).each do |i|
if primes[i] == true
prime_integers[count] = i
count = count + 1
end
end
return prime_integers
end

def is_factor_of(number, candidate)
if number % candidate == 0
return true
else
return false
end
end

# Finds a divisor for number in array. Returns false if no divisor is found.
def find_divisor(number, array)
array.each do |i|
if number % i == 0
return i
end
end
return false
end

def array_product(array)
product = 1
array.each do |i|
product = product * i
end
return product
end

puts problem_03(600851475143)
53 changes: 53 additions & 0 deletions problem05.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Algorithm outline:
# 1) Find all primes up to and including number.
# 2) Set lcm at 1.
# 3) Multiply lcm by the largest multiple of each prime that remains
# below number.
def problem_05(number)
lcm = 1
prime_array = find_primes_below(number)
prime_array.each do |prime|
i = prime
while i <= number
lcm = lcm * prime
i = i * prime
end
end
return lcm
end

# Returns an array of all primes below number, inclusive.
# Uses Sieve of Eratosthenes algorithm
def find_primes_below(number)
primes = Array.new(number + 1, true)
primes[0] = false
primes[1] = false
prime_count = primes.length - 2
index = 2
while index < primes.length
multiple = 2
number_not_prime = index * multiple
while number_not_prime < primes.length
if primes[number_not_prime] == true
primes[number_not_prime] = false
prime_count = prime_count - 1
end
multiple = multiple + 1
number_not_prime = index * multiple
end
index = index + 1
end

# Convert boolean array to integer array
prime_integers = Array.new(prime_count, nil)
count = 0
(0..primes.length).each do |i|
if primes[i] == true
prime_integers[count] = i
count = count + 1
end
end
return prime_integers
end

puts problem_05(20)
17 changes: 17 additions & 0 deletions problem08.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def problem_08()
number = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
# Convert number to string, then to array, then convert array elements to ints
number_array = number.to_s.split('').map(&:to_i)
max = 0
(0..number_array.length-5).each do |i|
candidate_product = 1
(0..4).each do |j|
candidate_product = candidate_product * number_array[i+j]
end
if candidate_product > max
max = candidate_product
end
end
return max
end
puts problem_08