0

它必须只有函数、变量、循环等(基本的东西)。从我到目前为止所学到的知识(应该能够做到)中,我无法从头开始编写代码。让我真的很生气:/。如果你能一步一步地给我以确保我理解我真的很感激。非常感谢。

我怎么能用比这个更简单的代码得到相同的结果:

var primes=4; 
for (var counter = 2; counter <= 100; counter = counter + 1)
{
    var isPrime = 0;
    if(isPrime === 0){ 
        if(counter === 2){console.log(counter);} 
        else if(counter === 3){console.log(counter);} 
        else if(counter === 5){console.log(counter);} 
        else if(counter === 7){console.log(counter);} 
        else if(counter % 2 === 0){isPrime=0;} 
        else if(counter % 3 === 0){isPrime=0;} 
        else if(counter % 5 === 0){isPrime=0;} 
        else if(counter % 7 === 0){isPrime=0;}
        else {
            console.log(counter);
            primes = primes + 1;
        }
    }
}
console.log("Counted: "+primes+" primes");
4

6 回答 6

7

我今天心情不好,所以:

function printPrimesBetweenTwoAndOneHundredSimply(){

   var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97],
   i,
   arrayLength = primes.length;

   for(i = 0; i < arrayLength; i++){
     console.log(primes[i]);
   }

   console.log("Counted: " + arrayLength + " primes");
}
于 2012-10-12T04:48:32.677 回答
4

首先,你真的不需要为此使用===。标准==就足够了。其次,您可以将所有这些相同的行,除了一个数字变成一行:

var primes=4; 
for (var counter = 2; counter <= 100; counter = counter + 1)
{
    var isPrime = 0;
    if(isPrime == 0){ 
        if(counter == 2 || counter == 3 ||counter == 5 || counter == 7)console.log(counter);
        else if(counter % 2 == 0 || counter % 3 == 0 || counter % 5 == 0 || counter % 7 == 0)isPrime=0; 
        else {
            console.log(counter);
            primes = primes + 1;
        }
    }
}
console.log("Counted: "+primes+" primes");

您还会注意到{}在几行中删除了 。这是因为(以及其他)后面的单行代码if总是被认为是嵌套的。

接下来,我们可以将您的更改primes = primes + 1;为:primes++;它只是告诉primes自己增加一。您的柜台也可以这样做。我们也知道isPrime等于“0”,因为您在一秒钟前将其设置为,所以我们不再需要该if语句:

var primes=4; 
for (var counter = 2; counter <= 100; counter++)
{
    var isPrime = 0;
    if(counter == 2 || counter == 3 ||counter == 5 || counter == 7)console.log(counter);
    else if(counter % 2 == 0 || counter % 3 == 0 || counter % 5 == 0 || counter % 7 == 0)isPrime=0; 
    else {
        console.log(counter);
        primes++;
    }
}
console.log("Counted: "+primes+" primes");

接下来,我们可以对值进行否定检查(!=而不是==),并将 yourelse if与 your结合起来else。由于我们正在进行否定检查(对于这种情况),我们必须将 s ( ) 切换OR||s AND( &&):

var primes=4; 
for (var counter = 2; counter <= 100; counter++)
{
    if(counter == 2 || counter == 3 ||counter == 5 || counter == 7)console.log(counter);
    else if(counter % 2 != 0 && counter % 3 != 0 && counter % 5 != 0 && counter % 7 != 0) {
        console.log(counter);
        primes++;
    }
}
console.log("Counted: "+primes+" primes");

还有很多其他的方法来编写它,但我觉得使用你开始的东西并从那里缩短它对你更有益。

于 2012-10-12T05:00:25.563 回答
1

查找 2 到 100 之间的所有素数:

var primes=0; 
var isprime = true;
for (var counter = 2; counter <= 100; counter = counter + 1)
{
    // For now, we believe that it is a prime
    isprime = true;
    var limit = Math.round(Math.sqrt(counter)); // See comment from @AresAvatar, below
    // We try to find a number between 2 and limit that gives us a reminder of 0
    for (var mod = 2; mod <= limit; mod++) {
        // If we find one, we know it's not a prime
        if (counter%mod == 0) {
            isprime = false;
            break; // Break out of the inner for loop
        }
    }

    if (isprime) {
        console.log(counter, limit);
        primes = primes + 1;
    }
}
console.log("Counted: "+primes+" primes");
于 2012-10-12T04:50:58.707 回答
0

当然“更简单”没有定义。:-)

以下可以做得更短,但内置了一些鲁棒性。

// Get primes from 0 to n. 
// n must be < 2^53
function primeSieve(n) {

  n = Number(n);
  if (n > Math.pow(2, 53) || isNaN(n) || n < 1) {
    return;
  }

  var primes = [];
  var notPrimes = {};
  var num;

  for (var i=2; i<n; i++) {
    for (var j=2; j<n/2; j++) {
      num = i*j;
      notPrimes[num] = num;
    }
    if (!(i in notPrimes)) {
      primes.push(i);
    }
  }
  return primes
} 

所以如果你想要更少的代码:

// Get primes from 0 to n. 
// n must be < 2^53
function primeSieve2(n) {
  var primes = [], notPrimes = {};
  for (var i=2; i<n; i++) { 
    for (var j=2; j<n/2; j++)
      notPrimes[i*j] = i*j;
    i in notPrimes? 0 : primes.push(i);
  }
  return primes
}
于 2012-10-12T05:31:41.923 回答
0

这很简单(我的意思是,简短):

console.log(2); console.log(3);
var m5=25, m7=49, i=5, d=2, c=2;
for( ; i<100; i+=d, d=6-d )
{
    if( i!=m5 && i!=m7) { console.log(i); c+=1; }
    if( m5 <= i ) m5+=10;
    if( m7 <= i ) m7+=14;
}
c

它是具有2-3 轮分解的 Eratosthenes的“展开”筛子

一方面,我们将所有与 2 和 3 互质的小于 100(且大于 3)的数列举为 5+2+4+2+4+... 的部分和,因此没有倍数如此列举的数字中的 2 和 3。

On the other hand, we discard from among them all the multiples of 5 and 7, by enumerating them as partial sums of 25+10+10+10+... and 49+14+14+14+... . Multiples of 2 and 3 are not there in the first place, and the first multiple of 11 that needs to be discarded by the sieve of Eratosthenes is 121.

于 2012-10-14T00:48:58.373 回答
-3

这对于您的问题会更简单......

 for (int i = 2; i < 100; i++) {
int j = 0;
for (j = 2; j < i; j++)
    if ((i % j) == 0)break;
        if (i == j)
        System.out.print("  " + i);}
于 2012-10-12T07:15:11.993 回答