- Related Questions & Answers
- Add minimum number to an array so that the sum becomes even in C programming
- C/C++ Program for nth Catalan Number?
- C Program to Check if all digits of a number divide it
- Concatenate a string given number of times in C++ programming
- Superperfect Number in C programming
- Super Prime in c programming
- HTML DOM Input Number value Property
- HTML DOM Input Number type Property
- How to find the number of days in a month of a particular year in Java?
- Number of elements a particular tag contains in JavaScript?
- Print multiples of Unit Digit of Given Number in C Program
- C/C++ Programming to Count trailing zeroes in factorial of a number?
- C++ Programming for Smallest K digit number divisible by X?
- To find sum of even factors of a number in C++ Program?
- Generating random number list in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we are given an integer N <= 10^18. Our task is to print all prime factors of the number along with there frequency of occurrence.

Let’s take an example to understand the problem,

Input: 100 Output: 2 2 5 2 Explanation: prime factorization of 100 = 2 * 2 * 5 * 5.

To solve this problem, we will have to find the prime factors of the number and then calculate their frequencies.

For this, we will find check the frequency of 2 as a factor and divide the number by 2. Then check from 3 to square root n. divide and increase the frequency of each prime number that is a factor of the number. And stop if the number becomes 1. Then print all primes with there frequencies.

The below code shows the implementation of our solution,

#include <iostream> #include <math.h> using namespace std; void factorize(long long n){ int count = 0; while (!(n % 2)) { n/= 2; count++; } if (count) cout<<2<<"\t"<<count<<endl; for (long long i = 3; i <= sqrt(n); i += 2) { count = 0; while (n % i == 0) { count++; n = n / i; } if (count) cout<<i<<"\t"<<count<<endl; } if (n > 2) cout<<n<<"\t"<<1<<endl; } int main() { long long N = 21000; cout<<"The prime factors and their frequencies of the number "<<N<<" are \n"; factorize(N); return 0; }

The prime factors and their frequencies of the number 21000 are 2 3 3 1 5 3 7 1

Advertisements