-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.22.rb
59 lines (51 loc) · 876 Bytes
/
1.22.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
def smallest_divisor(n)
find_divisor(n, 2)
end
def divides?(a, b)
b % a == 0
end
def find_divisor(n, test_divisor)
if test_divisor ** 2 > n
n
elsif divides?(test_divisor, n)
test_divisor
else
find_divisor(n, test_divisor + 1)
end
end
def prime?(n)
n == smallest_divisor(n)
end
def timed_prime_test(n)
puts
print n
start_prime_test(n, Time.now.to_f)
end
def start_prime_test(n, start_time)
if prime?(n)
report_prime(Time.now.to_f - start_time)
return true
else
return false
end
end
def report_prime(elapsed_time)
print " *** "
print elapsed_time
end
def first_3_primes_from(m)
q = 0
loop do
if timed_prime_test(m)
q += 1
if q == 3
break
end
end
m += 1
end
end
first_3_primes_from(1000)
first_3_primes_from(10000)
first_3_primes_from(100000)
first_3_primes_from(1000000)