Created
July 22, 2011 02:19
-
-
Save alenia/1098756 to your computer and use it in GitHub Desktop.
Why is this so much slower in Ruby than in Node?
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
def divisors_count(n) | |
count = 0 | |
bound = Math.sqrt(n).floor | |
if bound*bound == n | |
count = 1 | |
bound -= 1 | |
elsif (bound+1)*(bound+1) == n | |
count = 1 | |
end | |
(1..bound).each do |i| | |
count += 2 if n%i == 0 | |
end | |
return count | |
end |
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
exports.divisors_count = function (n) { | |
var count = 0 | |
var bound = Math.floor(Math.sqrt(n)) | |
if (bound*bound == n) { | |
count = 1; | |
bound -= 1; | |
} | |
else if ((bound+1)*(bound+1) == n) { | |
count = 1; | |
} | |
for(var i=1;i<=bound;i+=1) { | |
if (n%i ==0) {count += 2} | |
} | |
return 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
var methods = require('./eulerMethods.js'); | |
var problem12 = function() { | |
var n=1; | |
var triangle = 0; | |
var divisors | |
while (true) { | |
triangle +=n; | |
divisors=methods.divisors_count(triangle); | |
if (divisors>=100) { | |
console.log(triangle, divisors); | |
} | |
if (divisors >= 500) {return triangle} | |
n += 1; | |
} | |
} | |
console.log(problem12()); |
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
require "./euler_methods" | |
def problem12 | |
n=1 | |
triangle = 0 | |
while true | |
triangle +=n | |
divisors=divisors_count(triangle) | |
puts "#{triangle} #{divisors}" if divisors>=100 | |
return triangle if divisors >= 500 | |
n += 1 | |
end | |
end | |
puts problem12.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More time tests:
Ruby:
JS: