-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge03.js
51 lines (42 loc) · 1.18 KB
/
challenge03.js
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
50
51
// Project Euler Challenge 3
// Find largest prime factor in a given number
//This was also my first attempt at terminal js!!!! Used node to run.
// Kicked off by the get number function which prompts user
// FindLargest is then called as a callback function
getNumber();
function getNumber(){
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(">>What number should we look at? ", function(answer) {
console.log(answer);
findLargest(answer);
rl.close();
});
}
// 'i' and 'v' set to 2 initially as all numbers are divisible by 1
// divcount variable used to determine if a number is prime. A count greater than zero is not.
// if found to be prime, we determine if it is a factor of givennumber
// if it is a factor, we print! the last number printed is the largest.
function findLargest(givennumber){
//Starting at 2
var i = 2;
var v = 2;
var divcount = 0;
while(i<givennumber){
i=i+1;
divcount = 0;
for(v=2;v<i;v++){
if(i%v == 0){
divcount = divcount + 1;
}
}
if(divcount == 0){
if(givennumber%i == 0){
console.log(i + " is a prime factor!");
}
}
}
}