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="Simon Strandgaard, post: 4495078"] [snip] I could not find any fast solution.. -- Simon Strandgaard # Weird Numbers # Simon Strandgaard <neoneye@gmail.com> def divisors(value) ary =3D [] (value/2).times do |i| div =3D i + 1 ary << div if value % div =3D=3D 0 end ary end $bits =3D [] 32.times do |bit| $bits << 2 ** bit end def has_subset_equal_to(divs, value) pairs =3D divs.zip($bits) 1.upto(2 ** divs.size - 1) do |i| sum =3D 0 pairs.each{|div,b| sum+=3Ddiv if (i&b)>0 } return true if sum =3D=3D value end false end def find_weird_numbers(range_min, range_max) ary =3D [] range_min.upto(range_max) do |value| divs =3D divisors(value) sum =3D divs.inject(0){|a,b|a+b} ary << [value, divs] if sum > value end res =3D [] ary.each do |value, divs| if !has_subset_equal_to(divs, value) puts "##{value} is a WEIRD NUMBER" res << value else puts "##{value} is nothing" end end p res end p find_weird_numbers(1, 100) [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
[QUIZ] Weird Numbers (#57) Solution
Top