2018-04-22 00:52:40 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
2018-03-17 22:50:11 +08:00
|
|
|
#include <algorithm>
|
2017-10-28 04:31:19 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
// Declaring variables for maintaing prime numbers and to check whether a number is prime or not
|
|
|
|
bool isprime[1000006];
|
|
|
|
vector<int> prime_numbers;
|
2019-08-21 10:10:08 +08:00
|
|
|
vector<pair<int, int>> factors;
|
2017-10-28 04:31:19 +08:00
|
|
|
|
|
|
|
// Calculating prime number upto a given range
|
|
|
|
void SieveOfEratosthenes(int N)
|
|
|
|
{
|
2018-03-17 22:50:11 +08:00
|
|
|
// initializes the array isprime
|
|
|
|
memset(isprime, true, sizeof isprime);
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
for (int i = 2; i <= N; i++)
|
2018-03-17 22:50:11 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
if (isprime[i])
|
2018-03-17 22:50:11 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
for (int j = 2 * i; j <= N; j += i)
|
|
|
|
isprime[j] = false;
|
2018-03-17 22:50:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
for (int i = 2; i <= N; i++)
|
2018-03-17 22:50:11 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
if (isprime[i])
|
2018-03-17 22:50:11 +08:00
|
|
|
prime_numbers.push_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 04:31:19 +08:00
|
|
|
// Prime factorization of a number
|
|
|
|
void prime_factorization(int num)
|
|
|
|
{
|
2018-03-17 22:50:11 +08:00
|
|
|
|
2018-03-17 23:02:21 +08:00
|
|
|
int number = num;
|
2018-03-17 22:51:07 +08:00
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
for (int i = 0; prime_numbers[i] <= num; i++)
|
2018-03-17 22:50:11 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
int count = 0;
|
2018-03-17 22:50:11 +08:00
|
|
|
|
2018-03-17 23:02:21 +08:00
|
|
|
// termination condition
|
|
|
|
if (number == 1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2018-03-17 22:50:11 +08:00
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
while (number % prime_numbers[i] == 0)
|
2018-03-17 23:02:21 +08:00
|
|
|
{
|
|
|
|
count++;
|
2019-08-21 10:10:08 +08:00
|
|
|
number = number / prime_numbers[i];
|
2018-03-17 22:50:11 +08:00
|
|
|
}
|
2018-03-17 23:02:21 +08:00
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
if (count)
|
|
|
|
factors.push_back(make_pair(prime_numbers[i], count));
|
2018-03-17 22:50:11 +08:00
|
|
|
}
|
2017-10-28 04:31:19 +08:00
|
|
|
}
|
|
|
|
|
2018-03-17 23:02:21 +08:00
|
|
|
/*
|
|
|
|
I added a simple UI.
|
|
|
|
*/
|
2017-10-28 04:31:19 +08:00
|
|
|
int main()
|
|
|
|
{
|
2018-03-17 22:50:11 +08:00
|
|
|
int num;
|
|
|
|
cout << "\t\tComputes the prime factorization\n\n";
|
|
|
|
cout << "Type in a number: ";
|
2019-08-21 10:10:08 +08:00
|
|
|
cin >> num;
|
2018-03-17 22:50:11 +08:00
|
|
|
|
|
|
|
SieveOfEratosthenes(num);
|
|
|
|
|
|
|
|
prime_factorization(num);
|
|
|
|
|
|
|
|
// Prime factors with their powers in the given number in new line
|
2019-08-21 10:10:08 +08:00
|
|
|
for (auto it : factors)
|
2018-03-17 22:50:11 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
cout << it.first << " " << it.second << endl;
|
2018-03-17 22:50:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-10-28 04:31:19 +08:00
|
|
|
}
|