forked from StartupInstitute/DevPreWork01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem03.rb
More file actions
47 lines (42 loc) · 788 Bytes
/
problem03.rb
File metadata and controls
47 lines (42 loc) · 788 Bytes
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
def factors_of(num)
i = 2
limit = Math.sqrt(num)
@factors = []
while i <= limit do
if num % i == 0
@factors << i
end
i += 1
end
end
def is_prime(num)
return false if num == 0 || num == 1
i = 2
limit = Math.sqrt(num)
while i <= limit do
if num % i == 0
return false
break
end
i += 1
end
return true
end
def prime_numbers(numArray)
@prime_factors = []
numArray.each do |x|
@prime_factors << x if is_prime(x)
end
end
def largest_number(numArray)
@large_number = numArray.sort.last
end
def prime_factors(num)
factors_of(num)
prime_numbers(@factors)
largest_number(@prime_factors)
end
print "Enter a number: "
num = gets.to_i
prime_factors(num)
puts "#{@large_number} is the largest prime factor of #{num}"