-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem27.rb
57 lines (49 loc) · 882 Bytes
/
problem27.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
#!/usr/bin/ruby
require 'euler_helper'
def primes_under_detect x
primes = [2]
i = 3
while i < x
sq = Math.sqrt(i).round
primes.push(i) unless primes.detect {|divisor| (divisor <= sq) ? (i % divisor == 0) : break}
i += 2
end
primes
end
@primes = primes_under_detect 100000
puts 'primes ready'
def prime? n
@primes.include?(n)
end
def prime2 n
return false if n % 2 == 0
sq = Math.sqrt(n).ceil
i = 3
while i <= sq
return false if n % i == 0
i += 2
end
true
end
def quadratic a,b
lambda {|x| x ** 2 + a * x + b }
end
def test a,b
i = 0
form = quadratic(a,b)
while prime2(form.call(i).abs)
i += 1
end
i
end
res = {}
puts Benchmark.measure {
(-1000..1000).each do |a|
(-1000..1000).each do |b|
t = test(a,b)
res[t] = [a,b] if t > 0
end
end
}
puts res[res.keys.max].inspect
puts res.keys.max