-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path003.rb
84 lines (70 loc) · 1.56 KB
/
003.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
# frozen_string_literal: true
#solved
# The prime factors of 13195 are 5, 7, 13 and 29.
# What is the largest prime factor of the number 600851475143 ?
# def lpf (given, divisor=2)
# if given == divisor
# return divisor
# elsif given % divisor == 0
# given /= divisor
# lpf(given)
# else
# lpf(given, divisor+1)
# end
# end
# puts lpf 600851475143
# def lpf (a)
# f = 2
# while f <= a
# if a % f == 0
# a /= f
# else
# f += 1
# end
# end
# return f
# end
def lpf (a)
f = 3
while (a % 2 == 0) && (a > 2)
a /= 2
end
if a == 2
return a
end
while f <= a
if a % f == 0
a /= f
else
f += 2
end
end
return f
end
# lpf 10 # => 5
# one-liner:
a=600851475143; d=2; a%d==0 ? (f=d;a/=d) : d+=1 while d<=a; puts f # => nil
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
1000.times {a=600851475143; d=2; a%d==0 ? (f=d;a/=d) : d+=1 while d<=a; f}
ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
elapsed = ((ending - starting)*1000).round(4)
puts elapsed
# puts lpf 600851475143
# puts lpf 13195
# puts lpf 10
# puts lpf 8
# puts lpf 4
# puts lpf 3
# puts lpf 2
# puts ""
# for n in 2..10000
# puts lpf n
# end
# x = 600851475143
# starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
# 1000.times {lpf x}
# ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
# elapsed = ((ending - starting)*1000).round(4)
# puts "#{lpf x} (1000 times in #{elapsed} ms)"
# >> 6857
# >> 373.2327