Given a number N, print all the prime factors and their powers. Here N <= 10^18

**Examples :**

Input : 250 Output : 2 1 5 3 Explanation: The prime factors of 250 are 2 and 5. 2 appears once in the prime factorization of and 5 is thrice in it. Input : 1000000000000000000 Output : 2 18 5 18 Explanation: The prime factors of 1000000000000000000 are 2 and 5. The prime factor 2 appears 18 times in the prime factorization. 5 appears 18 times.

We cannot use Sieve’s implementation for a single large number as it requires proportional space. We first count the number of times 2 is the factor of the given number, then we iterate from **3 to Sqrt(n)** to get the number of times a prime number divides a particular number which reduces every time by n/i. We divide our number n (whose prime factorization is to be calculated) by its corresponding smallest prime factor till n becomes 1. And if at the end n>2, it means its a prime number, so we print that particular number.

## C++

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

*chevron_right*

*filter_none*

## Java

`//Java program to print prime ` `// factors and their powers. ` ` ` `class` `GFG { ` ` ` `// function to calculate all the ` `// prime factors and count of ` `// every prime factor ` ` ` `static` `void` `factorize(` `long` `n) { ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// count the number of times 2 divides ` ` ` `while` `(!(n % ` `2` `> ` `0` `)) { ` ` ` `// equivalent to n=n/2; ` ` ` `n >>= ` `1` `; ` ` ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// if 2 divides it ` ` ` `if` `(count > ` `0` `) { ` ` ` `System.out.println(` `"2"` `+ ` `" "` `+ count); ` ` ` `} ` ` ` ` ` `// check for all the possible ` ` ` `// numbers that can divide it ` ` ` `for` `(` `long` `i = ` `3` `; i <= (` `long` `) Math.sqrt(n); i += ` `2` `) { ` ` ` `count = ` `0` `; ` ` ` `while` `(n % i == ` `0` `) { ` ` ` `count++; ` ` ` `n = n / i; ` ` ` `} ` ` ` `if` `(count > ` `0` `) { ` ` ` `System.out.println(i + ` `" "` `+ count); ` ` ` `} ` ` ` `} ` ` ` ` ` `// if n at the end is a prime number. ` ` ` `if` `(n > ` `2` `) { ` ` ` `System.out.println(n + ` `" "` `+ ` `"1"` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` `long` `n = 1000000000000000000L; ` ` ` `factorize(n); ` ` ` `} ` `} ` ` ` `/*This code is contributed by 29AjayKumar*/` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to print prime factors ` `# and their powers. ` `import` `math ` ` ` `# Function to calculate all the prime ` `# factors and count of every prime factor ` `def` `factorize(n): ` ` ` `count ` `=` `0` `; ` ` ` ` ` `# count the number of ` ` ` `# times 2 divides ` ` ` `while` `((n ` `%` `2` `> ` `0` `) ` `=` `=` `False` `): ` ` ` ` ` `# equivalent to n = n / 2; ` ` ` `n >>` `=` `1` `; ` ` ` `count ` `+` `=` `1` `; ` ` ` ` ` `# if 2 divides it ` ` ` `if` `(count > ` `0` `): ` ` ` `print` `(` `2` `, count); ` ` ` ` ` `# check for all the possible ` ` ` `# numbers that can divide it ` ` ` `for` `i ` `in` `range` `(` `3` `, ` `int` `(math.sqrt(n)) ` `+` `1` `): ` ` ` `count ` `=` `0` `; ` ` ` `while` `(n ` `%` `i ` `=` `=` `0` `): ` ` ` `count ` `+` `=` `1` `; ` ` ` `n ` `=` `int` `(n ` `/` `i); ` ` ` `if` `(count > ` `0` `): ` ` ` `print` `(i, count); ` ` ` `i ` `+` `=` `2` `; ` ` ` ` ` `# if n at the end is a prime number. ` ` ` `if` `(n > ` `2` `): ` ` ` `print` `(n, ` `1` `); ` ` ` `# Driver Code ` `n ` `=` `1000000000000000000` `; ` `factorize(n); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to print prime ` `// factors and their powers. ` `using` `System; ` ` ` `public` `class` `GFG ` `{ ` ` ` `// function to calculate all the ` `// prime factors and count of ` `// every prime factor ` `static` `void` `factorize(` `long` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `// count the number of times 2 divides ` ` ` `while` `(! (n % 2 > 0)) ` ` ` `{ ` ` ` `// equivalent to n=n/2; ` ` ` `n >>= 1; ` ` ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// if 2 divides it ` ` ` `if` `(count > 0) ` ` ` `Console.WriteLine(` `"2"` `+ ` `" "` `+count); ` ` ` ` ` `// check for all the possible ` ` ` `// numbers that can divide it ` ` ` `for` `(` `long` `i = 3; i <= (` `long` `) ` ` ` `Math.Sqrt(n); i += 2) ` ` ` `{ ` ` ` `count = 0; ` ` ` `while` `(n % i == 0) { ` ` ` `count++; ` ` ` `n = n / i; ` ` ` `} ` ` ` `if` `(count > 0) ` ` ` `Console.WriteLine(i + ` `" "` `+ count); ` ` ` `} ` ` ` ` ` `// if n at the end is a prime number. ` ` ` `if` `(n > 2) ` ` ` `Console.WriteLine(n +` `" "` `+ ` `"1"` `); ` `} ` ` ` ` ` `// Driver Code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `long` `n = 1000000000000000000; ` ` ` `factorize(n); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to print prime ` `// factors and their powers. ` ` ` `// function to calculate all ` `// the prime factors and count ` `// of every prime factor ` `function` `factorize(` `$n` `) ` `{ ` ` ` `$count` `= 0; ` ` ` ` ` `// count the number of ` ` ` `// times 2 divides ` ` ` `while` `(!(` `$n` `% 2)) ` ` ` `{ ` ` ` `// equivalent to n = n / 2; ` ` ` `$n` `>>= 1; ` ` ` `$count` `++; ` ` ` `} ` ` ` ` ` `// if 2 divides it ` ` ` `if` `(` `$count` `) ` ` ` `echo` `(2 . ` `" "` `. ` `$count` `. ` `"\n"` `); ` ` ` ` ` `// check for all the possible ` ` ` `// numbers that can divide it ` ` ` `for` `(` `$i` `= 3; ` `$i` `<= sqrt(` `$n` `); ` `$i` `+= 2) ` ` ` `{ ` ` ` `$count` `= 0; ` ` ` `while` `(` `$n` `% ` `$i` `== 0) ` ` ` `{ ` ` ` `$count` `++; ` ` ` `$n` `= ` `$n` `/ ` `$i` `; ` ` ` `} ` ` ` `if` `(` `$count` `) ` ` ` `echo` `(` `$i` `. ` `" "` `. ` `$count` `); ` ` ` `} ` ` ` ` ` `// if n at the end is a prime number. ` ` ` `if` `(` `$n` `> 2) ` ` ` `echo` `(` `$n` `. ` `" "` `. 1); ` `} ` ` ` `// Driver Code ` `$n` `= 1000000000000000000; ` `factorize(` `$n` `); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

2 18 5 18

**Time complexity : **O(sqrt(n)).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Maximum number of prime factors a number can have with exactly x factors
- Check if a number exists having exactly N factors and K prime factors
- Find number of factors of N when location of its two factors whose product is N is given
- Number of factors of very large number N modulo M where M is any prime number
- Super Ugly Number (Number whose prime factors are in given set)
- Number with maximum number of prime factors
- Number which has the maximum number of distinct prime factors in the range M to N
- Efficient program to print all prime factors of a given number
- Maximum number of unique prime factors
- Number of steps to convert to prime factors
- C Program for efficiently print all prime factors of a given number
- Java Program for efficiently print all prime factors of a given number
- Product of unique prime factors of a number
- Number of distinct prime factors of first n natural numbers
- Sum of Factors of a Number using Prime Factorization
- Check if all Prime factors of number N are unique or not
- Check if there exists a number with X factors out of which exactly K are prime
- Number less than equals to N with maximum product of prime factors
- Product of divisors of a number from a given list of its prime factors

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.