Home > Backend Development > PHP Tutorial > . Sum of Square Numbers

. Sum of Square Numbers

WBOY
Release: 2024-07-18 01:22:15
Original
647 people have browsed it

. 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;
    }
}
Copy after login

Contact Links

  • LinkedIn
  • GitHub

The above is the detailed content of . Sum of Square Numbers. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template