# Technical Challenge: Solution to Consecutive Prime Sum using Javascript

The aim of this guide is to walk us through solving the code challenge on how to get consecutive prime sum using Javascript. The following are the prerequisite to follow through this guide:

**Problem Statement**

The prime 41, can be written as the sum of six consecutive primes:

41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred.

The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, > and is equal to 953.

Which prime, below one-million, can be written as the sum of the most consecutive primes?

**Consecutive Prime Sum**

What does that even mean? Lets break it down!

Prime Number: a number that is divisible only by itself and 1.

A consecutive prime number is then the sum of prime numbers in their sequential order.

**Steps to solving this challenge**

The steps for this problem are fairly simple:

Solve for the prime number given a specific number.

Calculate the sum of the prime number.

Get the sum of the prime number not greater than one-million.

In our solution, we would be optimising for modularity which improves maintainability of the code. Meaning that we would be writing different modular functions, to get to our end result.

### 1. getPrime

```
function getPrime(n) {
// a for loop that iterates through the given parameter
// starts its iteration from 2;
for (let i = 2; i <= n; i++) {
// boolean to set if numbers are a prime number or not;
let isprime = true;
// a for loop to divde i by j where j is all the values less than i starting iteration from 2,
// hence if any of the division gives a remainder of 0, then it is not a prime number
// if at any point i is divided by numbers (j)less than it and none gives a remainder of 0,
// then it is a prime number
for (let j = 2; j < i; j++) {
if (i%j == 0) {
isprime = false;
break;
}
}
// if prime number, push the prime number to primeArr array container;
if (isprime) {
primeArr.push(i)
}
}
}
```

**Somethings to Note:**

In the first for-loop, we are iterating through a given number starting from 2

In the second for-loop, we re trying to follow the mathematical stand-point of prime number which says:

*a prime number is a number that can only be divided evenly by itself or 1; meaning every other division gives us a remainder.*Going by this, any number that gives a remainder of 0 is definitely not a prime number.

**Let's test this function**

To test this function, open your VS Code, copy and paste the code above in your newly created .js file as seen below:

To test our code, lets open our terminal having called the getPrime() function with argument 20 as seen above and run this command:

node app.js

The result shows the list of prime numbers from 2-20 as seen below:

Now that our function works, lets move to the next step! Get sum of prime numbers

### 2. getPrimeSum

```
function getPrimeSum(n) {
let totalSum = 0;
// a for loop to iterate through the primeArr container
// and logs the sum of the prime numbers
for (let i = 0; i < n; i++) {
// returns the sum of each prime numbers
const sum = getSum(i);
// if the sum of the prime number is less than one-million, return/log the sum;
if (sum < 1000000) {
totalSum = sum;
console.log(totalSum);
}
}
return totalSum;
}
```

**Somethings to Note**

This function returns the sum of each prime numbers pushed to primeArr container sequentially.

It calls a getSum helper function which simply does the addition of the prime numbers.

At the end it returns the sum of prime numbers less than one-million.

**Let's test this function**

Copy and paste the above code in your .js file as seen below:

Now that we have our code, lets test our function by calling the getPrime and getPrimeSum function.

Run this command

node app.js

The result shows the sum of prime numbers sequentially as seen below:

Note we can spot **41** which was referred to in our problem statement. It represents the sum of the first 6 consecutive prime number. It's safe to say our getPrimeSum now works and we can put it all together.

### 3. getSum

```
// an helper function to return the sum of the prime;
// given an index to be used in the primeArr container;
function getSum(n) {
return eachSum += primeArr[n]
}
```

### Full Code

```
// pushes each prime number to this array container;
let primeArr = [];
// calculates the sum of each prime number
let eachSum = 0;
// a function that returns the sum of the most consecutive primes below one-million
// might as well pass in one-million as an argument
function getConsecutivePrimeSum(n) {
getPrime(n);
let consPrimeSum = getPrimeSum(primeArr.length);
console.log(consPrimeSum);
}
function getPrime(n) {
// a for loop that iterates through the given parameter
// starts its iteration from 2;
for (let i = 2; i <= n; i++) {
// boolean to set if numbers are a prime number or not;
let isprime = true;
// a for loop to divde i by j where j is all the values less than i starting iteration from 2,
// hence if any of the division gives a remainder of 0, then it is not a prime number
// if at any point i is divided by numbers (j)less than it and none gives a remainder of 0,
// then it is a prime number
for (let j = 2; j < i; j++) {
if (i%j == 0) {
isprime = false;
break;
}
}
// if number is true, push the prime number to primeArr array container;
if (isprime) {
primeArr.push(i)
// console.log(i);
}
}
}
function getPrimeSum(n) {
let totalSum = 0;
// a for loop to iterate through the primeArr container
// and logs the sum of the prime numbers
for (let i = 0; i < n; i++) {
// returns the sum od each prime numbers
const sum = getSum(i);
// if the sum of the prime number isless than one-million, return/log the sum;
if (sum < 1000000) {
totalSum = sum;
// console.log(totalSum);
}
}
return totalSum;
}
// an helper function to return the sum of the prime;
// given an index to be used in the primeArr container;
function getSum(n) {
return eachSum += primeArr[n]
}
// calling the function
getConsecutivePrimeSum(100000);
```

### Code Walkthrough

The full code above makes use of the functions we have tested above to get the sum of prime number less than one-million.

We would be calling the getConsecutivePrimeSum() to get this figure.

Now, what number do we pass in as argument to get the sum of prime number below one-million? Should we pass in 1,000,000 as stated in the problem statement?

You would guess right that this process would take time. Looping through 1,000,000 took approximately 5 mins while solving this challenge and we wouldn't want to wait that time.

**What should we then do?**

Since its the sum of prime numbers, getting the number below one-million would be gotten before looping through 1,000,000. So lets input 100,000 as argument or as well input 1,000,000

**Let test our function**

To test our code, run this command

node app.js

The result returned the sum of prime numbers below one-million as seen below:

So the consecutive prime sum less than one-million is **997661**

## Conclusion

In this guide, we have generated quite a number of useful functions for solving this challenge and learnt about some mathematical terms. You can find the source code for this challenge here on Github.

Pretty much sure there are other means to approaching this, if your solution is different, i am curious as to how you solved it. Drop your suggestion below

Happy Building!