• 5

    As said earlier, a natural number is a sum of two squares if and only if each prime factor that's 3 modulo 4 occurs to an even power in the number's prime factorization.

    require 'prime'
    def judge_square_sum(c)
      c == 0 || c.prime_division.all? { |p, e| p % 4 != 3 || e.even? }

  • 0

    @StefanPochmann Hi Stefan. You code very well! But unfortunately all are Python. Wish you code with Java. Then easy for me to understand the logic behind your code :)

  • 0

    @Roham Um, you say "all are Python" right under a Ruby one.

    I do write Java occasionally, but mostly if it's an advantage. It just rarely is. And I think everybody should learn Python and Ruby anyway :-P

  • 0

    @StefanPochmann Haha.... Whenever I see your posts, It reminds me Python by default ;)
    Sometimes I try to understand the logic behind your code! But since I only know Java, It's not easy for me to get all!
    Anyway, thank you for sharing your knowledge and wish you the best :)

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.