-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprob_005.rb
More file actions
37 lines (30 loc) · 774 Bytes
/
prob_005.rb
File metadata and controls
37 lines (30 loc) · 774 Bytes
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
# 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?
# useful:
# http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
# time: O(n*log(log(n)))
# space: O(n)
# stack: O(1)
# 0.05s user
# 0.03s system
# 65% cpu
# 0.133 total
def find_primes(n)
sieve = Hash.new(1)
for i in (2..n)
if sieve[i] >= 1
sieve[i] = (n**(1.0/i)).floor
(i**2).step(n, i) do |x|
sieve[x] = 0
end
end
end
primes = sieve.select { |n, v| v >= 1 }
primes.keys.map { |prime| prime**sieve[prime]}
end
def least_common_multiple(n)
find_primes(n).inject(:*)
end
puts least_common_multiple(20)