prime_factorization.cpp File Reference

Prime factorization of positive integers. More...

`#include <algorithm>`
`#include <cstring>`
`#include <iostream>`
`#include <vector>`
Include dependency graph for prime_factorization.cpp:

## Functions

void | SieveOfEratosthenes (int N)
void | prime_factorization (int num)
int | main ()

## Variables

bool | isprime [1000006]
std::vector< int > | prime_numbers
std::vector< std::pair< int, int > > | factors

## Detailed Description

Prime factorization of positive integers.

## Function Documentation

## main()

int main | ( | void |  | ) |

Main program

{
int num;
std::cout << "\t\tComputes the prime factorization\n\n";
std::cout << "Type in a number: ";
std::cin >> num;
SieveOfEratosthenes(num);
prime_factorization(num);
// Prime factors with their powers in the given number in new line
for (auto it : factors) {
std::cout << it.first << " " << it.second << std::endl;
}
return 0;
}
std::cin
std::cout
std::endl
T endl(T... args)
prime_factorization
void prime_factorization(int num)
Definition: prime_factorization.cpp:40
factors
std::vector< std::pair< int, int > > factors
Definition: prime_factorization.cpp:19
SieveOfEratosthenes
void SieveOfEratosthenes(int N)
Definition: prime_factorization.cpp:23
Here is the call graph for this function:

## prime_factorization()

void prime_factorization | ( | int | num | ) |

Prime factorization of a number

{
int number = num;
for (int i = 0; prime_numbers[i] <= num; i++) {
int count = 0;
// termination condition
if (number == 1) {
break;
}
while (number % prime_numbers[i] == 0) {
count++;
number = number / prime_numbers[i];
}
if (count)
factors.push_back(std::make_pair(prime_numbers[i], count));
}
}
std::make_pair
T make_pair(T... args)
prime_numbers
std::vector< int > prime_numbers
Definition: prime_factorization.cpp:16
Here is the call graph for this function:

## SieveOfEratosthenes()

void SieveOfEratosthenes | ( | int | N | ) |

Calculating prime number upto a given range

{
// initializes the array isprime
memset(isprime, true, sizeof isprime);
for (int i = 2; i <= N; i++) {
if (isprime[i]) {
for (int j = 2 * i; j <= N; j += i) isprime[j] = false;
}
}
for (int i = 2; i <= N; i++) {
if (isprime[i])
prime_numbers.push_back(i);
}
}
std::memset
T memset(T... args)
isprime
bool isprime[1000006]
Definition: prime_factorization.cpp:13
std::vector::push_back
T push_back(T... args)
Here is the call graph for this function:

## Variable Documentation

## factors

std::vector<std::pair<int, int> > factors

list of prime factor-pairs

## isprime

bool isprime[1000006]

Declaring variables for maintaing prime numbers and to check whether a number is prime or not

## prime_numbers

std::vector<int> prime_numbers

list of prime numbers