Created
March 12, 2012 05:53
-
-
Save funny-falcon/2020108 to your computer and use it in GitHub Desktop.
uglier is sometimes faster
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'yaml' | |
y=[] | |
(1..10000).each{|x| | |
#next unless x/2==(1.0*x)/2 | |
z=x | |
divisors=[1] | |
_continue=true | |
while _continue | |
_continue=false | |
((divisors.last+1)..z).each{|w| | |
r=z/w | |
if (1.0*z)/w==r && w<x | |
divisors<<w | |
_continue=true | |
break | |
end | |
} | |
end | |
if divisors.inject(&:+)>x and (3..(divisors.count)).find{|w| divisors.combination(w).find{|w2| w2.inject(&:+)==x } } | |
y<<x | |
end | |
} | |
puts "amount: #{y.count}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'yaml' | |
class Test | |
attr_reader :y | |
def initialize(up) | |
@up = up | |
@y = [] | |
end | |
def perform | |
for x in 1..@up | |
divisors, w = [1], 1 | |
while (w = get_next(w+1, x)) | |
divisors << w | |
end | |
y << x if sum(divisors) > x && check(divisors, x) | |
end | |
self | |
end | |
def get_next(from, z) | |
w = from | |
while w < z | |
return w if z/w == (1.0*z)/w | |
w += 1 | |
end | |
nil | |
end | |
def sum(ar) | |
s = ar[0] | |
i, up = 1, ar.size | |
while i < up | |
s += ar[i] | |
i += 1 | |
end | |
s | |
end | |
def check(divisors, x) | |
(3..(divisors.count)).find{|w| | |
divisors.combination(w).find{|w2| | |
sum(w2) == x | |
} | |
} | |
end | |
end | |
puts "amount: #{Test.new(10000).perform.y.count}" |
In JRuby, #[] is flagged as a method that requires a heap-based scope because some versions of it set $~ (backref). Using .at avoids that.
Wow :)
@mpapis that certainly could be true. There's also been some optimization work on JRuby master that may not be in your copy.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
that could be also java version (mine is a bit older):