forked from StartupInstitute/DevPreWork01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem03Module.rb
More file actions
35 lines (32 loc) · 828 Bytes
/
Copy pathProblem03Module.rb
File metadata and controls
35 lines (32 loc) · 828 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
require "prime"
module Problem03Module
def Problem03Module.factorize(input)
primeFactors = []
if Prime.prime?(input) || input == 1
primeFactors[0,0] = input
return primeFactors
else
Integer.each_prime(input-1) do |aPrime|
if input % aPrime == 0
primeFactors[0,0] = aPrime
return primeFactors.concat(factorize(input / aPrime))
end
end
end
end
def Problem03Module.main(args)
begin
inp = args.first.to_i
if inp <= 0
raise "Enter a non-zero positive number"
end
primeFactors = factorize(inp)
printf("Largest prime factor for %d is %d\n", inp, primeFactors.last)
return primeFactors.last
rescue StandardError
printf("Unexpected exception occured finding largest prime factor! ")
printf("Usage: ruby problem03.rb <number>\n")
return nil
end
end
end