Forum: Ruby Re: Weird Numbers (#57) Solution

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
87e9a89c53ccf984db792113471c2171?d=identicon&s=25 simon.kroeger.ext (Guest)
on 2005-12-05 14:03
(Received via mailing list)
Hi,

I aimed for a fast yet elegant solution:
(Its way faster than my first try, way slower than my
fastest, but the one I liked the most)

---------------------------------------------------------
require 'set'

class Fixnum
  def weird?
    divisors = (1..1+self/2).select{|i| (self % i).zero?}
    return false if divisors.inject{|s, x| s + x} <= self
    !divisors.inject([0].to_set) do |sums, d|
      sums.merge(sums.map{|s| s + d})
    end.include?(self)
  end
end

$stdout.sync = true
1.upto((ARGV.shift || 1000).to_i){|i| puts i if i.weird?}
---------------------------------------------------------

cheers

Simon
This topic is locked and can not be replied to.