求100以内的质数

/* For every number from 0 to 100, do the following: */
for (var counter = 0; counter <= 100; counter++)
{
    /* Loop through values from 2 to 1 before the counter. */
    for (var i = 2; i <= counter-1; i++) {
        /* if the remainder of dividing counter by the current value of `i` is zero,
         * we know we don't have a prime, so break out of the loop:
         */
        if (counter%i === 0) break; 
    }
    /* If the loop completed and `i` is equal to the counter, that means counter is not
     * divisible by anything except for 1 and itself, making it prime
     */
    if(i === counter)
        console.log(counter);
}

 

posted @ 2014-08-10 15:29  shrekuu  阅读(153)  评论(0编辑  收藏  举报