-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz329.cpp
More file actions
53 lines (48 loc) · 1.37 KB
/
Copy pathquiz329.cpp
File metadata and controls
53 lines (48 loc) · 1.37 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
46
47
48
49
50
51
52
53
#include <iostream>
#include <cmath>
using namespace std;
//jillian handrahan
int isaprime(int candidate){
int current = 2;
while (current * current <= candidate){ //checking all possible divisors until its square root
if ( candidate % current == 0){ // some divisor has been found
return(0);
}
current++;
}
return(1);
}
int largestprimefactor (int y) {
int candidate = 2;
y = abs(y);
int largest_so_far =1;
while (candidate * candidate <= y) { //checking all possible prime factors until its square root
if ((y % candidate == 0)){ // some prime factor has been found
if (((y / candidate) > candidate) && isaprime(y/candidate)) {
candidate = y/candidate;
}
if (isaprime(candidate) && (y % candidate == 0)) {
largest_so_far = candidate;
}
}
else {
}
candidate++;
}
if (largest_so_far == 1 && isaprime(y)) {
return y;
}
// if (largest_so_far < candidate && isaprime(candidate)){
// largest_so_far = candidate;
// }
return (largest_so_far);
}
int main() {
// int myTest[100];
largestprimefactor(28);
for (int i = 0; i < 100; i++) {
int largest = largestprimefactor(i);
cout << i << ": " << largest<< endl;
}
return 0;
}