- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Hardy-Ramanujan Theorem
The Hardy−Ramanujan Theorem states that the number of distinct prime factors of any natural number N will be approximately equal to the value of $\mathrm{\log(\log N)}$ for most of the cases.
For example, let’s consider N to be 1000.
The number of distinct prime factors of 15 are 2 and 5 i.e. 2 distinct prime factors.
The value of $\mathrm{\log_{e}(\log_{e}(1000))}$ is equal to 1.932 which is approximately equal to 2.
The Hardy−Ramanujan theorem is proved in the above case.
Since the theorem states that the number of distinct prime factors will be approximately equal to $\mathrm{\log(\log(N))}$ for most of the cases. There are cases where this theorem does not imply.
For example, N=286
The number of distinct prime factors of N i.e. 286 are 2, 11, and 13. There are 3 distinct prime factors of 286.
According to Hardy−Ramanujan theorem, the number of distinct prime factors of N will be approximately equal to $\mathrm{\log_{e}(\log_{e}(286))=1.732}$
We will implement the theorem in our C++ program to check for every natural number by calculating its distinct prime factors and the value of $\mathrm{\log(\log(N))}$ and check if they are approximately equal.
Implementation of Hardy−Ramanujan theorem in C++
To check the theorem for any natural number N, we will simply calculate all the distinct prime factors of N by creating a function to find the distinct prime factors of any number. We will count the number of distinct prime factors of N and then calculate the approximate number of distinct prime factors of N according to the Hardy−Ramanujan theorem i.e. equal to the value of $\mathrm{\log(\log(N))}$.
The number of distinct prime factors of N can be calculated using the log() function in C++ which returns the natural logarithmic value of any positive integer passed into it.
Syntax
int N; int value=log(N);
To compare the approximate value of the number of distinct prime factors of any number according to Hardy−Ramanujan theorem with the actual number of distinct prime factors, we will create a function to count the number of distinct prime factors of any number.
We will check if the number N is divisible by 2, we will divide N by 2 until it gives 0 remainder and increase the count of distinct prime factors by 1. Now we will iterate in a for loop from i=3 to i<=sqrt(N) to check for every odd value of i as N will be odd at this time. If N is divisible by i, we will divide N by i until it gives remainder as 0 and update N everytime. Similarly we will iterate and check for every value of i until it is less than or equal to the square root of N and update count by 1 for each iteration when i divides N.
Now for the case when N is greater than 2 and is a prime number, we will increase the count by 1 to get the exact number of distinct prime factors of N.
We will print the exact number of distinct prime factors calculated and approximate number of distinct prime factors of N according to Hardy−Ramanujan theorem to check the theorem for any natural number N.
The steps to implement the approach in C++:
We will make a function to get the exact number of distinct prime factors of N which will be the user input as discussed above.
We will print the exact number of distinct prime factors of N.
Now we will calculate the approximate number of distinct prime factors of N by calculating the value of $\mathrm{\log(\log(N))}$ and store it in a variable.
Print the approximate number of distinct prime factors of N according to the Hardy−Ramanujan theorem.
Example
//C++ code for the Hardy-Ramanujan theorem #include <bits/stdc++.h> using namespace std; //function to count exact number of distinct prime factors of N int exact_count(int N){ int a=0; //to store the count of distinct prime factors if(N%2==0){ //if N is divisible by 2 a++; //increase the count by 1 while(N%2==0){ //divide N by 2 until it gives remainder as 0 N = N/2; } } //N must be odd now so checking for odd values of i for(int i=3;i<=sqrt(N);i=i+2){ if(N%i==0){ //if N is divisible by i a++; //increase the count by 1 while(N%i==0){ //divide N by i until it gives 0 remainder N = N/i; } } } //for the case when N will be prime number greater than 2 if(N>2){ a++; } return a; //return the count of prime factors } int main() { int N=39573; //for taking input double approx; //for calculating approximate no of distinct prime factors approx = log(log(N)); //according to Hardy-Ramanujan theorem //printing exact no of distinct prime factors cout<<"Exact no. of distinct prime factors : "<<exact_count(N)<<endl; cout<<"No. of distinct prime factors acc. to Hardy-Ramanujan theorem : "<<approx<<endl; return 0; }
Output
Exact no. of distinct prime factors : 2 No. of distinct prime factors acc. to Hardy−Ramanujan theorem : 2.35952
Time complexity− O(sqrt(N)) , time taken to calculate exact number of distinct prime factors.
Space complexity − O(1) , because we didn’t use any extra space.
Conclusion
The Hardy−Ramanujan theorem and its different aspects were discussed in the article. We use the concept of the theorem to see the number of distinct prime factors of any natural number N and check the accountability of the theorem.
I hope you understand the topic better after reading this article.