Skip to content

Instantly share code, notes, and snippets.

@johana-star
Created May 26, 2011 09:55
Show Gist options
  • Save johana-star/992861 to your computer and use it in GitHub Desktop.
Save johana-star/992861 to your computer and use it in GitHub Desktop.
Euler Project Problem #005
# Solution to Project Euler's fifth problem
# http://projecteuler.net/index.php?section=problems&id=5
# Find the smallest number divisible with no remainder by each number below twenty.
def generate_primes(ceiling)
primes, count = [1, 2], 1
until primes.last > ceiling do
numbers = (primes[count-1]**2+1..primes[count]**2).to_a
1.upto(count) {|c| numbers.select! {|number| number%primes[c] != 0}}
primes.concat(numbers)
count += 1
end
primes.shift #remove 1
until primes.last < ceiling
primes.pop #remove last
end
return primes
end
def generate_exponents(factors)
ceiling = factors.last
until factors.first == ceiling do
if factors.first * factors.first < ceiling then
first = factors[0]
factors[0] = factors[0] * first
while factors[0] < ceiling/2
factors[0] = factors[0] * first
end
end
factors.push factors.shift
puts factors.last
end
return factors
end
number, smallest = 20, 1
factors = generate_primes(number)
factors = generate_exponents(factors)
p factors
factors.each do |f|
smallest = smallest * f
end
puts smallest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment