DEV Community

Cover image for 633. Sum of Square Numbers
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on

633. Sum of Square Numbers

633. Sum of Square Numbers

Medium

Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c.

Example 1:

  • Input: c = 5
  • Output: true
  • Explanation: 1 * 1 + 2 * 2 = 5

Example 2:

  • Input: c = 3
  • Output: false

Constraints:

  • 0 <= c <= 231 - 1

Solution:

class Solution {

    /**
     * @param Integer $c
     * @return Boolean
     */
    function judgeSquareSum($c) {
        for ($i = 2; $i * $i <= $c; $i++) {
            $count = 0;
            if ($c % $i == 0) {
                while ($c % $i == 0) {
                    $count++;
                    $c /= $i;
                }
                if ($i % 4 == 3 && $count % 2 != 0)
                    return false;
            }
        }
        return $c % 4 != 3;
    }
}
Enter fullscreen mode Exit fullscreen mode

Contact Links

Top comments (0)