<p>How to reduce your calculation to 1 microsecond? Easy.</p>

<p>Your grand total is : sum of the n square roots - sum of the n cubic roots</p>

<p>Math tips : </p>

<pre><code>double sumOfSquareRoots = 2D * Math.pow((n + 0.5D), 1.5D) / 3D - 0.22474487139;
</code></pre>

<p>See <a href="https://arxiv.org/pdf/1204.0877.pdf" rel="nofollow noreferrer">https://arxiv.org/pdf/1204.0877.pdf</a> for cubic roots :)</p>
java - How can i make this calculation faster?
<p>My function is <code>total += Math.sqrt(num) - Math.cbrt(num);</code> I want to apply this to every number till the max value(starting from 0) i determine. So i wrote some code  below which uses divide technique to calculate faster. How can i fasten this calculation? Code below(<em>8 threads</em>) takes <strong>20 seconds</strong> to finish while non-thread takes <strong>150 seconds</strong> to finish. I believe with <code>forkjoinpool</code> i can make it faster or maybe <code>parallel streams</code>? How to implement it with them?</p>

<pre><code>public class Main {

    private static int targetNum = Integer.MAX_VALUE;
    private static int threadCount = 8;
    private static double total = 0;
    public static void main(String[] args) {
    // write your code here
        DecimalFormat df2 = new DecimalFormat(".##");
        long time = System.currentTimeMillis();


        ExecutorService executor = Executors.newFixedThreadPool(threadCount);

        try {
            ArrayList&lt;Future&lt;Double&gt;&gt; futureList = new ArrayList&lt;&gt;();
            for(int a = 0; a &lt; threadCount; a++){
                calculatorService ss = new calculatorService(a*(targetNum/threadCount) ,(a+1) *(targetNum/threadCount));
                futureList.add(executor.submit(ss));
            }
            for(int a = 0; a &lt; threadCount; a++){
                total+= futureList.get(a).get();
            }

            System.out.println("Result= "+ df2.format(total) + "\nTime passed= " + ((System.currentTimeMillis() - time)/1000f));
            executor.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
class calculatorService implements Callable&lt;Double&gt;{

    private int start,end;

    public SqrtSummer(int start, int end) {
        this.start = start;
        this.end = end;
    }

    @Override
    public Double call(){
        double total = 0;
        for (int a = start; a &lt; end; a++) {
            total += Math.sqrt(a) - Math.cbrt(a);
        }
        return total;
    }
}
</code></pre>

<p><strong>Edit 1</strong></p>

<p><code>futureList.get(a).get();</code>i had to do that in that way because i don't know the thread(core) count. Thus i can not write futureList.get(0).get() + futureList.get(1).get()..... I know till futureList.get(0).get() loop will wait but still they will be doing their job. My thread count is not fixed and can change any moment.</p>