
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all prime factors and their powers in C++
In this problem, we are given a number N, and we have to find all unique prime factors and their powers that divide the number.
Let’s take an example to understand the topic −
Input: 55 Output: 5 power 1 11 power 1
Explanation −
55 is divisible by 5 and 11.
To solve this problem, an easy approach to solving the problem is to find prime factors of N. And then find power of the prime number that divides the number N and print it.
Algorithm
Efficient Approach
Step 1: Find an array s[N+1]. s[i] = prime factor of i dividing N. Step 2: Find all powers of i. prime = s[N] and pow = 1. Step 3: While (N > 1) : Step 3.1: N /= s[N]; Step 3.2: if(prime = s[N]) : pow++ Step 4: print prime and pow.
Example
#include<bits/stdc++.h> using namespace std; void primes(int N, int s[]){ vector <bool> prime(N+1, false); for (int i=2; i<=N; i+=2) s[i] = 2; for (int i=3; i<=N; i+=2){ if (prime[i] == false){ s[i] = i; for (int j=i; j*i<=N; j+=2){ if (prime[i*j] == false){ prime[i*j] = true; s[i*j] = i; } } } } } void generatePrimeFactors(int N) { int s[N+1]; primes(N, s); cout<<"Factor\tPower"<<endl; int prime = s[N]; int power = 1; while (N > 1){ N /= s[N]; if (prime == s[N]){ power++; continue; } cout<<prime<<"\t"<<power<<endl; prime = s[N]; power = 1; } } int main() { int N = 55; cout<<"The prime factors are and their powers are :\n"; generatePrimeFactors(N); return 0; }
Output
The prime factors are and their powers are −
Factor Power 5 1 11 1
- Related Articles
- C Program for efficiently print all prime factors of a given number?
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X in C++
- Print all combinations of factors in C++
- Count numbers in a range having GCD of powers of prime factors equal to 1 in C++
- Python Program for Efficient program to print all prime factors of a given number
- Print all integers that are sum of powers of two given numbers in C++
- Find all prime factors of a number - JavaScript
- Print all Prime Quadruplet of a number less than it in C++
- Print all prime numbers less than or equal to N in C++
- Find all the prime factors of 1729 and arrange them in ascending order. Now state the relation, if any; between two consecutive prime factors.
- Print all Semi-Prime Numbers less than or equal to N in C++
- Prime factors in java
- Prime factors of a big number in C++
- Maximum number of unique prime factors in C++
- Print powers using Anonymous Function in Python?

Advertisements