Hardy-Ramanujan Theorem
Last Updated :
14 Feb, 2023
Hardy Ramanujam theorem states that the number of prime factors of n will approximately be log(log(n)) for most natural numbers n
Examples :
5192 has 2 distinct prime factors and log(log(5192)) = 2.1615
51242183 has 3 distinct prime facts and log(log(51242183)) = 2.8765
As the statement quotes, it is only an approximation. There are counter examples such as
510510 has 7 distinct prime factors but log(log(510510)) = 2.5759
1048576 has 1 prime factor but log(log(1048576)) = 2.62922
This theorem is mainly used in approximation algorithms and its proof lead to bigger concepts in probability theory.
C++
#include <bits/stdc++.h>
using namespace std;
int exactPrimeFactorCount( int n)
{
int count = 0;
if (n % 2 == 0) {
count++;
while (n % 2 == 0)
n = n / 2;
}
for ( int i = 3; i <= sqrt (n); i = i + 2) {
if (n % i == 0) {
count++;
while (n % i == 0)
n = n / i;
}
}
if (n > 2)
count++;
return count;
}
int main()
{
int n = 51242183;
cout << "The number of distinct prime factors is/are "
<< exactPrimeFactorCount(n) << endl;
cout << "The value of log(log(n)) is "
<< log ( log (n)) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int exactPrimeFactorCount( int n)
{
int count = 0 ;
if (n % 2 == 0 ) {
count++;
while (n % 2 == 0 )
n = n / 2 ;
}
for ( int i = 3 ; i <= Math.sqrt(n); i = i + 2 )
{
if (n % i == 0 ) {
count++;
while (n % i == 0 )
n = n / i;
}
}
if (n > 2 )
count++;
return count;
}
public static void main (String[] args)
{
int n = 51242183 ;
System.out.println( "The number of distinct "
+ "prime factors is/are "
+ exactPrimeFactorCount(n));
System.out.println( "The value of log(log(n))"
+ " is " + Math.log(Math.log(n))) ;
}
}
|
Python3
import math
def exactPrimeFactorCount(n) :
count = 0
if (n % 2 = = 0 ) :
count = count + 1
while (n % 2 = = 0 ) :
n = int (n / 2 )
i = 3
while (i < = int (math.sqrt(n))) :
if (n % i = = 0 ) :
count = count + 1
while (n % i = = 0 ) :
n = int (n / i)
i = i + 2
if (n > 2 ) :
count = count + 1
return count
n = 51242183
print ( "The number of distinct prime factors is/are {}" .
format (exactPrimeFactorCount(n), end = "\n" ))
print ( "The value of log(log(n)) is {0:.4f}"
. format (math.log(math.log(n))))
|
C#
using System;
class GFG {
static int exactPrimeFactorCount( int n)
{
int count = 0;
if (n % 2 == 0) {
count++;
while (n % 2 == 0)
n = n / 2;
}
for ( int i = 3; i <= Math.Sqrt(n);
i = i + 2)
{
if (n % i == 0) {
count++;
while (n % i == 0)
n = n / i;
}
}
if (n > 2)
count++;
return count;
}
public static void Main ()
{
int n = 51242183;
Console.WriteLine( "The number of"
+ " distinct prime factors is/are "
+ exactPrimeFactorCount(n));
Console.WriteLine( "The value of "
+ "log(log(n)) is "
+ Math.Log(Math.Log(n))) ;
}
}
|
PHP
<?php
function exactPrimeFactorCount( $n )
{
$count = 0;
if ( $n % 2 == 0)
{
$count ++;
while ( $n % 2 == 0)
$n = $n / 2;
}
for ( $i = 3; $i <= sqrt( $n ); $i = $i + 2)
{
if ( $n % $i == 0)
{
$count ++;
while ( $n % $i == 0)
$n = $n / $i ;
}
}
if ( $n > 2)
$count ++;
return $count ;
}
$n = 51242183;
echo "The number of distinct prime" .
" factors is/are " ,exactPrimeFactorCount( $n ), "\n" ;
echo "The value of log(log(n)) " .
"is " ,log(log( $n )), "\n" ;
?>
|
Javascript
<script>
function exactPrimeFactorCount(n)
{
let count = 0;
if (n % 2 == 0)
{
count++;
while (n % 2 == 0)
n = n / 2;
}
for (let i = 3; i <= Math.sqrt(n); i = i + 2)
{
if (n % i == 0)
{
count++;
while (n % i == 0)
n = n / i;
}
}
if (n > 2)
count++;
return count;
}
let n = 51242183;
document.write( "The number of distinct prime factors is/are " +
exactPrimeFactorCount(n) + "<br>" );
document.write( "The value of log(log(n)) is " + Math.log(Math.log(n)) + "<br>" );
</script>
|
Output:
The number of distinct prime factors is/are 3
The value of log(log(n)) is 2.8765
Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...