-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path15_1_prime_factorization_using_sieve.cpp
51 lines (42 loc) · 1.27 KB
/
15_1_prime_factorization_using_sieve.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
#include <iostream>
using namespace std;
/*
Prime factorization is the process of writing a number as the product of prime numbers.
40 | 2
20 | 2
10 | 2
5 | 5
So, Prime factorization of 40 is 40=2*2*2*5, where all the factors are prime.
*/
void primefactor(int n)
{
int spf[n + 1] = {0}; // SPF = Smallest Prime Factor
for (int i = 2; i <= n; i++) // Marking smallest prime factor for every number to be itself or Initialize all numbers as prime
{
spf[i] = i;
}
for (int i = 2; i * i <= n; i++) // Using Sieve, Replace all Numbers from 2 to N, with their Prime Factors
{
if (spf[i] == i) // Checking if spf[i] is prime (i.e., its value is still i)
{
for (int j = i * i; j <= n; j += i) // Mark all multiples of i with i as their SPF
{
if (spf[j] == j) // Checking spf[j] if it is not previously marked
{
spf[j] = i; // Marking i with its smallest prime factor
}
}
}
}
while (n != 1)
{
cout << spf[n] << " "; // Printing Prime Factors
n = n / spf[n]; // Prime factorization by dividing by smallest prime factor at every step
}
}
int main()
{
int n;
cin >> n;
primefactor(n);
}