-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactor.cpp
More file actions
71 lines (63 loc) · 1.42 KB
/
factor.cpp
File metadata and controls
71 lines (63 loc) · 1.42 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
#include<cmath>
#include<vector>
#include<stdlib.h>
using namespace std;
vector<long long int> trialFactor(long long int num)
{
long long int upperBound = ceil(sqrt(num));
vector<long long int> factors;
for(long long int i = 2; i <= upperBound; i++)
{
if(num%i==0)
{
factors.push_back(i);
num /= i;
}
}
factors.push_back(num);
return factors;
}
vector<long long int> fermatFactor(long long int num)
{
vector<long long int> factors;
long long int start = ceil(sqrt(num));
for(long long int i = start; i < num/2; i++)
{
long long int test = i * i - num;
if(sqrt(test)==floor(sqrt(test)))
{
factors.push_back(i+sqrt(test));
factors.push_back(i-sqrt(test));
}
}
return factors;
}
int main(int argc, char** argv)
{
long long int num = atoi(argv[1]);
vector<long long int> factors = trialFactor(num);
cout << "Trial Testing" << endl;
if (factors[0]==num) cout << num << " is prime";
else
{
cout << "Factors of " << num << " are: ";
for(int i = 0; i < factors.size(); i++)
{
cout << factors[i] << " ";
}
}
cout << endl;
factors = fermatFactor(num);
cout << "Fermat's Method Testing" << endl;
if (factors[0]==num) cout << num << " is prime";
else
{
cout << "Factors of " << num << " are: ";
for(int i = 0; i < factors.size(); i++)
{
cout << factors[i] << " ";
}
}
cout << endl;
}