平方数之和
问题陈述
题目描述:判断一个非负整数是否为两个整数的平方和。
思路分析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public boolean getSquareSum(int c){ if(c<0) return false; int i=0,j=(int)Math.sqrt(c); while(i<=j){ int sum=i*i+j*j; if(sum==c) return true; else if(sum>c) j--; else i++; } return false; }
|
1 2 3 4 5 6 7 8 9 10
| public boolean getSquareSum(int c){ for(long a=0;a*a<=c;a++){ double b=Math.sqrt(c-a*a); if(b==(int)b){ return true; } return false; } }
|