Algorithm to check if a number if a perfect number

前端 未结 4 2176
走了就别回头了
走了就别回头了 2021-01-05 08:04

I am looking for an algorithm to find if a given number is a perfect number.

The most simple that comes to my mind is :

  1. Find all the factors of the nu
4条回答
  •  遥遥无期
    2021-01-05 08:21

    If the input is even, see if it is of the form 2^(p-1)*(2^p-1), with p and 2^p-1 prime.

    If the input is odd, return "false". :-)

    See the Wikipedia page for details.

    (Actually, since there are only 47 perfect numbers with fewer than 25 million digits, you might start with a simple table of those. Ask the interviewer if you can assume you are using 64-bit numbers, for instance...)

提交回复
热议问题