classSolution { public: booljudgeSquareSum(int c){ for (int a = sqrt(c); a >= 0; a--) { int b = sqrt(c - a * a); if (b * b + a * a == c) { returntrue; } } returnfalse; } };
Python
1 2 3 4 5 6 7 8 9
from math import sqrt
classSolution: defjudgeSquareSum(self, c: int) -> bool: for a inrange(int(sqrt(c)) + 1): b = sqrt(c - a * a) if b == int(b): returnTrue returnFalse
Java
1 2 3 4 5 6 7 8 9 10 11
classSolution { publicbooleanjudgeSquareSum(int c) { for (inta= (int)Math.sqrt(c); a >= 0; a--) { intb= (int)Math.sqrt(c - a * a); if (a * a + b * b == c) { returntrue; } } returnfalse; } }
Go
1 2 3 4 5 6 7 8 9 10 11 12
package main import"math"
funcjudgeSquareSum(c int)bool { for a := int(math.Sqrt(float64(c))); a >= 0; a-- { b := int(math.Sqrt(float64(c - a * a))) if a * a + b * b == c { returntrue } } returnfalse }