Number of factors of very large number N modulo M where M is any prime number
Last Updated :
13 Aug, 2021
Given a large number N, the task is to find the total number of factors of the number N modulo M where M is any prime number.
Examples:
Input: N = 9699690, M = 17
Output: 1
Explanation:
Total Number of factors of 9699690 is 256 and (256 % 17) = 1
Input: N = 193748576239475639, M = 9
Output: 8
Explanation:
Total Number of factors of 9699690 is 256 and (256 % 17) = 1
Definition of Factors of a number:
In mathematics, a factor of an integer N also called a divisor of N, is an integer M that may be multiplied by some integer to produce N.
Any number can be written as:
N = (P1A1) * (P2A2) * (P3A3) …. (PnAn)
where P1, P2, P3…Pn are distinct prime and A1, A2, A3…An are number of times the corresponding prime number occurs.
The general formula of total number of factors of a given number will be:
Factors = (1+A1) * (1+A2) * (1+A3) * … (1+An)
where A1, A2, A3, … An are count of distinct prime factors of N.
Here Sieve’s implementation to find prime factorization of a large number cannot be used because it requires proportional space.
Approach:
- Count the number of times 2 is the factor of the given number N.
- Iterate from 3 to √(N) to find the number of times a prime number divides a particular number which reduces every time by N / i.
- Divide number N by its corresponding smallest prime factor till N becomes 1.
- Find the number of factors of the number by using the formula
Factors = (1+A1) * (1+A2) * (1+A3) * … (1+An)
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll mod = 1000000007;
ll mult(ll a, ll b)
{
return ((a % mod) *
(b % mod)) % mod;
}
ll calculate_factors(ll n)
{
ll ans, cnt;
cnt = 0;
ans = 1;
while (n % 2 == 0) {
cnt++;
n = n / 2;
}
if (cnt) {
ans = mult(ans, (cnt + 1));
}
for ( int i = 3; i <= sqrt (n);
i += 2) {
cnt = 0;
while (n % i == 0) {
cnt++;
n = n / i;
}
if (cnt) {
ans = mult(ans, (cnt + 1));
}
}
if (n > 2) {
ans = mult(ans, (2));
}
return ans % mod;
}
int main()
{
ll n = 193748576239475639;
mod = 17;
cout << calculate_factors(n) << endl;
return 0;
}
|
Java
class GFG{
static long mod = 1000000007L;
static long mult( long a, long b)
{
return ((a % mod) *
(b % mod)) % mod;
}
static long calculate_factors( long n)
{
long ans, cnt;
cnt = 0 ;
ans = 1 ;
while (n % 2 == 0 ) {
cnt++;
n = n / 2 ;
}
if (cnt % 2 == 1 ) {
ans = mult(ans, (cnt + 1 ));
}
for ( int i = 3 ; i <= Math.sqrt(n);
i += 2 ) {
cnt = 0 ;
while (n % i == 0 ) {
cnt++;
n = n / i;
}
if (cnt % 2 == 1 ) {
ans = mult(ans, (cnt + 1 ));
}
}
if (n > 2 ) {
ans = mult(ans, ( 2 ));
}
return ans % mod;
}
public static void main(String[] args)
{
long n = 193748576239475639L;
mod = 17 ;
System.out.print(calculate_factors(n) + "\n" );
}
}
|
Python 3
from math import sqrt
mod = 1000000007
def mult(a, b):
return ((a % mod) * (b % mod)) % mod
def calculate_factors(n):
cnt = 0
ans = 1
while (n % 2 = = 0 ):
cnt + = 1
n = n / / 2
if (cnt):
ans = mult(ans, (cnt + 1 ))
for i in range ( 3 , int (sqrt(n)), 2 ):
cnt = 0
while (n % i = = 0 ):
cnt + = 1
n = n / / i
if (cnt):
ans = mult(ans, (cnt + 1 ))
if (n > 2 ):
ans = mult(ans, 2 )
return ans % mod
if __name__ = = '__main__' :
n = 19374857
mod = 17
print (calculate_factors(n))
|
C#
using System;
class GFG{
static long mod = 1000000007L;
static long mult( long a, long b)
{
return ((a % mod) *
(b % mod)) % mod;
}
static long calculate_factors( long n)
{
long ans, cnt;
cnt = 0;
ans = 1;
while (n % 2 == 0) {
cnt++;
n = n / 2;
}
if (cnt % 2 == 1) {
ans = mult(ans, (cnt + 1));
}
for ( int i = 3; i <= Math.Sqrt(n);
i += 2) {
cnt = 0;
while (n % i == 0) {
cnt++;
n = n / i;
}
if (cnt % 2 == 1) {
ans = mult(ans, (cnt + 1));
}
}
if (n > 2) {
ans = mult(ans, (2));
}
return ans % mod;
}
public static void Main(String[] args)
{
long n = 193748576239475639L;
mod = 17;
Console.Write(calculate_factors(n) + "\n" );
}
}
|
Javascript
<script>
mod = 1000000007;
function mult( a , b)
{
return ((a % mod) *
(b % mod)) % mod;
}
function calculate_factors( n)
{
var ans, cnt;
cnt = 0;
ans = 1;
while (n % 2 == 0) {
cnt++;
n = n / 2;
}
if (cnt % 2 == 1) {
ans = mult(ans, (cnt + 1));
}
for (i = 3; i <= Math.sqrt(n);
i += 2) {
cnt = 0;
while (n % i == 0) {
cnt++;
n = n / i;
}
if (cnt % 2 == 1) {
ans = mult(ans, (cnt + 1));
}
}
if (n > 2) {
ans = mult(ans, (2));
}
return ans % mod;
}
var n = 193748576239475639;
mod = 17;
document.write(calculate_factors(n));
</script>
|
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...