Find the number of divisors of a given number
Formula: (p1+1)*(p2+1)*(p3+1)*...(pn+1)
where pi is the count of different prime factors of the given number.
Example 1:
Input : num = 24
Output: 8
Approach:
Java
public class FindNumberofDivisors {public static void main(String[] args) {int num = 24;int divisors = divisorsCount(num);System.out.println("Number of divisors " + divisors);}// function to find the number of// divisors of given number// formula of number of divisors// (p1+1)*(p2+1)*(p3+1)*...(pn+1)// where pi are the count of different prime// factors of the given numberprivate static int divisorsCount(int num) {// initialize the divisors as 1int divisors = 1;int count = 0;// count the number of// times 2 divides the numberwhile (num % 2 == 0) {count++;num = num / 2;}// update the divisors// formula =divisors*(count+1)divisors = divisors * (count + 1);// now iterare for other numbersfor (int i = 3; i * i <= num; i += 2) {count = 0;// while the number is divisible// by current prime count// the no of times it divideswhile (num % i == 0) {num = num / i;count++;}// update the divisors// formula =divisors*(count+1)divisors = divisors * (count + 1);}// if number not become 1// then it is prime// then update the answerif (num > 1)divisors = divisors * 2;// return the count of divisorsreturn divisors;}}
C++
#include <bits/stdc++.h>using namespace std;//function to find the number of// divisors of given number//formula of number of divisors// (p1+1)*(p2+1)*(p3+1)*...(pn+1)//where pi are the count of different prime//fatcors of the given numberint divisorsCount(int num){//initialize the divisors as 1int divisors=1;int count=0;//count the number of//times 2 divides the numberwhile(num%2==0){count++;num=num/2;}//update the divisors//formula =divisors*(count+1)divisors=divisors*(count+1);//now iterare for other numbersfor(int i=3;i*i<=num;i+=2){count=0;//while the number is divisible//by current prime count//the no of times it divideswhile(num%i==0){num=num/i;count++;}//update the divisors//formula =divisors*(count+1)divisors=divisors*(count+1);}//if number not become 1//then it is prime//then update the answerif(num>1)divisors=divisors*2;//return the count of divisorsreturn divisors;}int main(){int num=24;int divisors=divisorsCount(num);cout<<"Number of divisors ";cout<<divisors<<"\n";}
C
#include <stdio.h>//function to find the number of// divisors of given number//formula of number of divisors// (p1+1)*(p2+1)*(p3+1)*...(pn+1)//where pi are the count of different prime//fatcors of the given numberint divisorsCount(int num){//initialize the divisors as 1int divisors = 1;int count = 0;int i;//count the number of//times 2 divides the numberwhile (num % 2 == 0){count++;num = num / 2;}//update the divisors//formula =divisors*(count+1)divisors = divisors * (count + 1);//now iterare for other numbersfor (i = 3; i * i <= num; i += 2){count = 0;//while the number is divisible//by current prime count//the no of times it divideswhile (num % i == 0){num = num / i;count++;}//update the divisors//formula =divisors*(count+1)divisors = divisors * (count + 1);}//if number not become 1//then it is prime//then update the answerif (num > 1)divisors = divisors * 2;//return the count of divisorsreturn divisors;}int main(){int num = 24;int divisors = divisorsCount(num);printf("%d\n", divisors);return 0;}
No comments:
Post a Comment