Count Primes in Range
JavaScript
Medium
3 views
Problem Description
Given L and R, count how many prime numbers are there in [L,R].
Input Format
One line: L R.
Output Format
One integer count.
Official Solution
const fs=require('fs');const p=fs.readFileSync(0,'utf8').trim().split(/\\s+/);if(!p[0])process.exit(0);let L=Number(p[0]),R=Number(p[1]);const n=R;let isPrime=new Uint8Array(n+1);isPrime.fill(1);if(n>=0)isPrime[0]=0;if(n>=1)isPrime[1]=0;for(let i=2;i*i<=n;i++){if(isPrime[i]){for(let j=i*i;j<=n;j+=i)isPrime[j]=0;}}let c=0;for(let x=L;x<=R;x++)if(isPrime[x])c++;process.stdout.write(String(c));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!