Skip to content

Instantly share code, notes, and snippets.

@ww24
Created November 15, 2012 03:41
Show Gist options
  • Save ww24/4076506 to your computer and use it in GitHub Desktop.
Save ww24/4076506 to your computer and use it in GitHub Desktop.
Prime - 10万以下の素数を表示
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);
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
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
@ww24
Copy link
Author

ww24 commented Nov 23, 2012

CPU: Intel Core2 Duo P8600 2.40Ghz
RAM: 4.00 GB

example.js: 5251ms
example.rb: 10192ms
prime.rb: 1665ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment