Skip to content

Commit 89bd200

Browse files
committed
exercism 63- perfect numbers
1 parent 9377037 commit 89bd200

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ solution of many challenges of [Leetcode](https://leetcode.com/), [Exercism](htt
280280
60. [Parallel Letter Frequency](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/parallel_letter_frequency.rb)
281281
61. [Bob](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/bob.rb)
282282
62. [Nth Prime](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/nth_prime.rb)
283+
63. [Perfect Numbers](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/perfect_numbers.rb)
283284

284285
<a name="leetcode"/>
285286

exercism/perfect_numbers.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Problem: https://exercism.org/tracks/ruby/exercises/perfect-numbers
2+
3+
# Solution
4+
class PerfectNumber
5+
def self.classify(num)
6+
raise RuntimeError if num<=0
7+
factor_sum = 1
8+
for i in 2..(Math.sqrt(num).ceil)
9+
if num%i==0
10+
factor_sum+=i
11+
factor_sum+=(num/i) unless i*i == num
12+
end
13+
end
14+
comparison = factor_sum <=> num
15+
case comparison
16+
when 0
17+
"perfect"
18+
when 1
19+
"abundant"
20+
when -1
21+
"deficient"
22+
end
23+
end
24+
end

0 commit comments

Comments
 (0)