I'm using JS and this code for founding 'goldbach partition'.
if I make length 100 array, it use more memory than I make length 101 array.
and also if I use primeArr.length, then it use more memory than using maxVal.
I don't understand why they have difference.
'use strict';
solution(arr);
function solution (arr) {
arr = arr.map(Number);
// arr = [6, 8, 10, 12, 100];
const maxVal = Math.max(...arr); // maxVal = 100;
// if it isn't added 1 at length, more memory about 1.2 mb.
const primeArr = Array.from({length : maxVal + 1}, v => true);
primeArr[0] = false;
primeArr[1] = false;
// if I use primeArr.length instead of maxVal, also more memory about 1.2 mb.
for (let i = 2; i <= maxVal; i++) {
if (primeArr[i]) {
for (let j = i * i; j <= maxVal; j += i) {
primeArr[j] = false;
}
}
}
let ans = [];
arr.forEach(el => {
let cnt = 0;
for (let i = 3; i <= el / 2; i += 2) {
if (primeArr[i] && primeArr[el - i]) {
cnt++;
}
}
if (el === 4) cnt = 1;
ans.push(cnt);
});
console.log(ans.join('
'));
}
thank you for reading.