I am trying to find out the square root of an integer, but in case the integer value is too large for instance - 2147395599. Then the following program gives this exception.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at com.aakash.BinarySearch.SquareRoot.mySqrt(SquareRoot.java:12)
at com.aakash.BinarySearch.SquareRoot.main(SquareRoot.java:8)
Process finished with exit code 1
Square root Program
package com.aakash.BinarySearch;
import java.util.Arrays;
public class SquareRoot {
public static void main(String[] args) {
int ans = mySqrt(2147395599);
System.out.println(ans);
}
public static int mySqrt(int x) {
int[] arrayUpton = new int[x];
int start =0;
int end = arrayUpton.length-1;
int mid = start + (start-end)/2;
for (int index = start; index <= end; index++) {
arrayUpton[index]=index+1;
}
for (int index = start; index < end; index++) {
if(arrayUpton[index]*arrayUpton[index]==x){
return arrayUpton[index];
} else if (arrayUpton[index]*arrayUpton[index]>x) {
return arrayUpton[index-1];
}
}
return 0;
}
}