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.
-
3\$\begingroup\$ @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) \$\endgroup\$Rohan Jhunjhunwala– Rohan Jhunjhunwala2017年06月03日 16:23:48 +00:00Commented Jun 3, 2017 at 16:23
-
1\$\begingroup\$ What is the smallest odd perfect number? \$\endgroup\$Leaky Nun– Leaky Nun2017年06月03日 16:23:49 +00:00Commented Jun 3, 2017 at 16:23
-
5\$\begingroup\$ 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. \$\endgroup\$Dennis– Dennis2017年06月03日 16:57:07 +00:00Commented Jun 3, 2017 at 16:57
-
2\$\begingroup\$ Not quite. There are only 49 known Mersenne primes. \$\endgroup\$Dennis– Dennis2017年06月03日 17:01:37 +00:00Commented Jun 3, 2017 at 17:01
-
1\$\begingroup\$ @BetaDecay: it is greater than 49ドル,ドル so the 60th perfect number is not known. \$\endgroup\$Ross Millikan– Ross Millikan2017年06月04日 17:55:19 +00:00Commented Jun 4, 2017 at 17:55
15 Answers 15
Jelly, 7 bytes
6Æṣ=$#Ṫ
How it works
6Æṣ=$#Ṫ Main link. Argument: n
6 Set the return value to 6.
# Execute the link to the left with argument k = 6, 7, 8, ... until n
values of k result in a truthy value. Yield the array of matches.
$ 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.
-
\$\begingroup\$ So many builtins regarding divisors... \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年06月03日 18:24:15 +00:00Commented Jun 3, 2017 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
-
\$\begingroup\$ I think there is a standard loophole for that? \$\endgroup\$Paŭlo Ebermann– Paŭlo Ebermann2017年06月04日 12:42:01 +00:00Commented Jun 4, 2017 at 12:42
-
1\$\begingroup\$ @PaŭloEbermann correct, with 19 downvotes and a comment with 94 upvotes approving of it: codegolf.meta.stackexchange.com/a/1078/32933 \$\endgroup\$Tim– Tim2017年06月04日 18:39:31 +00:00Commented Jun 4, 2017 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.
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)
05AB1E, 8 bytes
μNNÑ ̈OQ1⁄2
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
1⁄2 # 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
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.
-
\$\begingroup\$ notice that your
gis actually justsum. \$\endgroup\$Leaky Nun– Leaky Nun2017年06月03日 16:55:52 +00:00Commented Jun 3, 2017 at 16:55 -
\$\begingroup\$ @LeakyNun serves me right, for not knowing the python libraries. I really should learn more than just java and SILOS. \$\endgroup\$Rohan Jhunjhunwala– Rohan Jhunjhunwala2017年06月03日 16:57:23 +00:00Commented Jun 3, 2017 at 16:57
-
\$\begingroup\$ Also, I've replaced your
fwith a list comprehension \$\endgroup\$Leaky Nun– Leaky Nun2017年06月03日 16:57:44 +00:00Commented Jun 3, 2017 at 16:57 -
\$\begingroup\$ more golfings \$\endgroup\$Leaky Nun– Leaky Nun2017年06月03日 16:58:33 +00:00Commented Jun 3, 2017 at 16:58
-
\$\begingroup\$ no need for
k, just decrementi. \$\endgroup\$Leaky Nun– Leaky Nun2017年06月03日 16:59:35 +00:00Commented Jun 3, 2017 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];
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],x`mod`n==0]||x==1)[1..])
-
\$\begingroup\$ 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 thefilterwith another list comprehension. \$\endgroup\$faubi– faubi2017年06月04日 08:50:38 +00:00Commented Jun 4, 2017 at 8:50
Haskell, 52 bytes
([n|n<-[1..],n==sum[d|d<-[1..div n 2],mod n d<1]]!!)
Starts at n = 0. Not particularly fast
-
1\$\begingroup\$ By our consensus, it's valid to omit the
f=from the byte count. \$\endgroup\$naffetS– naffetS2022年08月10日 17:19:59 +00:00Commented Aug 10, 2022 at 17:19
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),
)
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))){$_}}