-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprime_factorization.cpp
68 lines (58 loc) · 948 Bytes
/
prime_factorization.cpp
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
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
bool
isprime(int n)
{
bool prime = true;
for (int i = 2; i <= sqrt(n); i++)
{
if (n % i == 0)
{
prime = false;
break;
}
}
return prime;
}
int
main()
{
std::vector<int> arr;
std::set<int> primefactors;
int num;
std::cin >> num;
for (int i = 2; i < num; i++)
{
if (isprime(i))
{
arr.push_back(i);
}
}
for (int i = 0; i < arr.size(); i++)
{
std::vector<int>::iterator it = std::find(arr.begin(), arr.end(), (num / arr[i]));
if (it!=arr.end())
{
if (arr[i] * (num / arr[i]) == num)
{
primefactors.insert(arr[i]);
primefactors.insert(num / arr[i]);
}
}
}
if (primefactors.empty())
{
std::cout << "The number " << num <<" cannot be factorized into prime numbers";
}
else
{
//If output is only one number it means (number^2)
for (int j : primefactors)
{
std::cout << j << " ";
}
}
return 0;
}