Files

36 lines
994 B
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: 'Problem 3: Largest prime factor'
2018-10-12 15:37:13 -04:00
---
# Problem 3: Largest prime factor
2018-10-12 15:37:13 -04:00
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
- 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.
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
2018-10-12 15:37:13 -04:00
```js
function largestPrimeFactor(number) {
let prime = 2,
max = 1;
while (prime <= number) {
2018-10-12 15:37:13 -04:00
if (number % prime == 0) {
max = prime;
number = number / prime;
} else prime++; //Only increment the prime number if the number isn't divisible by it
2018-10-12 15:37:13 -04:00
}
return max;
}
```
#### Relevant Links
2018-10-12 15:37:13 -04:00
- [Wikipedia](https://en.wikipedia.org/wiki/Prime_number)
</details>