633. 平方数之和 思路根据费马平方和定理:一个非负整数c能够被表示为两个整数的平方和,当且仅当c的所有形如4k+3的质因子的幂次均为偶数 Solutions123456789101112131415161718192021222324252627class Solution {public: bool judgeSquareSum(int c) { for (int base = 2; base * base <= c; base++) { // 如果不是因子,枚举下一个 if (c % base != 0) { continue; } // 计算 base 的幂 int exp = 0; while (c % base == 0) { c /= base; exp++; } // 根据 Sum of two squares theorem 验证 if (base % 4 == 3 && exp % 2 != 0) { return false; } } // 例如 11 这样的用例,由于上面的 for 循环里 base * base <= c ,base == 11 的时候不会进入循环体 // 因此在退出循环以后需要再做一次判断 return c % 4 != 3; }};