# Output the nth Even Perfect Number

## Challenge

Given an integer, n, as input where 0 <= n <= 2^10, output the nth even perfect number.

## Perfect Numbers

A perfect number is a number, x where the sum of its factors (excluding itself) equals x. For example, 6:

6: 1, 2, 3, 6


And, of course, 1 + 2 + 3 = 6, so 6 is perfect.

If a perfect number, x, is even, x mod 2 = 0.

## Examples

The following are the first 10 even perfect numbers:

6
28
496
8128
33550336
8589869056
137438691328
2305843008139952128
2658455991569831744654692615953842176
191561942608236107294793378084303638130997321548169216


Note that you may index this however you wish: 6 may be the 1st or the 0th even perfect number.

## Winning

Shortest code in bytes wins.

• @LeakyNun I think, that is an open question. If this question was output the nth odd perfect number... You would need a billion rep bounty to get it solved. blogs.ams.org/mathgradblog/2013/07/25/odd-perfect-numbers-exist (none exist below 10^300) Jun 3 '17 at 16:23
• What is the smallest odd perfect number? Jun 3 '17 at 16:23
• An even number n is perfect iff there is a Mersenne prime p such that n = p(p+1)/2. There is no such formula for odd perfect numbers; moreover, it is unknown if odd perfect numbers even exist. Jun 3 '17 at 16:57
• Not quite. There are only 49 known Mersenne primes. Jun 3 '17 at 17:01
• @BetaDecay: it is greater than $49$, so the 60th perfect number is not known. Jun 4 '17 at 17:55

