Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.
public class PerfectSquare{
public static void main(String[] args){
System.out.println(isPerfectSquare(Integer.parseInt(args[0])));
// MAX int value: 2147483647 = 2^31-1
// But, the maximum value I am able to work with is 2^30, larger numbers below MAX do not throw error but stall.
}
public static boolean isPerfectSquare(int input){
for(int i = 1; i < input; i*=2){
int product = i*i;
if (product == input){
System.out.println("power of 2: "+i);
return true;
}else if(product > input){
int lowerBound = i/2;
int upperBound = i;
return binarySearch(lowerBound, upperBound, input);
}
}
return false;
}
public static boolean binarySearch(int low, int high, int input){
int mid, product, ans = 0;
while (low <= high) {
mid = (low + high) / 2;
System.out.println("low: "+low+", high:" +high+", middle: "+mid);
product = mid*mid;
if(product == input){
System.out.println("other square: "+mid);
ans = 1;
break;
} else if (product > input) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans == 1 ? true : false;
}
}
public class PerfectSquare{
public static void main(String[] args){
System.out.println(isPerfectSquare(Integer.parseInt(args[0])));
// MAX int value: 2147483647 = 2^31-1
// But, the maximum value I am able to work with is 2^30, larger numbers below MAX do not throw error but stall.
}
public static boolean isPerfectSquare(long input){
for(long i = 1; i < input; i*=2){
long product = i*i;
if (product == input){
System.out.println("power of 2: "+i/4);
return true;
}else if(product > input){
long lowerBound = i/2;
long upperBound = i;
return binarySearch(lowerBound, upperBound, input);
}
}
return false;
}
public static boolean binarySearch(long low, long high, long input){
long mid, product, ans = 0;
while (low <= high) {
mid = (low + high) / 2;
System.out.println("low: "+low/4+", high:" +high/4+", middle: "+mid/4);
product = mid*mid;
if(product == input){
System.out.println("other square: "+mid/4);
ans = 1;
break;
} else if (product > input) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans == 1 ? true : false;
}
}
I don't understand why long works and int doesn't. long is 32bit, and int is 64bit,
then i*i > 2^32
conclusion: my algorithm is crappy,
public static boolean isPerfectSquare(int input){
for(int i = 1; i < input; i*=2){
long product = (long)i*i;
System.out.println("Product " + product);
if (product == input){
System.out.println("power of 2: "+i);
return true;
}else if(product > (long)input){
int lowerBound = i/2;
int upperBound = i;
return binarySearch(lowerBound, upperBound, input);
}
}
return false;
}
public static boolean binarySearch(long low, long high, long input){
long mid, product, ans = 0;
while (low <= high) {
mid = (low + high) / 2;
System.out.println("low: "+low+", high:" +high+", middle: "+mid);
product = mid*mid;
if(product == input){
System.out.println("other square: "+mid);
ans = 1;
break;
} else if (product > input) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans == 1 ? true : false;
}
If you simply switch to longs then the method works fine for me:
class inttest{
static int i=0;
public static void main(String[] args){
System.out.println(Integer.parseInt(args[0]));
i=Integer.parseInt(args[0])+1;
System.out.println(i);
}
}
class inttest{
static int i=0;
static long l =0;
public static void main(String[] args){
if (Integer.parseInt(args[0])<2147483647) {args[0]="2147483647";}
System.out.println(Integer.parseInt(args[0]));
i=Integer.parseInt(args[0])+1;
System.out.println(i);
l= (long)i;
try{
while (l<Long.MAX_VALUE&&l>Long.MIN_VALUE){
l *= 2;
System.out.println(l);
}
}catch(Exception e){}
}
}
If I haven't upset Doug, he might help further, but my money says that whatever type you use, it's finite and will overrun sometime.
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
eclipse package explorer vs project explorer view | 2 | 135 | |
ArrayIndexOutOfBoundExcept |
9 | 85 | |
MySQL on Tomcat | 8 | 48 | |
sql import cannot be resolved jsp | 3 | 26 |
Join the community of 500,000 technology professionals and ask your questions.