[Guide] Project Euler: Problem 10: Summation of primes (#30503)
* feat: added problem 10 solution * Added some formatting and minor grammar changes
This commit is contained in:
committed by
Christopher McCormack
parent
0a8b936915
commit
e653eab3f5
@ -3,8 +3,53 @@ title: Summation of primes
|
|||||||
---
|
---
|
||||||
## Problem 10: Summation of primes
|
## Problem 10: Summation of primes
|
||||||
|
|
||||||
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/project-euler/problem-10-summation-of-primes/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.
|
### Method:
|
||||||
|
- In this challenge we need to find sum of all prime numbers up to `n`.
|
||||||
|
- Example:
|
||||||
|
- If `n = 10` then prime numbers before it are `2, 3, 5, 7` and their sum is `17`.
|
||||||
|
- We've used Sieve of Eratosthenes algorithm to find prime numbers in the below solution.
|
||||||
|
|
||||||
<a href='https://github.com/freecodecamp/guides/blob/master/README.md' target='_blank' rel='nofollow'>This quick style guide will help ensure your pull request gets accepted</a>.
|
### Solution:
|
||||||
|
```js
|
||||||
|
function primeSummation(n) {
|
||||||
|
|
||||||
|
// Initializing the array and sum which hold all prime numbers and the total sum, respectively
|
||||||
|
let nums = []
|
||||||
|
let sum = 0;
|
||||||
|
|
||||||
|
// Upperbound of `n`
|
||||||
|
const upperBound = Math.ceil(Math.sqrt(n));
|
||||||
|
|
||||||
|
// Making an array of `n` numbers
|
||||||
|
for (let i = 0; i < n; i++){
|
||||||
|
nums.push(i);
|
||||||
|
}
|
||||||
|
nums[1] = 0;
|
||||||
|
|
||||||
|
// Looping until the upperbound
|
||||||
|
for (let i = 2; i <= upperBound; i++){
|
||||||
|
|
||||||
|
// Skipping if the number is already 0
|
||||||
|
if (nums[i] !== 0){
|
||||||
|
|
||||||
|
// Explcitly marking all multiples of `i` 0
|
||||||
|
for (let j = i*i; j < n; j += i){
|
||||||
|
if (nums[j] % i == 0) {
|
||||||
|
nums[j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sum and return all values in the array up to `n`
|
||||||
|
for (let i = 0; i < n; i++){
|
||||||
|
sum += nums[i];
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
- [Run Code](https://repl.it/@ezioda004/Project-Euler-Problem-10-Summation-of-primes)
|
||||||
|
|
||||||
|
### References:
|
||||||
|
- Sieve of Eratosthenes [Wikipedia](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
|
||||||
|
Reference in New Issue
Block a user