{Sum+=i++} to Reach N I am developing code for a problem posted on StackOverflow. 
I have written my solution in Java but it is taking too much time to execute for numbers such as 21432154197846387216432.  How could I improve the performance of the following code?

import java.util.Scanner;

public class Main {

    // private static final double N = Double.parseDouble("916548345678631") ;
    // private static final long loopUntil = (int)(Math.sqrt(2 * N));
    // private static double M = 1.0 ;

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in).useDelimiter("\n");
        while (scanner.hasNext()) {

            double N = Double.parseDouble(scanner.next());
            long loopUntil = (long) (Math.sqrt(2 * N));
            double M = 1.0;

            int doProcess = doProcess(N, loopUntil, M);
            System.out.println(doProcess);
        }
        scanner.close();
    }

    public static int doProcess(double N, long loopUntil, double M) {
        int result = 0;
        double constantDevisor = 1.0 / 2.0 ;

        // System.out.println("Loop Unttil " + loopUntil);
        while (M &lt;= loopUntil) {
            double val = 0;
            // System.out.println((N / M) +" " + ( M / 2 ) + " " + (1.0/2.0));
            val = (N / M) + (M / 2) + constantDevisor;
            // System.out.println(val);
            if (Math.floor(val) == val) {
                System.out.println("With : " + M);
                result++;
            }
            M++;
        }

        return result;
    }

}
You have one serious problem - that supergigantic number you want to use is larger than the precision of both doubles AND longs. In fact, because both doubles and longs use 64 bits in Java, you would be better off using longs because with doubles, only 53 of those 64 bits are available for what you want to do.

What this means is that, in order to get a meaningful calculation AT ALL for 21432154197846387216432 that you would have to implement it with something else because the algorithm, as you have implemented it, will give you answers that are completely meaningless because your initial value is a victim of roundoff error.

Now, for numbers up to the limit of long (9223372036854775807), I have created a different implementation that uses only long operations. It's organized differently because we have to force the algorithm to work correctly without any fractional arithmetic.

public class MainTwo {

    private static final long N = 916548345678631l;

    public static void main(String[] args) {

        long start = System.currentTimeMillis();
        long loopUntil = (long) (Math.sqrt(2 * N));
        long M = 1l;

        int doProcess = doProcess(N, loopUntil, M);
        System.out.println(doProcess);

        long end = System.currentTimeMillis();
        System.out.println("[Main.main] elapsed time: " + (end - start));
    }

    public static int doProcess(long N, long loopUntil, long M) {
        int result = 0;

        while (M &lt;= loopUntil) {

            boolean yesProcess = false;
            long val = 0;
            boolean mEven = (M % 2) == 0;
            if (mEven) {
                yesProcess = (N % M == M/2);
                if (yesProcess) {
                    val = (N + 1) / M + (M / 2);
                }
            }
            else {
                yesProcess = N % M == 0;
                if (yesProcess) {
                    val = (N / M) + (M + 1) / 2;
                }
            }

            if (yesProcess) {
                System.out.println("With : " + M);
                result++;
            }
            M++;
        }

        return result;
    }

}


And just for the sake of completeness, here is an implementation using BigInteger that will be able to work correctly against your giant number. Be warned though - using BigInteger makes it more than 20 times slower than the long implementation. I used this BigSquareRoot class.

public class MainThree {
//    private static final BigInteger N = new BigInteger("916548345678631");
    private static final BigInteger N = new BigInteger("21432154197846387216432");

    private static final BigSquareRoot bsr = new BigSquareRoot();
    private static final BigInteger zero = new BigInteger("0");
    private static final BigInteger one = new BigInteger("1");
    private static final BigInteger two = new BigInteger("2");

    public static void main(String[] args) {

        long start = System.currentTimeMillis();
        BigInteger loopUntil = bsr.get(N.multiply(two)).toBigInteger();
        BigInteger M = one;

        int doProcess = doProcess(N, loopUntil, M);
        System.out.println(doProcess);

        long end = System.currentTimeMillis();
        System.out.println("[Main.main] elapsed time: " + (end - start));
    }

    public static int doProcess(BigInteger N, BigInteger loopUntil, BigInteger M) {
        int result = 0;

        while (M.compareTo(loopUntil) &lt; 0) {

            boolean yesProcess = false;
            BigInteger val = zero;
            boolean mEven = M.mod(two).equals(zero);
            if (mEven) {
                yesProcess = N.mod(M).equals(M.divide(two));
                if (yesProcess) {
                    val = (N.add(one).divide(M)).add(M.divide(two));
                }
            }
            else {
                yesProcess = N.mod(M).equals(zero);
                if (yesProcess) {
                    val = N.divide(M).add(M.add(one).divide(two));
                }
            }

            if (yesProcess) {
                System.out.println("With : " + M);
                result++;
            }
            M = M.add(one);
        }

        return result;
    }
}