Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution for summatio of primes #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions Solutions/summation_of_primes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const readline = require('readline');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

let numbers
let testcases = []
let count = 1

rl.on('line', (input) => {
if (!numbers) {
numbers = parseInt(input);
} else {
if (count < numbers) {
testcases.push(parseInt(input));
count ++;
} else if (count === numbers) {
testcases.push(parseInt(input));

// here comes the logic
outputSummation(testcases)
rl.close()
}
};
});


const outputSummation = (testcases) => {
for (let item of testcases) {

if (testcases === 1) {
console.log(1)
} else if (testcases === 2) {
console.log(3)
} else {
let primesArray = [2]
for (let i = 3; i <= item; i ++) {
let bool = true
for (let prime of primesArray) {
if (i%prime === 0) {
bool = false;
break
}
}
if(bool) {
primesArray.push(i)
}
}

let sum = 1
for(let prime of primesArray) {
sum += prime
}
console.log(sum)
}


}
}