A prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13.
A semiprime is a natural number that is the product of two (not necessarily distinct) prime numbers. The first few semiprimes are 4, 6, 9, 10, 14, 15, 21, 22, 25, 26.
You are given two non-empty arrays P and Q, each consisting of M integers. These arrays represent queries about the number of semiprimes within specified ranges.
Query K requires you to find the number of semiprimes within the range (P[K], Q[K]), where 1 ≤ P[K] ≤ Q[K] ≤ N.
For example, consider an integer N = 26 and arrays P, Q such that:
P[0] = 1 Q[0] = 26 P[1] = 4 Q[1] = 10 P[2] = 16 Q[2] = 20The number of semiprimes within each of these ranges is as follows:
- (1, 26) is 10,
- (4, 10) is 4,
- (16, 20) is 0.
Write a function:
function solution(N, P, Q);
that, given an integer N and two non-empty arrays P and Q consisting of M integers, returns an array consisting of M elements specifying the consecutive answers to all the queries.
For example, given an integer N = 26 and arrays P, Q such that:
P[0] = 1 Q[0] = 26 P[1] = 4 Q[1] = 10 P[2] = 16 Q[2] = 20the function should return the values [10, 4, 0], as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..50,000];
- M is an integer within the range [1..30,000];
- each element of arrays P and Q is an integer within the range [1..N];
- P[i] ≤ Q[i].
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
const primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; )
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
const primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
console.log(primes);
}
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ false, false, true, true, false, true, false, true, false, false, false, true, false, true, false, false, false, true, false, true, false, false, false, true, false, false, false ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
const primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
const primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
const primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes =
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
const primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value) => {
if (value) {
acc.p
}
}, [])
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
const primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value) => {
if (value) {
acc.push()
}
}, [])
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
const primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) {
acc.push(index);
}
return acc;
}, [])
console.log(primes);
}
TypeError: Assignment to constant variable.
at solution (solution.js:16:12)
at solutionWrapper (/tmp/exec.js:421:28)
at Promise.resolve.then (/tmp/exec.js:447:24)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
at Function.Module.runMain (module.js:686:11)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) {
acc.push(index);
}
return acc;
}, [])
console.log(primes);
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) {
acc.push(index);
}
return acc;
}, [])
console.log(primes);
}
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 2, 3, 5, 7, 11, 13, 17, 19, 23 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
console.log(primes);
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
const primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
console.log(primes);
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
console.log(primes);
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = [];
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = [];
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semi
}
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Map();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.
}
}
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Map();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.set(semiPrime);
}
}
}
console.log(semiPrimes);
}
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
Map {
4 => undefined,
6 => undefined,
10 => undefined,
14 => undefined,
22 => undefined,
26 => undefined,
9 => undefined,
15 => undefined,
21 => undefined,
25 => undefined }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.set(semiPrime);
}
}
}
console.log(semiPrimes);
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
console.log(semiPrimes);
}
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
Set { 4, 6, 10, 14, 22, 26, 9, 15, 21, 25 }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
console.log(primes);
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
console.log(semiPrimes);
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
console.log(primes);
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
console.log(semiPrimes);
}
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 2, 3, 5, 7, 11, 13, 17, 19, 23 ]
Set { 4, 6, 10, 14, 22, 26, 9, 15, 21, 25 }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
console.log(primes);
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
console.log(semiPrimes);
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
console.log(primes);
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
console.log(semiPrime);
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
console.log(semiPrimes);
}
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 2, 3, 5, 7, 11, 13, 17, 19, 23 ]
4
6
10
14
22
26
34
38
46
6
9
15
21
33
39
51
57
69
10
15
25
35
55
65
85
95
115
14
21
35
49
77
91
119
133
161
22
33
55
77
121
143
187
209
253
26
39
65
91
143
169
221
247
299
34
51
85
119
187
221
289
323
391
38
57
95
133
209
247
323
361
437
46
69
115
161
253
299
391
437
529
Set { 4, 6, 10, 14, 22, 26, 9, 15, 21, 25 }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
console.log(primes);
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
console.log(semiPrime);
if (semiPrime <= N) {
console
semiPrimes.add(semiPrime);
}
}
}
console.log(semiPrimes);
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
console.log(primes);
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
console.log(semiPrimes)
semiPrimes.add(semiPrime);
}
}
}
console.log(semiPrimes);
}
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 2, 3, 5, 7, 11, 13, 17, 19, 23 ]
Set {}
Set { 4 }
Set { 4, 6 }
Set { 4, 6, 10 }
Set { 4, 6, 10, 14 }
Set { 4, 6, 10, 14, 22 }
Set { 4, 6, 10, 14, 22, 26 }
Set { 4, 6, 10, 14, 22, 26 }
Set { 4, 6, 10, 14, 22, 26, 9 }
Set { 4, 6, 10, 14, 22, 26, 9, 15 }
Set { 4, 6, 10, 14, 22, 26, 9, 15, 21 }
Set { 4, 6, 10, 14, 22, 26, 9, 15, 21 }
Set { 4, 6, 10, 14, 22, 26, 9, 15, 21 }
Set { 4, 6, 10, 14, 22, 26, 9, 15, 21, 25 }
Set { 4, 6, 10, 14, 22, 26, 9, 15, 21, 25 }
Set { 4, 6, 10, 14, 22, 26, 9, 15, 21, 25 }
Set { 4, 6, 10, 14, 22, 26, 9, 15, 21, 25 }
Set { 4, 6, 10, 14, 22, 26, 9, 15, 21, 25 }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
console.log(primes);
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
console.log(semiPrimes);
}
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 2, 3, 5, 7, 11, 13, 17, 19, 23 ]
Set { 4, 6, 10, 14, 22, 26, 9, 15, 21, 25 }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
console.log(primes);
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const or
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
console.log(primes);
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const orderedSemiPrimes = [...semiPrimes].sort((a, b) => b - a);
console.log(orderedSemiPrimes);
}
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 2, 3, 5, 7, 11, 13, 17, 19, 23 ] [ 26, 25, 22, 21, 15, 14, 10, 9, 6, 4 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
console.log(primes);
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const orderedSemiPrimes = [...semiPrimes].sort((a, b) => a - b);
console.log(orderedSemiPrimes);
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
console.log(primes);
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const orderedSemiPrimes = [...semiPrimes].sort((a, b) => a - b);
console.log(orderedSemiPrimes);
}
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 2, 3, 5, 7, 11, 13, 17, 19, 23 ] [ 4, 6, 9, 10, 14, 15, 21, 22, 25, 26 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const orderedSemiPrimes = [...semiPrimes].sort((a, b) => a - b);
const
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const orderedSemiPrimes = [...semiPrimes].sort((a, b) => a - b);
const cumSum = [];
fo
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const orderedSemiPrimes = [...semiPrimes].sort((a, b) => a - b);
const cumSum = [];
for (let i = 0; i <= N; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const orderedSemiPrimes = [...semiPrimes].sort((a, b) => a - b);
const cumSum = [0];
for (let i = 1; i <= N; i++) {
cumSum.push()
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const orderedSemiPrimes = [...semiPrimes].sort((a, b) => a - b);
const cumSum = [0];
for (let i = 1; i <= N; i++) {
cumSum.push(cumSum)
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const orderedSemiPrimes = [...semiPrimes].sort((a, b) => a - b);
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const orderedSemiPrimes = [...semiPrimes].sort((a, b) => a - b);
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (orde)
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.)
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i)) {
cumSum[i + 1]++;
}
}
console.log(cumSum);
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i)) {
cumSum[i + 1]++;
}
}
console.log(cumSum);
}
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 7, 8, 8, 8, 9, 10 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i)) {
cumSum[i + 1]++;
}
}
const results = [];
for (let i = 0; i < P.length; i++) {
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i)) {
cumSum[i + 1]++;
}
}
const results = [];
for (let i = 0; i < P.length; i++) {
results.push(cumSum[Q[i]] - cumSum[P[i]]);
}
return return;
}
solution.js:46
return return;
^^^^^^
SyntaxError: Unexpected token return
at createScript (vm.js:80:10)
at Object.runInNewContext (vm.js:135:10)
at getSolution (/tmp/exec.js:411:29)
at Promise.resolve.then (/tmp/exec.js:445:34)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
at Function.Module.runMain (module.js:686:11)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i)) {
cumSum[i + 1]++;
}
}
const results = [];
for (let i = 0; i < P.length; i++) {
results.push(cumSum[Q[i]] - cumSum[P[i]]);
}
return results;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i)) {
cumSum[i + 1]++;
}
}
const results = [];
for (let i = 0; i < P.length; i++) {
results.push(cumSum[Q[i]] - cumSum[P[i]]);
}
return results;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i)) {
cumSum[i + 1]++;
}
}
console.log(cumSum);
const results = [];
for (let i = 0; i < P.length; i++) {
results.push(cumSum[Q[i]] - cumSum[P[i]]);
}
return results;
}
[ 0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 7, 8, 8, 8, 9, 10 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i )) {
cumSum[i + 1]++;
}
}
console.log(cumSum);
const results = [];
for (let i = 0; i < P.length; i++) {
results.push(cumSum[Q[i]] - cumSum[P[i]]);
}
return results;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i + 1)) {
cumSum[i + 1]++;
}
}
console.log(cumSum);
const results = [];
for (let i = 0; i < P.length; i++) {
results.push(cumSum[Q[i]] - cumSum[P[i]]);
}
return results;
}
[ 0, 0, 0, 0, 1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 7, 8, 8, 8, 9, 10, 10 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i + 1)) {
cumSum[i + 1]++;
}
}
console.log(cumSum);
const results = [];
for (let i = 0; i < P.length; i++) {
results.push(cumSum[Q[i]] - cumSum[P[i] - 1]);
}
return results;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i + 1)) {
cumSum[i + 1]++;
}
}
console.log(cumSum);
const results = [];
for (let i = 0; i < P.length; i++) {
results.push(cumSum[Q[i]] - cumSum[P[i] - 1]);
}
return results;
}
[ 0, 0, 0, 0, 1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 7, 8, 8, 8, 9, 10, 10 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i + 1)) {
cumSum[i + 1]++;
}
}
const results = [];
for (let i = 0; i < P.length; i++) {
results.push(cumSum[Q[i]] - cumSum[P[i] - 1]);
}
return results;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i + 1)) {
cumSum[i + 1]++;
}
}
const results = [];
for (let i = 0; i < P.length; i++) {
results.push(cumSum[Q[i]] - cumSum[P[i] - 1]);
}
return results;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
// Sieve of Eratosthenes
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i + 1)) {
cumSum[i + 1]++;
}
}
const results = [];
for (let i = 0; i < P.length; i++) {
results.push(cumSum[Q[i]] - cumSum[P[i] - 1]);
}
return results;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
// Sieve of Eratosthenes
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
// converting to list of primes
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
// calculating semiprimes
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i + 1)) {
cumSum[i + 1]++;
}
}
const results = [];
for (let i = 0; i < P.length; i++) {
results.push(cumSum[Q[i]] - cumSum[P[i] - 1]);
}
return results;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
// Sieve of Eratosthenes
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
// converting to list of primes
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
// calculating semiprimes
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
// computing prefix sums of semiprimes
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i + 1)) {
cumSum[i + 1]++;
}
}
// running queries
const results = [];
for (let i = 0; i < P.length; i++) {
results.push(cumSum[Q[i]] - cumSum[P[i] - 1]);
}
return results;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
// Sieve of Eratosthenes
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
// converting to list of primes
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
// calculating semiprimes
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
// computing prefix sums of semiprimes
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i + 1)) {
cumSum[i + 1]++;
}
}
// running queries
const results = [];
for (let i = 0; i < P.length; i++) {
results.push(cumSum[Q[i]] - cumSum[P[i] - 1]);
}
return results;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(N, P, Q) {
// Sieve of Eratosthenes
let primes = new Array(N).fill(true);
primes[0] = primes[1] = false;
for (let i = 2; i < Math.sqrt(N); i++) {
if (primes[i]) {
for (let j = i + i; j <= N; j+=i) {
primes[j] = false;
}
}
}
// converting to list of primes
primes = primes.reduce((acc, value, index) => {
if (value) acc.push(index);
return acc;
}, [])
// calculating semiprimes
const semiPrimes = new Set();
for (let i = 0; i < primes.length; i++) {
for (let j = 0; j < primes.length; j++) {
const semiPrime = primes[i] * primes[j];
if (semiPrime <= N) {
semiPrimes.add(semiPrime);
}
}
}
// computing prefix sums of semiprimes
const cumSum = [0];
for (let i = 0; i <= N; i++) {
cumSum.push(cumSum[i]);
if (semiPrimes.has(i + 1)) {
cumSum[i + 1]++;
}
}
// running queries
const results = [];
for (let i = 0; i < P.length; i++) {
results.push(cumSum[Q[i]] - cumSum[P[i] - 1]);
}
return results;
}
The solution obtained perfect score.