-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdivisible_by_all.rb
More file actions
102 lines (91 loc) · 1.76 KB
/
divisible_by_all.rb
File metadata and controls
102 lines (91 loc) · 1.76 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# This approach would work and the code is clean..
# it just takes a long tim e to calculate
def multiples(x)
r = []
max = (2..x).inject(1) {|a, b| a * b}
(2..x).each do |v|
p = v
while p < max
r[p] ||= 0
r[p] += 1
p += v
end
end
m = []
r.each_with_index do |v, i|
m << [i, v]
end
m.detect{|a, s| s == x - 1}
end
# f = multiples(10)
def primes(x)
r = []
max = (x ** 0.5) + 1
r[max] = 1
(2..max).each do |v|
if r[v].nil?
p = v * 2
while p < max
r[p] = 1
p += v
end
end
end
primes = []
r.each_with_index do |v, i|
if v.nil?
primes << i
end
end
primes
end
def process(x)
# get the prime numbers
l = primes(x * x)
l.delete(1)
# determine which numbers are multiple of primes and can be ignored
# from the total set of numbers (primes must be included)
a = []
l.each do |v|
(l - [v]).each do |w|
if v * w <= x
a << (v * w)
end
end
end
# remove any which are not direct mutliples of primes
# but are multiple of values in the remaining set where the
# multiples are not multiple of themselves
# i.e. [12, 3, 4] => [3, 4]
# [8, 4, 2] => [8, 4, 2] (as 4 is 2 * 2..)
b = ((2..x).map - a)
g = []
b.each do |o|
(b - [o]).each do |e|
r = o / e
if (r < e) && (r * e == o)
q = e / r
if q * r != e
g << o
end
end
end
end
# remove any number which are divisors of values in
# the remaining set
# so for [2, 3, 8] => [3, 8]
k = b - g
u = []
k.each do |j|
(k - [j]).each do |i|
n = j / i
if i * n == j
u << i
end
end
end
# multiple your set
(k - u).inject{|a,b| a * b}
end
puts process(10)
puts process(20)