MY #100 DAYS OF CODE CHALLENGE JOURNEY-DAY 1

in #coding6 years ago

Screenshot (25).png

Problem: Summation of all prime numbers up to and including a given number. This is one of the @freecodecamp challenges for Javascript certification.

Definition of Problem: A Prime number is a number greater than 1 which is not divisible by no other numbers except itself and 1. Given a certain number n and I am to find the sum of all prime numbers from two to n and n included too.

Algorithm

1- I listed all the numbers between between 2 to n
2-Checked each number in the list if it's divisible by any other number below it.
3.I added any number that was not divisible by any other numbers below to a new list.
4.The new list above is a list of prime numbers.
5.I got the sum of every number in the new list created and this is the sum of all prime numbers we are looking for.

Javascript Code

function sumPrime(num){

function isPrime(num){

for (let i = 2; i < num; i++){
if(num%i == 0){
return false;
}
}

return true;
}

let primes = [];
let sum = 0;

for(let i =2; i<=num; i++){
if(isPrime(i)){
primes.push(i);
}
}

for (let i=0; i<primes.length; i++){
sum += primes[i];
}

return sum;
}

console.log(sumPrime(6));

Coin Marketplace

STEEM 0.22
TRX 0.20
JST 0.034
BTC 98934.25
ETH 3347.90
USDT 1.00
SBD 3.08