-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem3.js
More file actions
49 lines (34 loc) · 869 Bytes
/
problem3.js
File metadata and controls
49 lines (34 loc) · 869 Bytes
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
46
47
48
49
/* https://projecteuler.net/problem=3
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
Answer 6857
*/
var primeFunctions = function(target) {
var primeFactors = [] /* An array for logging all prime factors */
var isItPrime = function(n) { /* A function to test if an input ("n") is a prime number or not. If it is it will push the number to the primeFactors array */
var i = 2;
var notAPrime = false;
do {
if (n % i === 0) {
notAPrime = true;
i++
} else {
i++;
}
}
while (i < n);
if (notAPrime === false) primeFactors.push(n);
}
var f = 2;
do {
if (target % f === 0) {
isItPrime(f);
f++;
} else {
f++;
}
}
while (f < target);
console.log(primeFactors);
}
primeFunctions(13195);