$Combine the two links to the left into a monadic chain. Æṣ Compute the sum of k's proper divisors. = Compare the result with k. Ṫ Tail; extract the last match.  • So many builtins regarding divisors... Jun 3 '17 at 18:24 # Mathematica, 13 bytes Not surprisingly, there is a built-in. PerfectNumber  Example: In[1]:= PerfectNumber[18] Out[1]= 33570832131986724437010877211080384841138028499879725454996241573482158\ > 45044404288204877880943769038844953577426084988557369475990617384115743842\ > 47301308070476236559422361748505091085378276585906423254824947614731965790\ > 74656099918600764404702181660294469121778737965822199901663478093006075022\ > 35922320184998563614417718592540207818507301504509772708485946474363553778\ > 15002849158802448863064617859829560720600134749556178514816801859885571366\ > 09224841817877083608951191123174885226416130683197710667392351007374503755\ > 40335253147622794359007165170269759424103195552989897121800121464177467313\ > 49444715625609571796578815564191221029354502997518133405151709561679510954\ > 53649485576150660101689160658011770193274226308280507786835049549112576654\ > 51011967045674593989019420525517538448448990932896764698816315598247156499\ > 81962616327512831278795091980742531934095804545624886643834653798850027355\ > 06153988851506645137759275553988219425439764732399824712438125054117523837\ > 43825674443705501944105100648997234160911797840456379499200487305751845574\ > 87014449512383771396204942879824895298272331406370148374088561561995154576\ > 69607964052126908149265601786094447595560440059050091763547114092255371397\ > 42580786755435211254219478481549478427620117084594927467463298521042107553\ > 17849183589266903954636497214522654057134843880439116344854323586388066453\ > 13826206591131266232422007835577345584225720310518698143376736219283021119\ > 28761789614688558486006504887631570108879621959364082631162227332803560330\ > 94756423908044994601567978553610182466961012539222545672409083153854682409\ > 31846166962495983407607141601251889544407008815874744654769507268678051757\ > 74695689121248545626112138666740771113961907153092335582317866270537439303\ > 50490226038824797423347994071302801487692985977437781930503487497407869280\ > 96033906295910199238181338557856978191860647256209708168229116156300978059\ > 19702685572687764976707268496046345276316038409383829227754491185785965832\ > 8888332628525056  • I think there is a standard loophole for that? Jun 4 '17 at 12:42 • @PaŭloEbermann correct, with 19 downvotes and a comment with 94 upvotes approving of it: codegolf.meta.stackexchange.com/a/1078/32933 – Tim Jun 4 '17 at 18:39 # MATL, 15 bytes @Z\s@E=vtsG<}n  Very slow. It keeps trying increasing numbers one by one until the n-th perfect number is found. Try it online! ### Explanation  % Do...while @ % Push iteration index, k (starting at 1) Z\ % Array of divisors s % Sum @E % Push k. Multiply by 2 = % Equal? If so, k is a perfect number v % Concatenate vertically. This gradually builds an array which at the k-th % iteration contains k zero/one values, where ones indicate perfect numbers ts % Duplicate. Sum of array G< % Push input. Less than? This is the loop condition: if true, proceed with % next iteration } % Finally (execute right before exiting loop) n % Number of elements of the array % End (implicit). Display (implicit)  # Pyth, 13 bytes e.fqsf!%ZTStZ  Try it online! Please do not try any higher number. It just tests the even numbers one by one. • @BetaDecay thanks, updated. Jun 3 '17 at 17:07 # 05AB1E, 8 bytes µNNÑ¨OQ½  Try it online! Explanation µ # loop over increasing N until counter equals input N # push N NÑ # push factors of N ¨ # remove last factor (itself) O # sum factors Q # compare the sum to N for equality ½ # if true, increase counter  # Python 2, 198 153 83 78 77 75 74 bytes i=input() j=0 while i:j+=1;i-=sum(x*(j%x<1)for x in range(1,j))==j print j  Try it online! Now it just reads like psuedocode. • Saved 45 Countless Bytes because @Leaky Nun taught me about the sum function and list comprehension. • Saved 2 bytes thanks to @shooqie's suggestion to remove the uncessary brackets. We just iterate through every even number until we have found n perfect numbers. • notice that your g is actually just sum. Jun 3 '17 at 16:55 • @LeakyNun serves me right, for not knowing the python libraries. I really should learn more than just java and SILOS. Jun 3 '17 at 16:57 • Also, I've replaced your f with a list comprehension Jun 3 '17 at 16:57 • more golfings Jun 3 '17 at 16:58 • Jun 3 '17 at 16:59 # PHP, 111 Bytes 0-Indexing Works with the concept that a perfect number is a number where n=x*y x=2^i and y=2^(i+1)-1 and y must be prime for(;!$r[$argn];$u?:$r[]=$z)for($z=2**++$n*($y=2**($n+1)-1),$u=0,$j=1;$j++<sqrt($y);)$y%$j?:$u++;echo$r[$argn];  Try it online! # Python 3, 69 bytes f=lambda n,k=1:n and-~f(n-(sum(j>>k%j*j for j in range(1,k))==k),k+1)  Try it online! Scala, 103 bytes n=>Stream.from(1).filter(_%2==0).filter(x=>Stream.from(1).take(x-1).filter(x%_==0).sum==x).drop(n).head  # Haskell, 61 Bytes (!!)(filter(\x->x==sum[n|n<-[1..x-1],xmodn==0]||x==1)[1..])  • Since the index can start at 0, you don't need the ||x==1. You can also save bytes by moving the !! just before the closing parenthesis to make an operator section, and by replacing the filter with another list comprehension. Jun 4 '17 at 8:50 # JavaScript (ES6), 68 bytes n=>eval(for(x=5;n;s||n--)for(f=s=++x;f--;)(x/f-(x/f|0))||(s-=f);x)  F=n=>eval('for(x=5;n;s||n--)for(f=s=++x;f--;)(x/f-(x/f|0))||(s-=f);x') console.log( F(1), F(2), F(3), //F(4), //F(5), ) # Perl 6, 42 bytes {(grep {$_==[+] grep $_%%*,^$_},^∞)[$_]}  The input index is 1-based. ## Clojure, 79 bytes #(nth(for[i(range):when(=(apply +(for[j(range 1 i):when(=(mod i j)0)]j))i)]i)%)  Following the spec, heavy usage of for's :when condition. # PowerShell, 152 bytes $ErrorActionPreference='SilentlyContinue'
function p{param($x)(1..($x-1)|?{!($x%$_)})|%{$s+=$_};$s} 1..$args[0]|%{if(((p -x $_)-eq$_)-and(!($_%2))){$_}}