Finding all the prime factors of a number I want to calculate all the prime factors of a number in the range 2 ≤ N ≤ 107. Here is my function:

public static long cal_me(int a, long[] A){

     long ans=1;
     int count=0;
     while(a%2==0){ 
         a/=2;
       count++;
     }
     if(count!=0)  // I am calculating value which depend on Number of prime Number
     ans = (long) ((Math.pow(2,2*count+1)+1)/3);


     if(A[a]!=-1) return ans*A[a];

     for(int i=3;i&lt;=Math.sqrt(a) &amp;&amp; a!=1;i+=2)
     {
         if(A[a]!=-1){ 

             return ans*A[a];
         }
         count=0;
           while(a%i==0){ 
               a/=i;
               count++;

           }



           if(count!=0)
               ans *=(long) ((Math.pow(i,2*count+1)+1)/(i+1));



     }

     if(a&gt;2)
        ans *=  (long) ((Math.pow(a,3)+1)/(a+1));

     return ans;
}


My calling function:

for(int i=2;i&lt;=10000000;i++)
A[i] = cal_me(i,A);


The above program me took 10s. I want something faster — how should I improve this?
Quick observation: you use Math.pow() to calculate powers of two. This is highly inefficient, use bit shifting to prevent long -> double conversion

For example, 2^10 can be calculated very quicky as 1 &lt;&lt; 10. See for example here (https://stackoverflow.com/q/141525/461499) 

Also you divide a double by a long and then convert to long (in effect flooring), you can skip conversion to double entirely to speed things up.