Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
[QUIZ] Weird Numbers (#57) Solution
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="horndude77, post: 4495099"] Here's my solution. It looks pretty close to others. Not too fast, but it gets the job done. I think after I get a chance to look at other solutions I can write a some of this better. class Integer def divisors divs = [] 1.upto(Math.sqrt(self).to_i) do |i| divs += [i ,self/i].uniq if (self%i == 0) end divs.sort.reverse #reverse speeds things up a bit end def weird? divs = self.divisors - [self] return false if divs.sum < self divs.each_combination do |comb| return false if comb.sum == self end return true end end class Array def each_combination (2**self.length).times do |comb| curr = [] self.length.times do |index| curr << self[index] if(comb[index] == 1) end yield curr end end def sum inject(0) { |sum, i| sum + i } end end max = (ARGV[0] || 10000).to_i max.times do |i| puts i if i.weird? end -----HornDude77 [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
[QUIZ] Weird Numbers (#57) Solution
Top