fix(guide): restructure curriculum guide articles (#36501)
* fix: restructure certifications guide articles * fix: added 3 dashes line before prob expl * fix: added 3 dashes line before hints * fix: added 3 dashes line before solutions
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
title: Project Euler
|
||||
---
|
||||
|
||||
## Project Euler
|
||||
# Project Euler
|
||||
[Project Euler](https://projecteuler.net/), named for mathematician Leonard Euler, is a popular resource for practicing writing efficient algorithms. It is inspired by the shared goal of mathematicians and programmers to compose elegant and efficient solutions to complex problems. Project Euler is language agnostic. Rather than testing coded solutions, it presents a question to users and asks them only to input the correct answer. For this reason it is a popular platform for people who are learning new languages as well as those who want to practice writing more efficient solutions. The challenges presented by Project Euler are math problems of increasing complexity. Although all of these challenges can be solved by an algorithm that runs in less than a minute, the most obvious may take hours or even days to complete. The Project Euler platform does not run your algorithm and does not grade you based on its efficiency, but once you have solved the problem to arrive at a correct solution you will be granted access to a discussion forum of the challenge where you can get community help on how to improve your approach.
|
||||
|
||||
#### More Information:
|
||||
|
@@ -1,19 +1,26 @@
|
||||
---
|
||||
title: Multiples of 3 and 5
|
||||
---
|
||||
## Problem 1: Multiples of 3 and 5
|
||||
# Problem 1: Multiples of 3 and 5
|
||||
|
||||
### Method:
|
||||
---
|
||||
## Problem Explanation
|
||||
- We can find if a number is divisble by another number with the help of `%` modulo operator.
|
||||
- `num1 % num2` returns `0` if there's no remainder while doing `num1/num2`.
|
||||
- Starting from `i = 3` because that's the first number that's divisble by 3 or 5, we loop through till the `number` provided.
|
||||
- If the number is divisible either by 3 or 5, we add that to the variable `sum` and finally return it.
|
||||
|
||||
### Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```js
|
||||
function multiplesOf3and5(number) {
|
||||
let sum = 0, i = 3;
|
||||
while (i < number){
|
||||
let sum = 0,
|
||||
i = 3;
|
||||
while (i < number) {
|
||||
if (i % 3 == 0 || i % 5 == 0) sum += i;
|
||||
i++;
|
||||
}
|
||||
@@ -22,5 +29,7 @@ function multiplesOf3and5(number) {
|
||||
```
|
||||
|
||||
|
||||
### Reference:
|
||||
#### Relevant Links
|
||||
- [Modulo operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder_())
|
||||
|
||||
</details>
|
@@ -1,15 +1,20 @@
|
||||
---
|
||||
title: Summation of primes
|
||||
---
|
||||
## Problem 10: Summation of primes
|
||||
# Problem 10: Summation of primes
|
||||
|
||||
### Method:
|
||||
---
|
||||
## Problem Explanation
|
||||
- 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.
|
||||
|
||||
### Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```js
|
||||
function primeSummation(n) {
|
||||
|
||||
@@ -50,5 +55,6 @@ function primeSummation(n) {
|
||||
```
|
||||
|
||||
|
||||
### References:
|
||||
#### Relevant Links
|
||||
- Sieve of Eratosthenes [Wikipedia](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
|
||||
</details>
|
||||
|
@@ -1,9 +1,10 @@
|
||||
---
|
||||
title: Even Fibonacci Numbers
|
||||
---
|
||||
## Problem 2: Even Fibonacci Numbers
|
||||
# Problem 2: Even Fibonacci Numbers
|
||||
|
||||
### Method:
|
||||
---
|
||||
## Problem Explanation
|
||||
- A fibonacci sequence is a sequence where `fib(n) = fib(n-1) + fib(n-1)`.
|
||||
- In this challenge we have to sum all the even numbers upto `nth` term in the sequence.
|
||||
- Example for `fiboEvenSum(10)`:
|
||||
@@ -13,34 +14,51 @@ title: Even Fibonacci Numbers
|
||||
+ Sum of all even number in the above sequence is:
|
||||
2 + 8 + 34 = 44
|
||||
|
||||
### Solution:
|
||||
#### Relevant Links
|
||||
- [Wikipedia](https://en.wikipedia.org/wiki/Fibonacci_number)
|
||||
|
||||
#### Basic Solution - Iterative:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
Iterative
|
||||
```js
|
||||
function fiboEvenSum(n) {
|
||||
let first = 1, second = 2, sum = 2, fibNum; // declaring and initializing variables
|
||||
let first = 1,
|
||||
second = 2,
|
||||
sum = 2,
|
||||
fibNum; // declaring and initializing variables
|
||||
if (n <= 1) return sum; // edge case
|
||||
for (let i = 3; i <= n; i++){ // looping till n
|
||||
fibNum = first + second; // getting the ith fibonacci number
|
||||
for (let i = 3; i <= n; i++) {
|
||||
// looping till n
|
||||
fibNum = first + second; // getting the ith fibonacci number
|
||||
first = second;
|
||||
second = fibNum;
|
||||
if (fibNum%2 == 0) sum+=fibNum; // If even add to the sum variable
|
||||
if (fibNum % 2 == 0) sum += fibNum; // If even add to the sum variable
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
```
|
||||
#### Advanced Solution - Recursive:
|
||||
</details>
|
||||
|
||||
<details><summary>Solution 2 (Click to Show/Hide)</summary>
|
||||
|
||||
Recursive
|
||||
```js
|
||||
// We use memoization technique to save ith fibonacci number to the fib array
|
||||
function fiboEvenSum(n){
|
||||
const fib = [1, 1, 2];
|
||||
// We use memoization technique to save ith fibonacci number to the fib array
|
||||
function fiboEvenSum(n) {
|
||||
const fib = [1, 1, 2];
|
||||
let sumEven = fib[2];
|
||||
function fibonacci(n){
|
||||
if (n <= 1) return fib[n]; // base condition
|
||||
else if (fib[n]) return fib[n]; // if the number exists in the array we cache it and return
|
||||
function fibonacci(n) {
|
||||
if (n <= 1) return fib[n];
|
||||
// base condition
|
||||
else if (fib[n]) return fib[n];
|
||||
// if the number exists in the array we cache it and return
|
||||
else {
|
||||
fib[n] = fibonacci(n-1) + fibonacci(n-2); // otherwise calculcate and save it to the array
|
||||
if (fib[n]%2 == 0) sumEven+=fib[n]; //if the number is even, add it to the sumEven variable
|
||||
fib[n] = fibonacci(n - 1) + fibonacci(n - 2); // otherwise calculcate and save it to the array
|
||||
if (fib[n] % 2 == 0) sumEven += fib[n]; //if the number is even, add it to the sumEven variable
|
||||
return fib[n];
|
||||
}
|
||||
}
|
||||
@@ -48,6 +66,6 @@ function fiboEvenSum(n){
|
||||
return sumEven;
|
||||
}
|
||||
```
|
||||
</details>
|
||||
|
||||
|
||||
### References:
|
||||
- [Wikipedia](https://en.wikipedia.org/wiki/Fibonacci_number)
|
||||
|
@@ -1,29 +1,35 @@
|
||||
---
|
||||
title: Largest prime factor
|
||||
---
|
||||
## Problem 3: Largest prime factor
|
||||
# Problem 3: Largest prime factor
|
||||
|
||||
### Method:
|
||||
---
|
||||
## Problem Explanation
|
||||
- To find the largest prime factor of a number, we start from the smallest prime factor 2 and divide the number with it.
|
||||
- If the remainder is 0 that means the number is divisible by that prime number, we keep dividing the number by same prime number until that number is no more divisible by that prime number.
|
||||
- After that, we incrememnt the prime factor by 1 and repeat this process till the number becomes 1.
|
||||
|
||||
|
||||
### Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```js
|
||||
function largestPrimeFactor(number) {
|
||||
let prime = 2, max = 1;
|
||||
while (prime <= number){
|
||||
let prime = 2,
|
||||
max = 1;
|
||||
while (prime <= number) {
|
||||
if (number % prime == 0) {
|
||||
max = prime;
|
||||
number = number/prime;
|
||||
}
|
||||
else prime++; //Only increment the prime number if the number isn't divisible by it
|
||||
number = number / prime;
|
||||
} else prime++; //Only increment the prime number if the number isn't divisible by it
|
||||
}
|
||||
return max;
|
||||
}
|
||||
```
|
||||
|
||||
### Resources:
|
||||
#### Relevant Links
|
||||
|
||||
- [Wikipedia](https://en.wikipedia.org/wiki/Prime_number)
|
||||
</details>
|
||||
|
@@ -1,51 +1,54 @@
|
||||
---
|
||||
title: Largest palindrome product
|
||||
---
|
||||
## Problem 4: Largest palindrome product
|
||||
# Problem 4: Largest palindrome product
|
||||
|
||||
### Method:
|
||||
---
|
||||
## Problem Explanation
|
||||
- A palindromic number is the one that when reversed reads the same.
|
||||
- The largest number obtained from product of two 3 digit number is `999 * 999`, so we can make a loop that starts by producting the largest number and check if that number is palindromic or not.
|
||||
### Solution:
|
||||
|
||||
|
||||
---
|
||||
## Solutions
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```js
|
||||
function largestPalindromeProduct(n) {
|
||||
|
||||
//To get the maximum n digit number, + operator type castes String to Number type
|
||||
let max = +[...Array(n)].reduce((a, c) => a+=9, "");
|
||||
|
||||
let max = +[...Array(n)].reduce((a, c) => (a += 9), "");
|
||||
|
||||
//Next we get minimum n digit number from the max
|
||||
let min = (max+1)/10;
|
||||
|
||||
let min = (max + 1) / 10;
|
||||
|
||||
//To store the result
|
||||
let res = [];
|
||||
|
||||
|
||||
//Starting the loop from max to min
|
||||
for (let i = max; i >= min; i--){
|
||||
|
||||
//Another loop
|
||||
for (let j = max; j >= min; j--){
|
||||
|
||||
for (let i = max; i >= min; i--) {
|
||||
//Another loop
|
||||
for (let j = max; j >= min; j--) {
|
||||
//Getting the product
|
||||
let num = i*j;
|
||||
|
||||
let num = i * j;
|
||||
|
||||
//Reversing the number
|
||||
let numReverse = [...String(num)].reverse().join('');
|
||||
|
||||
let numReverse = [...String(num)].reverse().join("");
|
||||
|
||||
//Checking for palindromic number
|
||||
if (num == numReverse) {
|
||||
|
||||
//Pushing the number into array and breaking the loop for efficiency
|
||||
res.push(num);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Returning the maximum of the result array
|
||||
return Math.max(...res);
|
||||
}
|
||||
```
|
||||
|
||||
### References:
|
||||
#### Relevant Links
|
||||
|
||||
- [Wikipedia](https://en.wikipedia.org/wiki/Palindromic_number)
|
||||
</details>
|
||||
|
@@ -1,39 +1,45 @@
|
||||
---
|
||||
title: Smallest multiple
|
||||
---
|
||||
## Problem 5: Smallest multiple
|
||||
# Problem 5: Smallest multiple
|
||||
|
||||
### Method:
|
||||
---
|
||||
## Problem Explanation
|
||||
- In this challenge we need to find the LCM of 1 to n numbers.
|
||||
- To find LCM of a number we use the following formula:
|
||||
- 
|
||||
- To find GCD (Greatest Common Divisor) of two number we use Euclidean algorithm.
|
||||
- Once we get LCM of two numbers, we can get LCM of the numbers from 1 to n.
|
||||
|
||||
### Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```js
|
||||
//LCM of two numbers
|
||||
function lcm(a, b){
|
||||
return (a*b)/gcd(a, b);
|
||||
function lcm(a, b) {
|
||||
return (a * b) / gcd(a, b);
|
||||
}
|
||||
|
||||
//Euclidean recursive algorithm
|
||||
function gcd(a, b){
|
||||
//Euclidean recursive algorithm
|
||||
function gcd(a, b) {
|
||||
if (b === 0) return a;
|
||||
return gcd(b, a%b);
|
||||
return gcd(b, a % b);
|
||||
}
|
||||
|
||||
function smallestMult(n){
|
||||
function smallestMult(n) {
|
||||
let maxLCM = 1;
|
||||
|
||||
|
||||
//Getting the LCM in the range
|
||||
for (let i = 2; i <= n; i++){
|
||||
for (let i = 2; i <= n; i++) {
|
||||
maxLCM = lcm(maxLCM, i);
|
||||
}
|
||||
return maxLCM;
|
||||
}
|
||||
```
|
||||
|
||||
### References:
|
||||
#### Relevant Links
|
||||
- [Euclidean algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm)
|
||||
- [LCM](https://en.wikipedia.org/wiki/Least_common_multiple)
|
||||
</details>
|
||||
|
@@ -1,9 +1,10 @@
|
||||
---
|
||||
title: Sum square difference
|
||||
---
|
||||
## Problem 6: Sum square difference
|
||||
# Problem 6: Sum square difference
|
||||
|
||||
### Method:
|
||||
---
|
||||
## Problem Explanation
|
||||
- Sum of first n natural numbers can be calculated by using this formula:
|
||||
- 
|
||||
|
||||
@@ -12,7 +13,11 @@ title: Sum square difference
|
||||
|
||||
- We can calculate the values using the above formula and subtract them to get the result.
|
||||
|
||||
### Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```js
|
||||
function sumSquareDifference(n) {
|
||||
const sumOfN = (n*(n+1))/2;
|
||||
@@ -23,7 +28,9 @@ function sumSquareDifference(n) {
|
||||
}
|
||||
```
|
||||
|
||||
### References:
|
||||
#### Relevant Links
|
||||
|
||||
- [Sum of n numbers - Wikipedia](https://en.wikipedia.org/wiki/1_%2B_2_%2B_3_%2B_4_%2B_%E2%8B%AF)
|
||||
- [Sum of n square numbers - Wikipedia](https://en.wikipedia.org/wiki/Square_pyramidal_number)
|
||||
|
||||
</details>
|
@@ -1,49 +1,54 @@
|
||||
---
|
||||
title: 10001st prime
|
||||
---
|
||||
## Problem 7: 10001st prime
|
||||
### Method:
|
||||
# Problem 7: 10001st prime
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
- A prime number is a number which is divided by 1 and itself.
|
||||
- We can find a number is prime if it's not divisible by other prime numbers smaller than itself.
|
||||
|
||||
### Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```js
|
||||
function nthPrime(n) {
|
||||
|
||||
//Primes array which will store all the prime numbers
|
||||
const primes = [2];
|
||||
|
||||
|
||||
//Num is the number we want to check
|
||||
let num = 3, isPrime = true;
|
||||
|
||||
let num = 3,
|
||||
isPrime = true;
|
||||
|
||||
//Looping until primes array is equal to n
|
||||
while (primes.length < n){
|
||||
|
||||
while (primes.length < n) {
|
||||
//All the primes numbers of a number is always <= its square root
|
||||
let max = Math.ceil(Math.sqrt(num));
|
||||
|
||||
for (let i = 0; primes[i] <= max; i++){
|
||||
|
||||
for (let i = 0; primes[i] <= max; i++) {
|
||||
if (num % primes[i] == 0) {
|
||||
|
||||
//Looping till we find the prime
|
||||
isPrime = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//if Prime found, push it to the array
|
||||
if (isPrime) primes.push(num);
|
||||
isPrime = true;
|
||||
|
||||
//An optimization technique, since we know of all even numbers only 2 is a prime number, we can skip the rest
|
||||
num+=2;
|
||||
}
|
||||
|
||||
//Returning the last number
|
||||
return primes[primes.length-1];
|
||||
|
||||
//An optimization technique, since we know of all even numbers only 2 is a prime number, we can skip the rest
|
||||
num += 2;
|
||||
}
|
||||
|
||||
//Returning the last number
|
||||
return primes[primes.length - 1];
|
||||
}
|
||||
```
|
||||
|
||||
### References:
|
||||
#### Relevant Links
|
||||
- [Wikipedia](https://en.wikipedia.org/wiki/Prime_number)
|
||||
|
||||
</details>
|
File diff suppressed because it is too large
Load Diff
@@ -1,32 +1,39 @@
|
||||
---
|
||||
title: Special Pythagorean triplet
|
||||
---
|
||||
## Problem 9: Special Pythagorean triplet
|
||||
# Problem 9: Special Pythagorean triplet
|
||||
|
||||
### Method:
|
||||
---
|
||||
## Problem Explanation
|
||||
- In this challenge we need to find the pythagorean triple.
|
||||
- We have the following information - `a < b < c`
|
||||
- Based on this, we can make a loop starting from `a = 0` and `b = a` since `a < b` always.
|
||||
- We also know that `a + b + c = n` and `a^2 + b^2 = c^2`, since we have `a`, `b` and `n`. We can find `c` and see if it satisfies the triplet theorem.
|
||||
|
||||
### Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```js
|
||||
function specialPythagoreanTriplet(n) {
|
||||
let sumOfabc = n;
|
||||
for (let a = 1; a < n; a++){
|
||||
for (let b = a; b < n; b++){
|
||||
let c = n - a- b;
|
||||
if (c > 0){
|
||||
if (c**2 == a**2 + b**2){
|
||||
return a*b*c;
|
||||
for (let a = 1; a < n; a++) {
|
||||
for (let b = a; b < n; b++) {
|
||||
let c = n - a - b;
|
||||
if (c > 0) {
|
||||
if (c ** 2 == a ** 2 + b ** 2) {
|
||||
return a * b * c;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
specialPythagoreanTriplet(1000);
|
||||
```
|
||||
|
||||
### References:
|
||||
#### Relevant Links
|
||||
- [Wikipedia](https://en.wikipedia.org/wiki/Pythagorean_triple)
|
||||
|
||||
</details>
|
Reference in New Issue
Block a user