Prime factors of a big number | CPP program to print prime factors and their powers . ; function to calculate all the prime factors and count of every prime factor ; count the number of times 2 divides ; n >>= 1 ; equivalent to n = n / 2 ; ; if 2 divides it ; check for all the possible numbers that can divide it ; if n at the end is a prime number . ; driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void factorize ( long long n ) { int count = 0 ; while ( ! ( n % 2 ) ) { count ++ ; } if ( count ) cout << 2 << " ▁ " << 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 << " ▁ " << count << endl ; } if ( n > 2 ) cout << n << " ▁ " << 1 << endl ; } int main ( ) { long long n = 1000000000000000000 ; factorize ( n ) ; return 0 ; }