java - Effective way of find Total number of even divisors of the number -



i have find number of number of divisor of given number. tried.i getting correct output getting time complexity more required.
question :- first line contains number of testcases t, followed t lines each containing integer n
output should - for each testcase, print required answer in single line.
how can reduce complexity given code.or can please suggest more efficient way...

import java.io.bufferedreader; import java.io.inputstreamreader;  public class testclass {     public static void main(string args[]) throws exception {         // read input stdin , provide input before running         string frt = "";         bufferedreader br = new bufferedreader(new inputstreamreader(system.in));         string line = br.readline();             int t = integer.parseint(line);              int[] inp = new int[t];         (int = 0; < t; i++) {                        int x = integer.parseint(br.readline());             inp[i] = x;          }         int[] ans = new int[t];         int count = 1;         (int = 0; < t; i++) {             int x = inp[i];             if (x % 2 == 0) {                 (int j = 2; j <= x / 2; j = j + 2) {                     if (x % j == 0)                         count++;                 }             } else                 count = 0;             ans[i] = count;         }         (int = 0; < t; i++)             system.out.println(ans[i]);     }  } 

 import java.io.*;   class ideone  {     public static void main (string[] args) throws exception     {        bufferedreader br = new bufferedreader(new inputstreamreader(system.in));       int t = integer.parseint(br.readline());       int i,j,k,n;        int[] inp = new int[t];       (i = 0; < t; i++) {       inp[i] = integer.parseint(br.readline());       }       //find primes numbers till square-root of 10^9.      int max, root, arrlen;      max=1000000000;      arrlen=(int)math.sqrt(max);  // arrlen=31622       boolean[] primes=new boolean[arrlen+2]; // no need find primes numbers till max       primes[0]=primes[1]=false;      for(i=2;i<arrlen;i++)            primes[i]=true;        // using sieve of eratosthenes     // square root of 31622 177.8     root=(int)math.sqrt(arrlen); // root=177     for(i=2;i<=root;i++)     {        if(primes[i])        {            n=i*i;            k=0;             //arrlen length of primes array.            for(j=n; j<arrlen; k+=1, j=n+(i*k))                   primes[j]=false;        }    }      int[] ans = new int[t];     for( = 0; < t; i++) {          n = inp[i];           if(n%2==1)          {              ans[i]=0; // odd numbers have 0 divisors          }          else          {            int[] facts=new int[50];           for(k=0;k<50;k++)               facts[k]=1;          facts[0]=0; // fact[0] contain highest power of 2 divides n.         while(n%2==0)         {              facts[0]+=1;              n=n/2;         }                     // prime factorizing n         j=1;         for( k=3; k<arrlen; k+=2)         {             if(primes[k] && n%k==0)             {                 while(n%k==0)                {                     facts[j]+=1;                     n=n/k;                }                j+=1;             }             if(n==1)  // check if n has been divided or not.                break;        }         if(n!=1) // check if there prime factor greater square root of max.        {             facts[j]+=1;             j+=1;        }         int count=1;        for(k=0;k<j;k++)             count=count*facts[k];          ans[i]=count;        } }     ( = 0; < t; i++)            system.out.println(ans[i]);       }  } 

i of feeling question might have been posted on competitive coding platform, hackerearth. if so, please don't post direct questions on stackoverflow(in opinion). anyways, have tested code , runs correctly. in questions not able reduce time complexity, first make sure unnecessary objects not created. object creation in memory time consuming operation. avoid irrelevant creation of object , variables. code above can still optimized, reduce it's readability. :)

also before approaching problem, try figure out various test cases. odd numbers have 0 divisors. checking whether number odd, can reduce several operations.

a bit more explanation above code: number of divisor of number are: (n1+1)(n2+1)(n3+1).... n1,n2,n3 etc powers of prime multiples of number. if n1 2(the prime number), number of divisors of number are: n1*(n2+1)*(n3+1)...

in facts[] array, facts[0] corresponds n1, while n2, n3 etc stored in facts[1],facts[2], etc. facts[0] initialized 0, while others initialized 1. count stores final product: n1*(n2+1)*(n3+1)... equal number of divisors of original number.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -