TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
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:

Go to the source code of this file.

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.

Definition in file prime_factorization.cpp.

Function Documentation

◆ main()

int main ( void )

Main program

Definition at line 62 of file prime_factorization.cpp.

62 {
63 int num;
64 std::cout << "\t\tComputes the prime factorization\n\n";
65 std::cout << "Type in a number: ";
66 std::cin >> num;
67
69
71
72 // Prime factors with their powers in the given number in new line
73 for (auto it : factors) {
74 std::cout << it.first << " " << it.second << std::endl;
75 }
76
77 return 0;
78}
void prime_factorization(int num)
std::vector< std::pair< int, int > > factors
void SieveOfEratosthenes(int N)

◆ prime_factorization()

void prime_factorization ( int num)

Prime factorization of a number

Definition at line 40 of file prime_factorization.cpp.

40 {
41 int number = num;
42
43 for (int i = 0; prime_numbers[i] <= num; i++) {
44 int count = 0;
45
46 // termination condition
47 if (number == 1) {
48 break;
49 }
50
51 while (number % prime_numbers[i] == 0) {
52 count++;
53 number = number / prime_numbers[i];
54 }
55
56 if (count)
57 factors.push_back(std::make_pair(prime_numbers[i], count));
58 }
59}
std::vector< int > prime_numbers

◆ SieveOfEratosthenes()

void SieveOfEratosthenes ( int N)

Calculating prime number upto a given range

Definition at line 23 of file prime_factorization.cpp.

23 {
24 // initializes the array isprime
25 memset(isprime, true, sizeof isprime);
26
27 for (int i = 2; i <= N; i++) {
28 if (isprime[i]) {
29 for (int j = 2 * i; j <= N; j += i) isprime[j] = false;
30 }
31 }
32
33 for (int i = 2; i <= N; i++) {
34 if (isprime[i])
35 prime_numbers.push_back(i);
36 }
37}
bool isprime[1000006]
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....

Variable Documentation

◆ factors

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

list of prime factor-pairs

Definition at line 19 of file prime_factorization.cpp.

◆ isprime

bool isprime[1000006]

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

Definition at line 13 of file prime_factorization.cpp.

◆ prime_numbers

std::vector<int> prime_numbers

list of prime numbers

Definition at line 16 of file prime_factorization.cpp.