Created
November 15, 2012 03:41
-
-
Save ww24/4076506 to your computer and use it in GitHub Desktop.
Prime - 10万以下の素数を表示
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 start_time = new Date().getTime(); | |
// https://gist.github.com/4077178 | |
Number.prototype.upto = function (n, f) { | |
if (typeof n !== "number") | |
throw new TypeError(f + " is not a number"); | |
if (typeof f !== "function") | |
throw new TypeError(f + " is not a function"); | |
f = f.bind(this); | |
for (var i = this + 0; i <= n; i++) | |
f(i); | |
return this; | |
}; | |
var primes = []; | |
(2).upto(100000, function (x) { | |
if (primes.every(function (y) { | |
return x % y !== 0; | |
})) { | |
primes.push(x); | |
console.log(x); | |
} | |
}); | |
var end_time = new Date().getTime(); | |
console.log("time: %dms", end_time - start_time); |
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
start_time = Time.now | |
primes = [] | |
2.upto 100000 do |x| | |
if primes.all? do |y| | |
x % y != 0 | |
end | |
then | |
primes << x | |
p x | |
end | |
end | |
end_time = Time.now | |
p "%dms" % ((end_time - start_time) * 1000).to_i |
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 'prime' | |
start_time = Time.now | |
Prime.each(100000) do |prime| | |
p prime | |
end | |
end_time = Time.now | |
p "%dms" % ((end_time - start_time) * 1000).to_i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CPU: Intel Core2 Duo P8600 2.40Ghz
RAM: 4.00 GB
example.js: 5251ms
example.rb: 10192ms
prime.rb: 1665ms