-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler_helper.rb
166 lines (144 loc) · 2.49 KB
/
euler_helper.rb
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
require 'benchmark'
def f n
r = 1
i = n
while i > 1
r *= i
i -= 1
end
r
end
def sum list
list.inject(0) {|acc, i| acc += i}
end
def mul list
list.inject(1) {|acc, i| acc *= i}
end
def prime? n
return false if n % 2 == 0 && n != 2
sq = Math.sqrt(n).ceil
i = 3
while i <= sq
return false if n % i == 0
i += 2
end
true
end
def profile
require 'rubygems'
require 'ruby-prof'
RubyProf.start
yield
result = RubyProf.stop
# Print a flat profile to text
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT, 0)
end
def benchmark message=''
puts "#{message} "+ Benchmark.measure(message) { yield }.to_s
end
def factors_all(n)
x = n
factors = []
sq = Math.sqrt(n).floor
(2..sq).each do |prime|
#next if n % prime != 0
#break if prime > sq
if (n % prime == 0)
factors.push(prime)
end
end
factors + factors.map {|f| x / f} + [1,n]
end
def factors(n)
x = n
factors = []
sq = Math.sqrt(n).floor
(2..sq).each do |prime|
#next if n % prime != 0
#break if prime > sq
if (n % prime == 0)
factors.push(prime)
end
end
factors
end
def factorize(n)
return [n] if n < 4
x = n
factors = []
sq = Math.sqrt(n).floor
primes_under(sq).each do |prime|
next if n % prime != 0 || n ==1
#break if prime > sq
while (n % prime == 0)
factors.push(prime)
n /= prime
end
end
factors.push(n) unless n == 1
factors
end
def each_digit n, base = 10
while n >= 1
yield(n % base)
n /= base
end
end
def digits n
q = []
each_digit(n) {|d| q.unshift(d) }
q
end
def count_digits n, base=10
i=0
while n >= 1
n /= base
i+=1
end
i
end
def int? i
i.to_i == i
end
def primes_under n #euler sieve
candidates = (1..n).to_a
fin = (n**0.5).ceil
# Loop over the candidates, marking out each multiple.
# If the current candidate is already checked off then
# continue to the next iteration.
for i in 2..fin do
next if candidates[i-1].nil?
for j in i..candidates.size/i+1 do
candidates[j*i-1] = nil
end
end
candidates.shift
candidates.compact!
end
class Integer
def each_digit base = 10
n = self
while n >= 1
yield(n % base)
n /= base
end
end
def digits
q = []
each_digit {|d| q.unshift(d) }
q
end
end
class Array
def except *x
self - x.to_a.flatten
end
def sum
self.inject(:+)
end
end
def overput str
STDOUT.write("\r#{str}")
STDOUT.flush
end