エラトステネスの篩 - Wikipediaにあるものを自分で作ってみたいなーと思ったので、実装してみました。
エラトステネスの篩シミュレータ
×
探索時間:[ms]
- 素数:
エラトステネスの篩シミュレータのソースコード
function p(s){
document.getElementById('stdout').innerHTML += s + '<br>';
}
function getRandomInt(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function fill(a, n){
if(a instanceof Array){
for(var i = 0; i < a.length; i++){
a[i] = n;
}
}
}
function throughASieve(p){
if(!(p instanceof Array)) return;
var len = p.length;
var border = Math.sqrt(len);
for(var i = 1; i <= border; i++){
if(p[i] == 1){
var num = i + 1;
for(var j = num * 2; j <= len; j += num){
p[j - 1] = 0;
}
}
}
}
run_demo = function(){
var parentOfTable = document.getElementById('div');
var stdout = document.getElementById('stdout');
parentOfTable.innerHTML = '';
stdout.innerHTML = '';
var rowMax = document.getElementById('rowMax').value;
var colMax = document.getElementById('colMax').value;
var cellMax = rowMax * colMax;
var prime = new Array(parseInt(cellMax));
fill(prime, 1);
throughASieve(prime);
var cells = (function createTable(rowMax, colMax, opt_parent){
try{
if (rowMax < 1 || colMax < 1) throw 'createTable : Bouth rowMax and colMax should not be a negative number.';
} catch(e){
alert(e);
}
var parent = opt_parent || document.getElementsByTagName('body')[0];
var cells = new Array(rowMax * colMax);
var table = document.createElement('table');
var tbody = document.createElement('tbody');
var index;
for(var i = 0; i < rowMax; i++){
var row = document.createElement('tr');
for(var j = 0; j < colMax; j++){
index = parseInt(i * colMax + j);
var cell = document.createElement('td');
var cellText = document.createTextNode((index + 1));
cell.style.border = '1px #666 solid';
cell.appendChild(cellText);
row.appendChild(cell);
cells[index] = cell;
}
row.style.textAlign = 'center';
tbody.appendChild(row);
}
table.style.borderCollapse = 'collapse';
table.appendChild(tbody);
parent.appendChild(table);
return cells;
})(rowMax, colMax, parentOfTable);
var Color = {
value : 0,
changeColorMin : 60,
changeColorMax : 80,
nextColor : function(){
this.value += getRandomInt(this.changeColorMin, this.changeColorMax);
},
toHsl : function(lightness){
return 'hsl(' + this.value + ', 100%, ' + lightness + ')';
}
};
var border = Math.floor(Math.sqrt(cellMax));
var searchTime = document.getElementById('searchTime').value;
(function paint(index){
if(index + 1 > border) {
return finish();
}
p((index+1));
Color.nextColor();
cells[index].style.backgroundColor = Color.toHsl('50%');
var intervalId = window.setInterval((function(){
var num = index + 1;
var i = num * num;
return function(){
if(i > cellMax) {
window.clearInterval(intervalId);
while(prime[++index] != 1);
return paint(index);
}
cells[i - 1].style.backgroundColor = Color.toHsl('75%');
i += num;
}
})(), searchTime);
function finish(){
var intervalIdFinish = window.setInterval((function(){
Color.nextColor();
return function(){
while(index < cellMax && prime[index] != 1) index++;
if(index >= cellMax) {
window.clearInterval(intervalIdFinish);
return;
}
p((index + 1));
cells[index].style.backgroundColor = Color.toHsl('50%');
index++;
}
})(), searchTime);
}
})(1);
}
備考
シミュレータの実行中に「Run」を押すと、window.clearInterval()が実行されず素数の出力結果に前回実行時の素数が混ざって表示されます。
IE9ではhslがサポートされていないため、正しく動作しません。Firefox4, Google Chrome11, Opera11での動作は確認しました。
C言語-エラトステネスの篩 : Please Comment on My Codeこちらもどうぞ。