Given a number N, print all the prime factors and their powers. Here N <= 10^18
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.
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.
- Check if a number exists having exactly N factors and K prime factors
- Maximum number of prime factors a number can have with exactly x factors
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Sum of Factors of a Number using Prime Factorization
- Number which has the maximum number of distinct prime factors in the range M to N
- Check if all Prime factors of number N are unique or not
- Number of steps to convert to prime factors
- Generate K co-prime pairs of factors of a given number
- Check if there exists a number with X factors out of which exactly K are prime
- Maximum number of unique prime factors
- Product of unique prime factors of a number
- Super Ugly Number (Number whose prime factors are in given set)
- Number of factors of very large number N modulo M where M is any prime number
- Product of divisors of a number from a given list of its prime factors
- Efficient program to print all prime factors of a given number
- Number of distinct prime factors of first n natural numbers
- Number less than equals to N with maximum product of prime factors
- C Program for efficiently print all prime factors of a given number
- Number with maximum number of prime factors
- Java Program for efficiently print all prime factors of a given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.