-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargest-Prime-Factor.js
More file actions
45 lines (37 loc) · 1.16 KB
/
Largest-Prime-Factor.js
File metadata and controls
45 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
Description: The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the given number?
Some examples are the following
largestPrimeFactor(2) should return 2.
largestPrimeFactor(3) should return 3.
largestPrimeFactor(5) should return 5.
largestPrimeFactor(7) should return 7.
largestPrimeFactor(8) should return 2.
largestPrimeFactor(13195) should return 29.
largestPrimeFactor(600851475143) should return 6857.
A better description of the problem can be found here: https://www.geeksforgeeks.org/prime-factor/
*/
function largestPrimeFactor(number) {
let prime = []
let num = number
//check if the number can be divided by 2
while (num%2==0){
num = num / 2
prime.push(2)
}
//else it can be divided by odd primes
//we know that every composite number has at least one
//prime factor less than or equal to square root of itself.
for(let i=3;i<=Math.sqrt(num);i=i+2){
while(num%i==0){
num = num / i
prime.push(i)
}
}
//get the final prime if one exists
if (num>2){
prime.push(num)
}
return Math.max(...prime);
}
largestPrimeFactor(2);