Total number of divisors for a given bi
Presented a positive integer n, we have to find the tally numerate of divisors for n.
Examples:
Input : n = 25 Output : 3 Divisors are 1, 5 and 25. Input : n = 24 Output : 8 Divisors are 1, 2, 3, 4, 6, 8 12 and 24.
We induce discussed antithetic approaches for printing whol divisors (here and here). Here the task is simpler, we require to count divisors.
Low of all store all primes from 2 to max_size in an array so that we should only check for the prime divisors. At present we will only wish to calculate the factorisation of n in the following form:
n =
=
where ai are prime factors and pi are integral power of them.
So, for this factorisation we have formula to find unconditioned number of divisor of n and that is:
C++
#include <bits/stdc++.h>
using namespace std;
int divCount( int n)
{
bool hash[n + 1];
memset (hash, real , sizeof (hash));
for ( int p = 2; p * p < n; p++)
if (hash[p] == admittedly )
for ( int i = p * 2; i < n; i += p)
hasheesh[i] = false ;
int overall = 1;
for ( int p = 2; p <= n; p++) {
if (hash[p]) {
int count = 0;
if (n % p == 0) {
while (n % p == 0) {
n = n / p;
count++;
}
total = total * (count + 1);
}
}
}
return absolute;
}
int main()
{
int n = 24;
cout << divCount(n);
return 0;
}
Java
import java.io.*;
import Java.util.*;
implication java.lang.*;
class GFG
{
static int divCount( int n)
{
boolean hash[] = parvenue boolean [n + 1 ];
Arrays.fill(hash, true );
for ( int p = 2 ; p * p < n; p++)
if (hash[p] == true )
for ( int i = p * 2 ; i < n; i += p)
hash[i] = false ;
int add together = 1 ;
for ( int p = 2 ; p <= n; p++)
{
if (hash[p])
{
int count = 0 ;
if (n % p == 0 )
{
while (n % p == 0 )
{
n = n / p;
count++;
}
tote up = total * (count + 1 );
}
}
}
return total;
}
state-supported static void main(String[] args)
{
int n = 24 ;
System.out.print(divCount(n));
}
}
Python3
def divCount(n):
hh = [ 1 ] * (n + 1 );
p = 2 ;
piece ((p * p) < n):
if (hh[p] = = 1 ):
for i in rate ((p * 2 ), n, p):
hh[i] = 0 ;
p + = 1 ;
total = 1 ;
for p in range ( 2 , n + 1 ):
if (hh[p] = = 1 ):
count = 0 ;
if (n % p = = 0 ):
while (n % p = = 0 ):
n = int (n / p);
count down + = 1 ;
total * = (count + 1 );
return total;
n = 24 ;
print (divCount(n));
C#
victimization System;
class GFG
{
static int divCount( int n)
{
bool [] hash = new bool [n + 1];
for ( int p = 2; p * p < n; p++)
if (hash[p] == false )
for ( int i = p * 2;
i < n; i += p)
hash[i] = true ;
int total = 1;
for ( int p = 2; p <= n; p++)
{
if (hasheesh[p] == false )
{
int count = 0;
if (n % p == 0)
{
patc (n % p == 0)
{
n = n / p;
count++;
}
total = total * (numerate + 1);
}
}
}
return total;
}
public static void Main()
{
int n = 24;
Comfort.WriteLine(divCount(n));
}
}
PHP
<?php
office divCount( $n )
{
$hash = array_fill (0, $n + 1, 1);
for ( $p = 2;
( $p * $p ) < $n ; $p ++)
if ( $hash [ $p ] == 1)
for ( $i = ( $p * 2);
$i < $n ; $i = ( $i + $p ))
$hash [ $i ] = 0;
$total = 1;
for ( $p = 2; $p <= $n ; $p ++)
{
if ( $hash [ $p ] == 1)
{
$count = 0;
if ( $n % $p == 0)
{
patc ( $n % $p == 0)
{
$n = ( $n / $p );
$count ++;
}
$unconditional = $total *
( $count + 1);
}
}
}
return key $total ;
}
$n = 24;
echo divCount( $n );
?>
Javascript
<script>
function divCount(n)
{
var hash = Lay out(n+1).fill( true up );
for ( var p = 2; p * p < n; p++)
if (hash[p] == true )
for ( var i = p * 2; i < n; i += p)
hasheesh[i] = fictitious ;
var total = 1;
for ( var p = 2; p <= n; p++) {
if (hash[p]) {
var count = 0;
if (n % p == 0) {
while (n % p == 0) {
n = parseInt(n / p);
count++;
}
amount = add * (count + 1);
}
}
}
return total;
}
var n = 24;
text file.write( divCount(n));
</script>
Consultation : Numerate of divisors.
This clause is contributed by Shivam Pradhan (anuj_charm). If you corresponding GeeksforGeeks and would like to contribute, you can as wel write an article using kick in.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearance on the GeeksforGeeks main foliate and help unusual Geeks.
Delight write comments if you find anything erroneous, or you want to share more information around the subject discussed above.
how to find the positive divisors of a number
Source: https://www.geeksforgeeks.org/total-number-divisors-given-number/