Ruby using prime factorization


  • 0

    Based on @awice's solution (see explanation there), but using Ruby's existing factorization method.

    require 'prime'
    
    def check_perfect_number(num)
      num > 0 && Prime.prime_division(num).map { |p, e| (p**(e+1) - 1) / (p - 1) }.inject(:*) == 2 * num
    end
    

Log in to reply
 

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