Prime factors of a big number
Last Updated :
08 Jun, 2022
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 it’s a prime number, so we print that particular number.
C++
#include <bits/stdc++.h>
using namespace std;
void factorize(long long n)
{
int count = 0;
while (!(n % 2)) {
n >>= 1;
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;
}
|
Java
class GFG {
static void factorize(long n) {
int count = 0;
while (!(n % 2 > 0)) {
n >>= 1;
count++;
}
if (count > 0) {
System.out.println("2" + " " + count);
}
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 > 2) {
System.out.println(n + " " + "1");
}
}
public static void main(String[] args) {
long n = 1000000000000000000L;
factorize(n);
}
}
|
Python3
import math
def factorize(n):
count = 0;
while ((n % 2 > 0) == False):
n >>= 1;
count += 1;
if (count > 0):
print(2, count);
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 > 2):
print(n, 1);
n = 1000000000000000000;
factorize(n);
|
C#
using System;
public class GFG
{
static void factorize(long n)
{
int count = 0;
while (! (n % 2 > 0))
{
n >>= 1;
count++;
}
if (count > 0)
Console.WriteLine("2" + " " +count);
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 > 2)
Console.WriteLine(n +" " + "1" );
}
static public void Main ()
{
long n = 1000000000000000000;
factorize(n);
}
}
|
PHP
<?php
function factorize($n)
{
$count = 0;
while (!($n % 2))
{
$n >>= 1;
$count++;
}
if ($count)
echo(2 . " " . $count . "\n");
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 > 2)
echo($n . " " . 1);
}
$n = 1000000000000000000;
factorize($n);
?>
|
Javascript
<script>
function factorize(n)
{
var count = 0;
while ((n % 2)==0) {
n = parseInt(n/2)
count++;
}
if (count)
document.write( 2 + " " + count + "<br>");
for (var i = 3; i <= parseInt(Math.sqrt(n)); i += 2) {
count = 0;
while (n % i == 0) {
count++;
n = parseInt(n / i);
}
if (count!=0)
document.write( i + " " + count + "<br>");
}
if (n > 2)
document.write( n + " " + 1 + "<br>");
}
var n = 1000000000000000000;
factorize(n);
</script>
|
Output:
2 18
5 18
Time Complexity: O(sqrt(N)), as we are using a loop to traverse sqrt(N) times.
Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...