javascript sieve_eratosphen.c

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript sieve_eratosphen.c相关的知识,希望对你有一定的参考价值。

static unsigned char primeTbl[999999999];

int main (void) {
    int i, j;
    //by sieve, all numbers except 1 and 0 are prime in the beginning;
    for (i = 0; i < sizeof(primeTbl); i++)
        primeTbl[i] = 1;
    primeTbl[0] = 0;
    primeTbl[1] = 0;
    //the Sieve;
    //for each number
    for (i = 2; i < sizeof(primeTbl); i++)
    	{
    	//if this number is prime
    	if (primeTbl[i]!=0)
    		{
    		//we mark each of its multipliers as composite;
        	for (j = i + i; j < sizeof(primeTbl); j += i)
            	{
            	primeTbl[j] = 0;
            	}
    		}
    	}
    int primeCounter=0;
    //counting the primes;
    for (i = 0; i < sizeof(primeTbl); i++) {
    	if(primeTbl[i]!=0){
    		primeCounter++;
    	}
    }
    //output.
    printLineBreak();
    printInteger(sizeof(primeTbl));
    printLineBreak();
    printInteger(primeCounter);
    return 0;
}
function arrayOfPrimes(ceiling) {    
    var array = [2, 3];
    for (var i = 5; i <= ceiling; i += 2) {
        if (array.every(function(p) { return i % p; })) {
            array.push(i);
        }
    }
    return array;
}

以上是关于javascript sieve_eratosphen.c的主要内容,如果未能解决你的问题,请参考以下文章

javascript JavaScript isset()等效: - JavaScript

JavaScript 使用JavaScript更改CSS(JavaScript)

JavaScript之基础-1 JavaScript(概述基础语法)

前端基础-JavaScript的基本概述和语法

JavaScript

JavaScript