# Thursday Code Puzzler:Perfect Numbers

Thursday is code puzzler day here at DZone. The idea is simple: solve the coding problem as efficiently as you can, in any language or framework that you find suitable.

*Note: Even
though there really is nothing stopping you from finding a solution to
this on the internet, try to keep honest, and come up with your own
answer. It's all about the participation!*

Do you have code puzzlers that you'd like to share with the DZone community? If so, please submit here.

**Find The First 20 Perfect Numbers **

The definition of a perfect number, from Wikipedia, states:

In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum). Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself) i.e. σ1(n) = 2n.

So, the first perfect number is 6, since 1+2+3 = 6.

With this information, your task is to write a method that will find the first 20 perfect numbers.

Catch up on all our previous puzzlers here.

## Comments

## Vijay Nathani replied on Thu, 2013/04/11 - 1:06pm

Groovy

## sun east replied on Thu, 2013/04/11 - 7:19pm in response to: Vijay Nathani

I think your code is

too slowto solve the first 20 perfect numbers.## sun east replied on Thu, 2013/04/11 - 7:47pm

Here is my codes in scala using the known result

that 2p−1(2p−1) is an even perfect number whenever 2p−1 is prime:## Pieter van der Meer replied on Fri, 2013/04/12 - 10:20am

A long version in Java, can be optimized but result is verified.

## Pieter van der Meer replied on Fri, 2013/04/12 - 10:26am in response to: sun east

Did not run this one, but based on my own solution i think the at least 5th answer is incorrect. Just checking prime is not enough. p=11,23,29 and some more do not result in a perfect number :-(

## sun east replied on Fri, 2013/04/12 - 11:39am in response to: Pieter van der Meer

Actually it is correct, you can look this page for more information, and of course you can run my code yourself to check the result.

## Eric Bennion replied on Thu, 2013/04/18 - 12:03pm in response to: Pieter van der Meer

When I ran your code, I verified that it found all 20 of the values of p. What I then found was that once the numbers reached the limit of BigInteger, it repeated that number for the remaining values. I made some adjustments and used BigDecimal and had success in matching the Wiki page of Perfect numbers.

## Bob Beechey replied on Thu, 2013/04/25 - 5:56pm

Checking a sequence of numbers for their divisors would produce a program that would take days to run. Checking that any number n and 2^p - 1 are both prime takes an age using home-grown naïve prime tests but is an almost bearable time using probabilistic techniques (as in BigInteger library in Java or primepy in Python). The best times use the Lucas Lehmer test as in Eric Bennion and Peter van der Bier showed above. Here is the same algorithm in Python

However, the exercise is then not really finding perfect numbers but rather finding Mersenne primes. If we assume knowledge of such primes, we get the fastest solution

## Wissem Belguidoum replied on Fri, 2013/05/03 - 9:46pm