如何使用函数 showPrimes() 和参数 (limit) 按升序显示小于 (limit)

<script type = "text/javascript">

  function showPrimes(limit) {

    for (let number = 2; number <= limit; ++number) {

      let isPrime = true;

    }

  }

</script>


在代码之上,我目前正在努力。不确定如何继续。


白猪掌柜的
浏览 88回答 1
1回答

HUH函数

function getPrimes(limit) {&nbsp; &nbsp; var sieve = [], i, j, primes = [];&nbsp; &nbsp; for (i = 2; i <= limit; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; if (!sieve[i]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; primes.push(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (j = i << 1; j <= limit; j += i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sieve[j] = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return primes;}console.log(getPrimes(100));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript