Skip to content

Instantly share code, notes, and snippets.

@karbassi
Created September 11, 2008 05:31
Show Gist options
  • Save karbassi/10169 to your computer and use it in GitHub Desktop.
Save karbassi/10169 to your computer and use it in GitHub Desktop.
class PerfectNumbers
attr_reader :perfects
def initialize(len = nil)
return nil if len.nil?
# sequence A000043 in OEIS
# http://www.research.att.com/~njas/sequences/A000043
n = [2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203,
2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497,
86243, 110503, 132049, 216091, 756839, 859433, 1257787, 1398269,
2976221, 3021377, 6972593, 13466917]
@perfects = Array.new
(0..len.abs-1).each do
|i|
@perfects << (2**(n[i]-1)) * (2**n[i] - 1)
end
end
end
require 'perfects.rb'
p = PerfectNumbers.new(10)
puts p.perfects
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment