Jump to content

Perfect numbers: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 27:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F perf(n)
V sum = 0
L(i) 1 .< n
Line 36:
L(i) 1..10000
I perf(i)
print(i, end' ‘ ’)</langsyntaxhighlight>
 
{{out}}
Line 50:
The only added optimization is the loop up to n/2 instead of n-1.
With 31 bit integers the limit is 2,147,483,647.
<langsyntaxhighlight lang="360asm">* Perfect numbers 15/05/2016
PERFECTN CSECT
USING PERFECTN,R13 prolog
Line 96:
PG DC CL12' ' buffer
YREGS
END PERFECTN</langsyntaxhighlight>
{{out}}
<pre>
Line 108:
Use of optimizations found in Rexx algorithms and use of packed decimal to have bigger numbers.
With 15 digit decimal integers the limit is 999,999,999,999,999.
<langsyntaxhighlight lang="360asm">* Perfect numbers 15/05/2016
PERFECPO CSECT
USING PERFECPO,R13 prolog
Line 183:
PW2 DS PL16
YREGS
END PERFECPO</langsyntaxhighlight>
{{out}}
<pre>
Line 197:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program perfectNumber64.s */
Line 458:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
<pre>
Perfect : 6
Line 471:
</pre>
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Main()
DEFINE MAXNUM="10000"
CARD ARRAY pds(MAXNUM+1)
Line 494:
FI
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Perfect_numbers.png Screenshot from Atari 8-bit computer]
Line 505:
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">function Is_Perfect(N : Positive) return Boolean is
Sum : Natural := 0;
begin
Line 514:
end loop;
return Sum = N;
end Is_Perfect;</langsyntaxhighlight>
 
=={{header|ALGOL 60}}==
{{works with|A60}}
<langsyntaxhighlight lang="algol60">
begin
 
Line 562:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 574:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}}
<langsyntaxhighlight lang="algol68">PROC is perfect = (INT candidate)BOOL: (
INT sum :=1;
FOR f1 FROM 2 TO ENTIER ( sqrt(candidate)*(1+2*small real) ) WHILE
Line 594:
IF is perfect(i) THEN print((i, new line)) FI
OD
)</langsyntaxhighlight>
{{Out}}
<pre>
Line 606:
=={{header|ALGOL W}}==
Based on the Algol 68 version.
<langsyntaxhighlight lang="algolw">begin
% returns true if n is perfect, false otherwise %
% n must be > 0 %
Line 627:
% test isPerfect %
for n := 2 until 10000 do if isPerfect( n ) then write( n );
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 639:
===Functional===
{{Trans|JavaScript}}
<langsyntaxhighlight AppleScriptlang="applescript">-- PERFECT NUMBERS -----------------------------------------------------------
 
-- perfect :: integer -> bool
Line 746:
end script
end if
end mReturn</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight AppleScriptlang="applescript">{6, 28, 496, 8128}</langsyntaxhighlight>
----
===Idiomatic===
====Sum of proper divisors====
<langsyntaxhighlight lang="applescript">on aliquotSum(n)
if (n < 2) then return 0
set sum to 1
Line 783:
if (isPerfect(n)) then set end of output to n
end repeat
return output</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{6, 28, 496, 8128}</langsyntaxhighlight>
 
====Euclid====
<langsyntaxhighlight lang="applescript">on isPerfect(n)
-- All the known perfect numbers listed in Wikipedia end with either 6 or 28.
-- These endings are either preceded by odd digits or are the numbers themselves.
Line 813:
if (isPerfect(n)) then set end of output to n
end repeat
return output</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{6, 28, 496, 8128, 33550336}</langsyntaxhighlight>
 
====Practical====
But since AppleScript can only physically manage seven of the known perfect numbers, they may as well be in a look-up list for maximum efficiency:
 
<langsyntaxhighlight lang="applescript">on isPerfect(n)
if (n > 1.37438691328E+11) then return missing value -- Too high for perfection to be determinable.
return (n is in {6, 28, 496, 8128, 33550336, 8.589869056E+9, 1.37438691328E+11})
end isPerfect</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
 
/* ARM assembly Raspberry PI */
Line 904:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
Perfect : 6
Line 913:
</pre>
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">divisors: $[n][ select 1..(n/2)+1 'i -> 0 = n % i ]
perfect?: $[n][ n = sum divisors n ]
loop 2..1000 'i [
if perfect? i -> print i
]</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
This will find the first 8 perfect numbers.
<langsyntaxhighlight lang="autohotkey">Loop, 30 {
If isMersennePrime(A_Index + 1)
res .= "Perfect number: " perfectNum(A_Index + 1) "`n"
Line 943:
Return false
Return true
}</langsyntaxhighlight>
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">$ awk 'func perf(n){s=0;for(i=1;i<n;i++)if(n%i==0)s+=i;return(s==n)}
BEGIN{for(i=1;i<10000;i++)if(perf(i))print i}'
6
28
496
8128</langsyntaxhighlight>
 
=={{header|Axiom}}==
{{trans|Mathematica}}
Using the interpreter, define the function:
<langsyntaxhighlight Axiomlang="axiom">perfect?(n:Integer):Boolean == reduce(+,divisors n) = 2*n</langsyntaxhighlight>
Alternatively, using the Spad compiler:
<langsyntaxhighlight Axiomlang="axiom">)abbrev package TESTP TestPackage
TestPackage() : withma
perfect?: Integer -> Boolean
Line 964:
add
import IntegerNumberTheoryFunctions
perfect? n == reduce("+",divisors n) = 2*n</langsyntaxhighlight>
 
Examples (testing 496, testing 128, finding all perfect numbers in 1...10000):
<langsyntaxhighlight Axiomlang="axiom">perfect? 496
perfect? 128
[i for i in 1..10000 | perfect? i]</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight Axiomlang="axiom">true
false
[6,28,496,8128]</langsyntaxhighlight>
 
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}
<langsyntaxhighlight lang="qbasic">FUNCTION perf(n)
sum = 0
for i = 1 to n - 1
Line 989:
perf = 0
END IF
END FUNCTION</langsyntaxhighlight>
 
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
<lang BASIC256>
function isPerfect(n)
if (n < 2) or (n mod 2 = 1) then return False
Line 1,014:
next i
end
</syntaxhighlight>
</lang>
 
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "PerfectN.bas"
110 FOR X=1 TO 10000
120 IF PERFECT(X) THEN PRINT X;
Line 1,029:
190 NEXT
200 LET PERFECT=N=S
210 END DEF</langsyntaxhighlight>
 
==={{header|Sinclair ZX81 BASIC}}===
Call this subroutine and it will (eventually) return <tt>PERFECT</tt> = 1 if <tt>N</tt> is perfect or <tt>PERFECT</tt> = 0 if it is not.
<langsyntaxhighlight lang="basic">2000 LET SUM=0
2010 FOR F=1 TO N-1
2020 IF N/F=INT (N/F) THEN LET SUM=SUM+F
2030 NEXT F
2040 LET PERFECT=SUM=N
2050 RETURN</langsyntaxhighlight>
 
==={{header|True BASIC}}===
<langsyntaxhighlight lang="basic">
FUNCTION perf(n)
IF n < 2 or ramainder(n,2) = 1 then LET perf = 0
Line 1,063:
PRINT "Presione cualquier tecla para salir"
END
</syntaxhighlight>
</lang>
 
=={{header|BBC BASIC}}==
===BASIC version===
<langsyntaxhighlight lang="bbcbasic"> FOR n% = 2 TO 10000 STEP 2
IF FNperfect(n%) PRINT n%
NEXT
Line 1,079:
NEXT
IF I% = SQR(N%) S% += I%
= (N% = S%)</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,090:
===Assembler version===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> DIM P% 100
[OPT 2 :.S% xor edi,edi
.perloop mov eax,ebx : cdq : div ecx : or edx,edx : loopnz perloop : inc ecx
Line 1,099:
IF B% = USRS% PRINT B%
NEXT
END</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,111:
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">( ( perf
= sum i
. 0:?sum
Line 1,128:
& (perf$!n&out$!n|)
)
);</langsyntaxhighlight>
{{Out}}
<pre>6
Line 1,136:
 
=={{header|Burlesque}}==
<langsyntaxhighlight lang="burlesque">Jfc++\/2.*==</langsyntaxhighlight>
 
<langsyntaxhighlight lang="burlesque">blsq) 8200ro{Jfc++\/2.*==}f[
 
{6 28 496 8128}</langsyntaxhighlight>
 
=={{header|C}}==
{{trans|D}}
<langsyntaxhighlight lang="c">#include "stdio.h"
#include "math.h"
 
Line 1,170:
 
return 0;
}</langsyntaxhighlight>
Using functions from [[Factors of an integer#Prime factoring]]:
<langsyntaxhighlight lang="c">int main()
{
int j;
Line 1,186:
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
{{trans|C++}}
<langsyntaxhighlight lang="csharp">static void Main(string[] args)
{
Console.WriteLine("Perfect numbers from 1 to 33550337:");
Line 1,213:
 
return sum == num ;
}</langsyntaxhighlight>
===Version using Lambdas, will only work from version 3 of C# on===
<langsyntaxhighlight lang="csharp">static void Main(string[] args)
{
Console.WriteLine("Perfect numbers from 1 to 33550337:");
Line 1,231:
{
return Enumerable.Range(1, num - 1).Sum(n => num % n == 0 ? n : 0 ) == num;
}</langsyntaxhighlight>
 
=={{header|C++}}==
{{works with|gcc}}
<langsyntaxhighlight lang="cpp">#include <iostream>
using namespace std ;
 
Line 1,254:
return 0 ;
}
</syntaxhighlight>
</lang>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn proper-divisors [n]
(if (< n 4)
[1]
Line 1,265:
 
(defn perfect? [n]
(= (reduce + (proper-divisors n)) n))</langsyntaxhighlight>
 
{{trans|Haskell}}
<langsyntaxhighlight lang="clojure">(defn perfect? [n]
(->> (for [i (range 1 n)] :when (zero? (rem n i))] i)
(reduce +)
(= n)))</langsyntaxhighlight>
 
===Functional version===
<langsyntaxhighlight lang="clojure">(defn perfect? [n]
(= (reduce + (filter #(zero? (rem n %)) (range 1 n))) n))</langsyntaxhighlight>
 
=={{header|COBOL}}==
Line 1,281:
{{works with|Visual COBOL}}
main.cbl:
<langsyntaxhighlight lang="cobol"> $set REPOSITORY "UPDATE ON"
IDENTIFICATION DIVISION.
Line 1,304:
GOBACK
.
END PROGRAM perfect-main.</langsyntaxhighlight>
 
perfect.cbl:
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
FUNCTION-ID. perfect.
Line 1,343:
GOBACK
.
END FUNCTION perfect.</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
Optimized version, for fun.
<langsyntaxhighlight lang="coffeescript">is_perfect_number = (n) ->
do_factors_add_up_to n, 2*n
Line 1,395:
for n in known_perfects
throw Error("fail") unless is_perfect_number(n)
throw Error("fail") if is_perfect_number(n+1)</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,407:
=={{header|Common Lisp}}==
{{trans|Haskell}}
<langsyntaxhighlight lang="lisp">(defun perfectp (n)
(= n (loop for i from 1 below n when (= 0 (mod n i)) sum i)))</langsyntaxhighlight>
 
=={{header|D}}==
===Functional Version===
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.range;
 
bool isPerfectNumber1(in uint n) pure nothrow
Line 1,423:
void main() {
iota(1, 10_000).filter!isPerfectNumber1.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>[6, 28, 496, 8128]</pre>
Line 1,429:
===Faster Imperative Version===
{{trans|Algol}}
<langsyntaxhighlight lang="d">import std.stdio, std.math, std.range, std.algorithm;
 
bool isPerfectNumber2(in int n) pure nothrow {
Line 1,449:
void main() {
10_000.iota.filter!isPerfectNumber2.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>[6, 28, 496, 8128]</pre>
Line 1,457:
=={{header|Dart}}==
=== Explicit Iterative Version ===
<langsyntaxhighlight lang="d">/*
* Function to test if a number is a perfect number
* A number is a perfect number if it is equal to the sum of all its divisors
Line 1,479:
// We return the test if n is equal to sumOfDivisors
return n == sumOfDivisors;
}</langsyntaxhighlight>
 
=== Compact Version ===
{{trans|Julia}}
<langsyntaxhighlight lang="d">isPerfect(n) =>
n == new List.generate(n-1, (i) => n%(i+1) == 0 ? i+1 : 0).fold(0, (p,n)=>p+n);</langsyntaxhighlight>
 
In either case, if we test to find all the perfect numbers up to 1000, we get:
<langsyntaxhighlight lang="d">main() =>
new List.generate(1000,(i)=>i+1).where(isPerfect).forEach(print);</langsyntaxhighlight>
{{out}}
<pre>6
Line 1,497:
=={{header|Dyalect}}==
 
<langsyntaxhighlight lang="dyalect">func isPerfect(num) {
var sum = 0
for i in 1..<num {
Line 1,517:
print("\(x) is perfect")
}
}</langsyntaxhighlight>
 
=={{header|E}}==
<langsyntaxhighlight lang="e">pragma.enable("accumulator")
def isPerfectNumber(x :int) {
var sum := 0
Line 1,528:
}
return sum <=> x
}</langsyntaxhighlight>
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 1,573:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,584:
=={{header|Elena}}==
ELENA 4.x:
<langsyntaxhighlight lang="elena">import system'routines;
import system'math;
import extensions;
Line 1,603:
console.readChar()
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,613:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule RC do
def is_perfect(1), do: false
def is_perfect(n) when n > 1 do
Line 1,625:
end
 
IO.inspect (for i <- 1..10000, RC.is_perfect(i), do: i)</langsyntaxhighlight>
 
{{out}}
Line 1,633:
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">is_perfect(X) ->
X == lists:sum([N || N <- lists:seq(1,X-1), X rem N == 0]).</langsyntaxhighlight>
 
=={{header|ERRE}}==
<langsyntaxhighlight ERRElang="erre">PROGRAM PERFECT
 
PROCEDURE PERFECT(N%->OK%)
Line 1,655:
IF OK% THEN PRINT(N%)
END FOR
END PROGRAM</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,665:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let perf n = n = List.fold (+) 0 (List.filter (fun i -> n % i = 0) [1..(n-1)])
 
for i in 1..10000 do if (perf i) then printfn "%i is perfect" i</langsyntaxhighlight>
{{Out}}
<pre>6 is perfect
Line 1,675:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: kernel math math.primes.factors sequences ;
IN: rosettacode.perfect-numbers
 
: perfect? ( n -- ? ) [ divisors sum ] [ 2 * ] bi = ;</langsyntaxhighlight>
 
=={{header|FALSE}}==
<langsyntaxhighlight lang="false">[0\1[\$@$@-][\$@$@$@$@\/*=[@\$@+@@]?1+]#%=]p:
45p;!." "28p;!. { 0 -1 }</langsyntaxhighlight>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: perfect? ( n -- ? )
1
over 2/ 1+ 2 ?do
over i mod 0= if i + then
loop
= ;</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">FUNCTION isPerfect(n)
LOGICAL :: isPerfect
INTEGER, INTENT(IN) :: n
Line 1,705:
END DO
IF (factorsum == n) isPerfect = .TRUE.
END FUNCTION isPerfect</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
{{trans|C (with some modifications)}}
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Function isPerfect(n As Integer) As Boolean
Line 1,732:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,741:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">isPerfect = {|n| sum[allFactors[n, true, false]] == n}
println[select[1 to 1000, isPerfect]]</langsyntaxhighlight>
 
{{out}}
Line 1,749:
 
=={{header|FunL}}==
<langsyntaxhighlight lang="funl">def perfect( n ) = sum( d | d <- 1..n if d|n ) == 2n
 
println( (1..500).filter(perfect) )</langsyntaxhighlight>
 
{{out}}
Line 1,760:
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap">Filtered([1 .. 10000], n -> Sum(DivisorsInt(n)) = 2*n);
# [ 6, 28, 496, 8128 ]</langsyntaxhighlight>
 
=={{header|Go}}==
 
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,802:
}
 
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,813:
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">def isPerfect = { n ->
n > 4 && (n == (2..Math.sqrt(n)).findAll { n % it == 0 }.inject(1) { factorSum, i -> factorSum += i + n/i })
}</langsyntaxhighlight>
Test program:
<langsyntaxhighlight lang="groovy">(0..10000).findAll { isPerfect(it) }.each { println it }</langsyntaxhighlight>
{{Out}}
<pre>6
Line 1,825:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">perfect n =
n == sum [i | i <- [1..n-1], n `mod` i == 0]</langsyntaxhighlight>
 
Create a list of known perfects:
<langsyntaxhighlight lang="haskell">perfect =
(\x -> (2 ^ x - 1) * (2 ^ (x - 1))) <$>
filter (\x -> isPrime x && isPrime (2 ^ x - 1)) maybe_prime
Line 1,847:
main = do
mapM_ print $ take 10 perfect
mapM_ (print . (\x -> (x, isPerfect x))) [6, 27, 28, 29, 496, 8128, 8129]</langsyntaxhighlight>
 
 
or, restricting the search space to improve performance:
<langsyntaxhighlight lang="haskell">isPerfect :: Int -> Bool
isPerfect n =
let lows = filter ((0 ==) . rem n) [1 .. floor (sqrt (fromIntegral n))]
Line 1,866:
 
main :: IO ()
main = print $ filter isPerfect [1 .. 10000]</langsyntaxhighlight>
{{Out}}
<pre>[6,28,496,8128]</pre>
 
=={{header|HicEst}}==
<langsyntaxhighlight HicEstlang="hicest"> DO i = 1, 1E4
IF( perfect(i) ) WRITE() i
ENDDO
Line 1,882:
ENDDO
perfect = sum == n
END</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main(arglist)
limit := \arglist[1] | 100000
write("Perfect numbers from 1 to ",limit,":")
Line 1,899:
end
 
link factors</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}} [http://www.cs.arizona.edu/icon/library/src/procs/factors.icn Uses divisors from factors]
Line 1,912:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">is_perfect=: +: = >:@#.~/.~&.q:@(6>.<.)</langsyntaxhighlight>
 
Examples of use, including extensions beyond those assumptions:
<langsyntaxhighlight lang="j"> is_perfect 33550336
1
I. is_perfect i. 100000
Line 1,929:
0 0 0 0 0 0 0 0 1 0
is_perfect 191561942608236107294793378084303638130997321548169216x
1</langsyntaxhighlight>
 
More efficient version based on [http://jsoftware.com/pipermail/programming/2014-June/037695.html comments] by Henry Rich and Roger Hui (comment train seeded by Jon Hough).
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public static boolean perf(int n){
int sum= 0;
for(int i= 1;i < n;i++){
Line 1,942:
}
return sum == n;
}</langsyntaxhighlight>
Or for arbitrary precision:[[Category:Arbitrary precision]]
<langsyntaxhighlight lang="java">import java.math.BigInteger;
 
public static boolean perf(BigInteger n){
Line 1,955:
}
return sum.equals(n);
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Line 1,962:
 
{{trans|Java}}
<langsyntaxhighlight lang="javascript">function is_perfect(n)
{
var sum = 1, i, sqrt=Math.floor(Math.sqrt(n));
Line 1,982:
if (is_perfect(i))
print(i);
}</langsyntaxhighlight>
 
{{Out}}
Line 1,996:
Naive version (brute force)
 
<langsyntaxhighlight JavaScriptlang="javascript">(function (nFrom, nTo) {
 
function perfect(n) {
Line 2,014:
return range(nFrom, nTo).filter(perfect);
 
})(1, 10000);</langsyntaxhighlight>
 
Output:
 
<langsyntaxhighlight JavaScriptlang="javascript">[6, 28, 496, 8128]</langsyntaxhighlight>
 
Much faster (more efficient factorisation)
 
<langsyntaxhighlight JavaScriptlang="javascript">(function (nFrom, nTo) {
 
function perfect(n) {
Line 2,044:
return range(nFrom, nTo).filter(perfect)
 
})(1, 10000);</langsyntaxhighlight>
 
Output:
 
<langsyntaxhighlight JavaScriptlang="javascript">[6, 28, 496, 8128]</langsyntaxhighlight>
 
Note that the filter function, though convenient and well optimised, is not strictly necessary.
Line 2,054:
(Monadic return/inject for lists is simply lambda x --> [x], inlined here, and fail is [].)
 
<langsyntaxhighlight JavaScriptlang="javascript">(function (nFrom, nTo) {
 
// MONADIC CHAIN (bind) IN LIEU OF FILTER
Line 2,088:
}
 
})(1, 10000);</langsyntaxhighlight>
 
Output:
<langsyntaxhighlight JavaScriptlang="javascript">[6, 28, 496, 8128]</langsyntaxhighlight>
 
 
====ES6====
 
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
const main = () =>
enumFromTo(1, 10000).filter(perfect);
Line 2,120:
// MAIN ---
return main();
})();</langsyntaxhighlight>
 
{{Out}}
<langsyntaxhighlight JavaScriptlang="javascript">[6, 28, 496, 8128]</langsyntaxhighlight>
 
=={{header|jq}}==
<syntaxhighlight lang="jq">
<lang jq>
def is_perfect:
. as $in
Line 2,133:
 
# Example:
range(1;10001) | select( is_perfect )</langsyntaxhighlight>
{{Out}}
$ jq -n -f is_perfect.jq
Line 2,144:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">isperfect(n::Integer) = n == sum([n % i == 0 ? i : 0 for i = 1:(n - 1)])
perfects(n::Integer) = filter(isperfect, 1:n)
 
@show perfects(10000)</langsyntaxhighlight>
 
{{out}}
Line 2,154:
=={{header|K}}==
{{trans|J}}
<langsyntaxhighlight Klang="k"> perfect:{(x>2)&x=+/-1_{d:&~x!'!1+_sqrt x;d,_ x%|d}x}
perfect 33550336
1
Line 2,169:
(0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0)</langsyntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun isPerfect(n: Int): Boolean = when {
Line 2,196:
println("The first five perfect numbers are:")
for (i in 2 .. 33550336) if (isPerfect(i)) print("$i ")
}</langsyntaxhighlight>
 
{{out}}
Line 2,208:
 
=={{header|Lasso}}==
<langsyntaxhighlight lang="lasso">#!/usr/bin/lasso9
define isPerfect(n::integer) => {
Line 2,222:
with x in generateSeries(1, 10000)
where isPerfect(#x)
select #x</langsyntaxhighlight>
{{Out}}
<syntaxhighlight lang ="lasso">6, 28, 496, 8128</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">for n =1 to 10000
if perfect( n) =1 then print n; " is perfect."
next n
Line 2,245:
perfect =0
end if
end function</langsyntaxhighlight>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">on isPercect (n)
sum = 1
cnt = n/2
Line 2,255:
end repeat
return sum=n
end</langsyntaxhighlight>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">to perfect? :n
output equal? :n apply "sum filter [equal? 0 modulo :n ?] iseq 1 :n/2
end</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">function isPerfect(x)
local sum = 0
for i = 1, x-1 do
Line 2,269:
end
return sum == x
end</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module PerfectNumbers {
Function Is_Perfect(n as decimal) {
Line 2,344:
PerfectNumbers
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,363:
 
=={{header|M4}}==
<langsyntaxhighlight M4lang="m4">define(`for',
`ifelse($#,0,``$0'',
`ifelse(eval($2<=$3),1,
Line 2,381:
for(`x',`2',`33550336',
`ifelse(isperfect(x),1,`x
')')</langsyntaxhighlight>
 
=={{header|MAD}}==
 
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
R FUNCTION THAT CHECKS IF NUMBER IS PERFECT
Line 2,403:
PRINT COMMENT $ $
END OF PROGRAM
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,414:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">isperfect := proc(n) return evalb(NumberTheory:-SumOfDivisors(n) = 2*n); end proc:
isperfect(6);
true</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Custom function:
<langsyntaxhighlight Mathematicalang="mathematica">PerfectQ[i_Integer] := Total[Divisors[i]] == 2 i</langsyntaxhighlight>
Examples (testing 496, testing 128, finding all perfect numbers in 1...10000):
<langsyntaxhighlight Mathematicalang="mathematica">PerfectQ[496]
PerfectQ[128]
Flatten[PerfectQ/@Range[10000]//Position[#,True]&]</langsyntaxhighlight>
gives back:
<syntaxhighlight lang="mathematica">True
<lang Mathematica>True
False
{6,28,496,8128}</langsyntaxhighlight>
 
=={{header|MATLAB}}==
Standard algorithm:
<langsyntaxhighlight MATLABlang="matlab">function perf = isPerfect(n)
total = 0;
for k = 1:n-1
Line 2,440:
end
perf = total == n;
end</langsyntaxhighlight>
Faster algorithm:
<langsyntaxhighlight MATLABlang="matlab">function perf = isPerfect(n)
if n < 2
perf = false;
Line 2,461:
perf = total == n;
end
end</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">".."(a, b) := makelist(i, i, a, b)$
infix("..")$
 
Line 2,470:
 
sublist(1 .. 10000, perfectp);
/* [6, 28, 496, 8128] */</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<langsyntaxhighlight lang="maxscript">fn isPerfect n =
(
local sum = 0
Line 2,484:
)
sum == n
)</langsyntaxhighlight>
 
=={{header|Microsoft Small Basic}}==
{{trans|BBC BASIC}}
<langsyntaxhighlight lang="microsoftsmallbasic">
For n = 2 To 10000 Step 2
VerifyIfPerfect()
Line 2,518:
EndIf
EndSub
</syntaxhighlight>
</lang>
 
=={{header|Modula-2}}==
{{trans|BBC BASIC}}
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}}
<langsyntaxhighlight lang="modula2">
MODULE PerfectNumbers;
 
Line 2,567:
END;
END PerfectNumbers.
</syntaxhighlight>
</lang>
 
=={{header|Nanoquery}}==
{{trans|Python}}
<langsyntaxhighlight Nanoquerylang="nanoquery">def perf(n)
sum = 0
for i in range(1, n - 1)
Line 2,579:
end
return sum = n
end</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import math
 
proc isPerfect(n: int): bool =
Line 2,595:
for n in 2..10_000:
if n.isPerfect:
echo n</langsyntaxhighlight>
 
{{out}}
Line 2,604:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">bundle Default {
class Test {
function : Main(args : String[]) ~ Nil {
Line 2,626:
}
}
}</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let perf n =
let sum = ref 0 in
for i = 1 to n-1 do
Line 2,635:
sum := !sum + i
done;
!sum = n</langsyntaxhighlight>
Functional style:
<langsyntaxhighlight lang="ocaml">(* range operator *)
let rec (--) a b =
if a > b then
Line 2,644:
a :: (a+1) -- b
 
let perf n = n = List.fold_left (+) 0 (List.filter (fun i -> n mod i = 0) (1 -- (n-1)))</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">: isPerfect(n) | i | 0 n 2 / loop: i [ n i mod ifZero: [ i + ] ] n == ; </langsyntaxhighlight>
 
{{out}}
Line 2,657:
 
=={{header|ooRexx}}==
<langsyntaxhighlight ooRexxlang="oorexx">-- first perfect number over 10000 is 33550336...let's not be crazy
loop i = 1 to 10000
if perfectNumber(i) then say i "is a perfect number"
Line 2,673:
end
 
return sum = n</langsyntaxhighlight>
{{out}}
<pre>6 is a perfect number
Line 2,681:
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
fun {IsPerfect N}
fun {IsNFactor I} N mod I == 0 end
Line 2,692:
in
{Show {Filter {List.number 1 10000 1} IsPerfect}}
{Show {IsPerfect 33550336}}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
Uses built-in method. Faster tests would use the LL test for evens and myriad results on OPNs otherwise.
<langsyntaxhighlight lang="parigp">isPerfect(n)=sigma(n,-1)==2</langsyntaxhighlight>
Show perfect numbers
<langsyntaxhighlight lang="parigp">forprime(p=2, 2281,
if(isprime(2^p-1),
print(p"\t",(2^p-1)*2^(p-1))))</langsyntaxhighlight>
Faster with Lucas-Lehmer test
<langsyntaxhighlight lang="parigp">p=2;n=3;n1=2;
while(p<2281,
if(isprime(p),
Line 2,710:
if(s==0 || p==2,
print("(2^"p"-1)2^("p"-1)=\t"n1*n"\n")));
p++; n1=n+1; n=2*n+1)</langsyntaxhighlight>
{{Out}}
<pre>(2^2-1)2^(2-1)= 6
Line 2,724:
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">program PerfectNumbers;
 
function isPerfect(number: longint): boolean;
Line 2,746:
if isPerfect(candidate) then
writeln (candidate, ' is a perfect number.');
end.</langsyntaxhighlight>
{{Out}}
<pre>
Line 2,759:
=={{header|Perl}}==
=== Functions ===
<langsyntaxhighlight lang="perl">sub perf {
my $n = shift;
my $sum = 0;
Line 2,768:
}
return $sum == $n;
}</langsyntaxhighlight>
Functional style:
<langsyntaxhighlight lang="perl">use List::Util qw(sum);
 
sub perf {
my $n = shift;
$n == sum(0, grep {$n % $_ == 0} 1..$n-1);
}</langsyntaxhighlight>
=== Modules ===
The functions above are terribly slow. As usual, this is easier and faster with modules. Both ntheory and Math::Pari have useful functions for this.
{{libheader|ntheory}}
A simple predicate:
<langsyntaxhighlight lang="perl">use ntheory qw/divisor_sum/;
sub is_perfect { my $n = shift; divisor_sum($n) == 2*$n; }</langsyntaxhighlight>
Use this naive method to show the first 5. Takes about 15 seconds:
<langsyntaxhighlight lang="perl">use ntheory qw/divisor_sum/;
for (1..33550336) {
print "$_\n" if divisor_sum($_) == 2*$_;
}</langsyntaxhighlight>
Or we can be clever and look for 2^(p-1) * (2^p-1) where 2^p -1 is prime. The first 20 takes about a second.
<langsyntaxhighlight lang="perl">use ntheory qw/forprimes is_prime/;
use bigint;
forprimes {
my $n = 2**$_ - 1;
print "$_\t", $n * 2**($_-1),"\n" if is_prime($n);
} 2, 4500;</langsyntaxhighlight>
{{out}}
<pre>
Line 2,810:
 
We can speed this up even more using a faster program for printing the large results, as well as a faster primality solution. The first 38 in about 1 second with most of the time printing the large results. Caveat: this goes well past the current bound for odd perfect numbers and does not check for them.
<langsyntaxhighlight lang="perl">use ntheory qw/forprimes is_mersenne_prime/;
use Math::GMP qw/:constant/;
forprimes {
print "$_\t", (2**$_-1)*2**($_-1),"\n" if is_mersenne_prime($_);
} 7_000_000;</langsyntaxhighlight>
 
In addition to generating even perfect numbers, we can also have a fast function which returns true when a given even number is perfect:
<langsyntaxhighlight lang="perl">use ntheory qw(is_mersenne_prime valuation);
 
sub is_even_perfect {
Line 2,826:
($m >> $v) == 1 || return;
is_mersenne_prime($v + 1);
}</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">is_perfect</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))=</span><span style="color: #000000;">n</span>
Line 2,837:
<span style="color: #008080;">if</span> <span style="color: #000000;">is_perfect</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">i</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,847:
=== gmp version ===
{{libheader|Phix/mpfr}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Perfect_numbers.exw (includes native version above)</span>
Line 2,862:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_free</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,881:
=={{header|PHP}}==
{{trans|C++}}
<langsyntaxhighlight lang="php">function is_perfect($number)
{
$sum = 0;
Line 2,897:
if(is_perfect($num))
echo $num . PHP_EOL;
}</langsyntaxhighlight>
 
=={{header|Picat}}==
===Simple divisors/1 function===
First is the slow <code>perfect1/1</code> that use the simple divisors/1 function:
<langsyntaxhighlight Picatlang="picat">go =>
println(perfect1=[I : I in 1..10_000, perfect1(I)]),
nl.
perfect1(N) => sum(divisors(N)) == N.
divisors(N) = [J: J in 1..1+N div 2, N mod J == 0].</langsyntaxhighlight>
 
{{out}}
Line 2,914:
===Using formula for perfect number candidates===
The formula for perfect number candidates is: 2^(p-1)*(2^p-1) for prime p. This is used to find some more perfect numbers in reasonable time. <code>perfect2/1</code> is a faster version of checking if a number is perfect.
<langsyntaxhighlight Picatlang="picat">go2 =>
println("Using the formula: 2^(p-1)*(2^p-1) for prime p"),
foreach(P in primes(32))
Line 2,953:
% I is not a divisor of N.
sum_divisors(I,N,Sum0,Sum) =>
sum_divisors(I+1,N,Sum0,Sum).</langsyntaxhighlight>
 
{{out}}
Line 2,971:
 
The perfect numbers are printed only if they has < 80 digits, otherwise the number of digits are shown. The program stops when reaching a number with more than 100 000 digits. (Note: The major time running this program is getting the number of digits.)
<langsyntaxhighlight Picatlang="picat">go3 =>
ValidP = [2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607,
1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213,
Line 2,992:
end
end,
nl.</langsyntaxhighlight>
 
{{out}}
Line 3,028:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de perfect (N)
(let C 0
(for I (/ N 2)
(and (=0 (% N I)) (inc 'C I)) )
(= C N) ) )</langsyntaxhighlight>
 
<langsyntaxhighlight PicoLisplang="picolisp">(de faster (N)
(let (C 1 Stop (sqrt N))
(for (I 2 (<= I Stop) (inc I))
Line 3,040:
(=0 (% N I))
(inc 'C (+ (/ N I) I)) ) )
(= C N) ) )</langsyntaxhighlight>
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">perfect: procedure (n) returns (bit(1));
declare n fixed;
declare sum fixed;
Line 3,053:
end;
return (sum=n);
end perfect;</langsyntaxhighlight>
 
==={{header|PL/I-80}}===
<langsyntaxhighlight PLlang="pl/Ii">perfect_search: procedure options (main);
 
%replace
Line 3,097:
end isperfect;
 
end perfect_search;</langsyntaxhighlight>
 
{{out}}
Line 3,111:
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<langsyntaxhighlight lang="pli">100H: /* FIND SOME PERFECT NUMBERS: NUMBERS EQUAL TO THE SUM OF THEIR PROPER */
/* DIVISORS */
/* CP/M SYSTEM CALL AND I/O ROUTINES */
Line 3,161:
END;
END;
EOF</langsyntaxhighlight>
{{out}}
<pre>
Line 3,170:
{{Trans|Action!}}
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<langsyntaxhighlight lang="pli">100H: /* FIND SOME PERFECT NUMBERS: NUMBERS EQUAL TO THE SUM OF THEIR PROPER */
/* DIVISORS */
/* CP/M SYSTEM CALL AND I/O ROUTINES */
Line 3,213:
END;
END;
EOF</langsyntaxhighlight>
{{out}}
<pre>
Line 3,223:
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">Function IsPerfect($n)
{
$sum=0
Line 3,236:
}
 
Returns "True" if the given number is perfect and "False" if it's not.</langsyntaxhighlight>
 
=={{header|Prolog}}==
===Classic approach===
Works with SWI-Prolog
<langsyntaxhighlight Prologlang="prolog">tt_divisors(X, N, TT) :-
Q is X / N,
( 0 is X mod N -> (Q = N -> TT1 is N + TT;
Line 3,254:
perfect_numbers(N, L) :-
numlist(2, N, LN),
include(perfect, LN, L).</langsyntaxhighlight>
 
===Faster method===
Since a perfect number is of the form 2^(n-1) * (2^n - 1), we can eliminate a lot of candidates by merely factoring out the 2s and seeing if the odd portion is (2^(n+1)) - 1.
<syntaxhighlight lang="prolog">
<lang Prolog>
perfect(N) :-
factor_2s(N, Chk, Exp),
Line 3,285:
N mod D =\= 0,
D2 is D + A, prime(N, D2, As).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,298:
===Functional approach===
Works with SWI-Prolog and module lambda, written by <b>Ulrich Neumerkel</b> found there http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl
<langsyntaxhighlight Prologlang="prolog">:- use_module(library(lambda)).
 
is_divisor(V, N) :-
Line 3,334:
%% f_compose_1(Pred1, Pred2, Pred1(Pred2)).
%
f_compose_1(F,G, \X^Z^(call(G,X,Y), call(F,Y,Z))).</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure is_Perfect_number(n)
Protected summa, i=1, result=#False
Repeat
Line 3,349:
EndIf
ProcedureReturn result
EndProcedure</langsyntaxhighlight>
 
=={{header|Python}}==
Line 3,378:
 
===Python: Procedural===
<langsyntaxhighlight lang="python">def perf1(n):
sum = 0
for i in range(1, n):
if n % i == 0:
sum += i
return sum == n</langsyntaxhighlight>
 
===Python: Optimised Procedural===
<langsyntaxhighlight lang="python">from itertools import chain, cycle, accumulate
 
def factor2(n):
Line 3,407:
def perf4(n):
"Using most efficient prime factoring routine from: http://rosettacode.org/wiki/Factors_of_an_integer#Python"
return 2 * n == sum(factor2(n))</langsyntaxhighlight>
 
===Python: Functional===
<langsyntaxhighlight lang="python">def perf2(n):
return n == sum(i for i in range(1, n) if n % i == 0)
 
print (
list(filter(perf2, range(1, 10001)))
)</langsyntaxhighlight>
 
 
 
<langsyntaxhighlight lang="python">'''Perfect numbers'''
 
from math import sqrt
Line 3,453:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>[6, 28, 496, 8128]</pre>
Line 3,461:
<code>factors</code> is defined at [http://rosettacode.org/wiki/Factors_of_an_integer#Quackery Factors of an integer].
 
<langsyntaxhighlight Quackerylang="quackery"> [ 0 swap witheach + ] is sum ( [ --> n )
 
[ factors -1 pluck dip sum = ] is perfect ( n --> n )
Line 3,468:
10000 times
[ i^ 1+ perfect if [ i^ 1+ echo cr ] ]
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,480:
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">is.perf <- function(n){
if (n==0|n==1) return(FALSE)
s <- seq (1,n-1)
Line 3,490:
# Usage - Warning High Memory Usage
is.perf(28)
sapply(c(6,28,496,8128,33550336),is.perf)</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(require math)
 
Line 3,503:
; filtering to only even numbers for better performance
(filter perfect? (filter even? (range 1e5)))
;-> '(0 6 28 496 8128)</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
Naive (very slow) version
<syntaxhighlight lang="raku" perl6line>sub is-perf($n) { $n == [+] grep $n %% *, 1 .. $n div 2 }
 
# used as
put ((1..Inf).hyper.grep: {.&is-perf})[^4];</langsyntaxhighlight>
{{out}}
<pre>6 28 496 8128</pre>
Much, much faster version:
<syntaxhighlight lang="raku" perl6line>my @primes = lazy (2,3,*+2 … Inf).grep: { .is-prime };
my @perfects = lazy gather for @primes {
my $n = 2**$_ - 1;
Line 3,521:
}
 
.put for @perfects[^12];</langsyntaxhighlight>
 
{{out}}
Line 3,538:
 
=={{header|REBOL}}==
<langsyntaxhighlight lang="rebol">perfect?: func [n [integer!] /local sum] [
sum: 0
repeat i (n - 1) [
Line 3,546:
]
sum = n
]</langsyntaxhighlight>
 
=={{header|REXX}}==
===Classic REXX version of ooRexx===
This version is a '''Classic Rexx''' version of the '''ooRexx''' program as of 14-Sep-2013.
<langsyntaxhighlight lang="rexx">/*REXX version of the ooRexx program (the code was modified to run with Classic REXX).*/
do i=1 to 10000 /*statement changed: LOOP ──► DO*/
if perfectNumber(i) then say i "is a perfect number"
Line 3,562:
if n//i==0 then sum=sum+i /*statement changed: sum += i */
end
return sum=n</langsyntaxhighlight>
'''output''' &nbsp; when using the default of 10000:
<pre>
Line 3,574:
This version is a '''Classic REXX''' version of the '''PL/I''' program as of 14-Sep-2013, &nbsp; a REXX &nbsp; '''say''' &nbsp; statement
<br>was added to display the perfect numbers. &nbsp; Also, an epilog was written for the re-worked function.
<langsyntaxhighlight lang="rexx">/*REXX version of the PL/I program (code was modified to run with Classic REXX). */
parse arg low high . /*obtain the specified number(s).*/
if high=='' & low=='' then high=34000000 /*if no arguments, use a range. */
Line 3,590:
if n//i==0 then sum=sum+i /*I is a factor of N, so add it.*/
end /*i*/
return sum=n /*if the sum matches N, perfect! */</langsyntaxhighlight>
'''output''' &nbsp; when using the input defaults of: &nbsp; <tt> 1 &nbsp; 10000 </tt>
 
Line 3,600:
:::* &nbsp; testing bypasses the test of the first and last factors
:::* &nbsp; the &nbsp; ''corresponding factor'' &nbsp; is also used when a factor is found
<langsyntaxhighlight lang="rexx">/*REXX program tests if a number (or a range of numbers) is/are perfect. */
parse arg low high . /*obtain optional arguments from the CL*/
if high=='' & low=="" then high=34000000 /*if no arguments, then use a range. */
Line 3,620:
s = s + j + x%j /* ··· add it and the other factor. */
end /*j*/ /*(above) is marginally faster. */
return s==x /*if the sum matches X, it's perfect! */</langsyntaxhighlight>
'''output''' &nbsp; when using the default inputs:
<pre>
Line 3,635:
===optimized using digital root===
This REXX version makes use of the fact that all &nbsp; ''known'' &nbsp; perfect numbers > 6 have a &nbsp; ''digital root'' &nbsp; of &nbsp; '''1'''.
<langsyntaxhighlight lang="rexx">/*REXX program tests if a number (or a range of numbers) is/are perfect. */
parse arg low high . /*obtain the specified number(s). */
if high=='' & low=="" then high=34000000 /*if no arguments, then use a range. */
Line 3,662:
s = s + j + x%j /*··· add it and the other factor. */
end /*j*/ /*(above) is marginally faster. */
return s==x /*if the sum matches X, it's perfect! */</langsyntaxhighlight>
'''output''' &nbsp; is the same as the traditional version &nbsp; and is about &nbsp; '''5.3''' &nbsp; times faster &nbsp; (testing '''34,000,000''' numbers).
 
===optimized using only even numbers===
This REXX version uses the fact that all &nbsp; ''known'' &nbsp; perfect numbers are &nbsp; ''even''.
<langsyntaxhighlight lang="rexx">/*REXX program tests if a number (or a range of numbers) is/are perfect. */
parse arg low high . /*obtain optional arguments from the CL*/
if high=='' & low=="" then high=34000000 /*if no arguments, then use a range. */
Line 3,695:
s = s + j + x%j /* ··· add it and the other factor. */
end /*j*/ /*(above) is marginally faster. */
return s==x /*if sum matches X, then it's perfect!*/</langsyntaxhighlight>
'''output''' &nbsp; is the same as the traditional version &nbsp; and is about &nbsp; '''11.5''' &nbsp; times faster &nbsp; (testing '''34,000,000''' numbers).
 
===Lucas-Lehmer method===
This version uses memoization to implement a fast version of the Lucas-Lehmer test.
<langsyntaxhighlight lang="rexx">/*REXX program tests if a number (or a range of numbers) is/are perfect. */
parse arg low high . /*obtain the optional arguments from CL*/
if high=='' & low=="" then high=34000000 /*if no arguments, then use a range. */
Line 3,732:
s=s + j + x%j /*··· add it and the other factor. */
end /*j*/ /*(above) is marginally faster. */
return s==x /*if the sum matches X, it's perfect!*/</langsyntaxhighlight>
'''output''' &nbsp; is the same as the traditional version &nbsp; and is about &nbsp; '''75''' &nbsp; times faster &nbsp; (testing '''34,000,000''' numbers).
 
Line 3,742:
 
An integer square root function was added to limit the factorization of a number.
<langsyntaxhighlight lang="rexx">/*REXX program tests if a number (or a range of numbers) is/are perfect. */
parse arg low high . /*obtain optional arguments from the CL*/
if high=='' & low=="" then high=34000000 /*No arguments? Then use a range. */
Line 3,788:
if x//j==0 then s=s+j+x%j /*J divisible by X? Then add J and X÷J*/
end /*j*/
return s==x /*if the sum matches X, then perfect! */</langsyntaxhighlight>
'''output''' &nbsp; is the same as the traditional version &nbsp; and is about &nbsp; '''500''' &nbsp; times faster &nbsp; (testing '''34,000,000''' numbers). <br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
for i = 1 to 10000
if perfect(i) see i + nl ok
Line 3,804:
if sum = n return 1 else return 0 ok
return sum
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def perf(n)
sum = 0
for i in 1...n
Line 3,813:
end
sum == n
end</langsyntaxhighlight>
Functional style:
<langsyntaxhighlight lang="ruby">def perf(n)
n == (1...n).select {|i| n % i == 0}.inject(:+)
end</langsyntaxhighlight>
Faster version:
<langsyntaxhighlight lang="ruby">def perf(n)
divisors = []
for i in 1..Integer.sqrt(n)
Line 3,825:
end
divisors.uniq.inject(:+) == 2*n
end</langsyntaxhighlight>
Test:
<langsyntaxhighlight lang="ruby">for n in 1..10000
puts n if perf(n)
end</langsyntaxhighlight>
{{out}}
<pre>
Line 3,839:
===Fast (Lucas-Lehmer)===
Generate and memoize perfect numbers as needed.
<langsyntaxhighlight lang="ruby">require "prime"
 
def mersenne_prime_pow?(p)
Line 3,863:
p perfect?(13164036458569648337239753460458722910223472318386943117783728128)
p Time.now - t1
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,873:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">for i = 1 to 10000
if perf(i) then print i;" ";
next i
Line 3,882:
next i
IF sum = n THEN perf = 1
END FUNCTION</langsyntaxhighlight>
{{Out}}
<pre>6 28 496 8128</pre>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
fn main ( ) {
fn factor_sum(n: i32) -> i32 {
Line 3,909:
perfect_nums(10000);
}
</syntaxhighlight>
</lang>
 
=={{header|SASL}}==
Copied from the SASL manual, page 22:
<syntaxhighlight lang="sasl">
<lang SASL>
|| The function which takes a number and returns a list of its factors (including one but excluding itself)
|| can be written
Line 3,920:
|| we can write the list of all perfect numbers as
perfects = { n <- 1... ; n = sum(factors n) }
</syntaxhighlight>
</lang>
 
=={{header|S-BASIC}}==
<langsyntaxhighlight lang="basic">
$lines
 
Line 3,963:
 
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,975:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">def perfectInt(input: Int) = ((2 to sqrt(input).toInt).collect {case x if input % x == 0 => x + input / x}).sum == input - 1</langsyntaxhighlight>
 
'''or'''
 
<langsyntaxhighlight lang="scala">def perfect(n: Int) =
(for (x <- 2 to n/2 if n % x == 0) yield x).sum + 1 == n
</syntaxhighlight>
</lang>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(define (perf n)
(let loop ((i 1)
(sum 0))
Line 3,992:
(loop (+ i 1) (+ sum i)))
(else
(loop (+ i 1) sum)))))</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func boolean: isPerfect (in integer: n) is func
Line 4,026:
end if;
end for;
end func;</langsyntaxhighlight>
{{Out}}
<pre>
Line 4,037:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_perfect(n) {
n.sigma == 2*n
}
Line 4,043:
for n in (1..10000) {
say n if is_perfect(n)
}</langsyntaxhighlight>
 
Alternatively, a more efficient check for even perfect numbers:
<langsyntaxhighlight lang="ruby">func is_even_perfect(n) {
 
var square = (8*n + 1)
Line 4,059:
for n in (1..10000) {
say n if is_even_perfect(n)
}</langsyntaxhighlight>
 
{{out}}
Line 4,070:
 
=={{header|Simula}}==
<langsyntaxhighlight lang="simula">BOOLEAN PROCEDURE PERF(N); INTEGER N;
BEGIN
INTEGER SUM;
Line 4,077:
SUM := SUM + I;
PERF := SUM = N;
END PERF;</langsyntaxhighlight>
 
=={{header|Slate}}==
<langsyntaxhighlight lang="slate">n@(Integer traits) isPerfect
[
(((2 to: n // 2 + 1) select: [| :m | (n rem: m) isZero])
inject: 1 into: #+ `er) = n
].</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
<langsyntaxhighlight lang="smalltalk">Integer extend [
 
"Translation of the C version; this is faster..."
Line 4,107:
inject: 1 into: [ :a :b | a + b ] ) = self
]
].</langsyntaxhighlight>
 
<langsyntaxhighlight lang="smalltalk">1 to: 9000 do: [ :p | (p isPerfect) ifTrue: [ p printNl ] ]</langsyntaxhighlight>
 
=={{header|Swift}}==
{{trans|Java}}
<langsyntaxhighlight Swiftlang="swift">func perfect(n:Int) -> Bool {
var sum = 0
for i in 1..<n {
Line 4,127:
println(i)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,137:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc perfect n {
set sum 0
for {set i 1} {$i <= $n} {incr i} {
Line 4,143:
}
expr {$sum == 2*$n}
}</langsyntaxhighlight>
 
=={{header|Ursala}}==
<langsyntaxhighlight Ursalalang="ursala">#import std
#import nat
 
is_perfect = ~&itB&& ^(~&,~&t+ iota); ^E/~&l sum:-0+ ~| not remainder</langsyntaxhighlight>
This test program applies the function to a list of the first five hundred natural
numbers and deletes the imperfect ones.
<langsyntaxhighlight Ursalalang="ursala">#cast %nL
 
examples = is_perfect*~ iota 500</langsyntaxhighlight>
{{Out}}
<pre><6,28,496></pre>
Line 4,161:
{{trans|Phix}}
Using [[Factors_of_an_integer#VBA]], slightly adapted.
<langsyntaxhighlight lang="vb">Private Function Factors(x As Long) As String
Application.Volatile
Dim i As Long
Line 4,189:
If is_perfect(i) Then Debug.Print i
Next i
End Sub</langsyntaxhighlight>{{out}}
<pre> 6
28
Line 4,196:
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vb">Function IsPerfect(n)
IsPerfect = False
i = n - 1
Line 4,212:
 
WScript.StdOut.Write IsPerfect(CInt(WScript.Arguments(0)))
WScript.StdOut.WriteLine</langsyntaxhighlight>
 
{{out}}
Line 4,228:
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang="vlang">fn compute_perfect(n i64) bool {
mut sum := i64(0)
for i := i64(1); i < n; i++ {
Line 4,255:
}
}
}</langsyntaxhighlight>
{{Out}}
<pre>
Line 4,268:
{{trans|D}}
Restricted to the first four perfect numbers as the fifth one is very slow to emerge.
<langsyntaxhighlight lang="ecmascript">var isPerfect = Fn.new { |n|
if (n <= 2) return false
var tot = 1
Line 4,291:
i = i + 2 // there are no known odd perfect numbers
}
System.print()</langsyntaxhighlight>
 
{{out}}
Line 4,301:
{{libheader|Wren-math}}
This makes use of the fact that all known perfect numbers are of the form <big> (2<sup>''n''</sup> - 1) × 2<sup>''n'' - 1</sup></big> where <big> (2<sup>''n''</sup> - 1)</big> is prime and finds the first seven perfect numbers instantly. The numbers are too big after that to be represented accurately by Wren.
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
 
var isPerfect = Fn.new { |n|
Line 4,330:
p = p + 1
}
System.print()</langsyntaxhighlight>
 
{{out}}
Line 4,338:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
 
func Perfect(N); \Return 'true' if N is a perfect number
Line 4,355:
if Perfect(N) then [IntOut(0, N); CrLf(0)];
];
]</langsyntaxhighlight>
 
{{out}}
Line 4,369:
=={{header|Yabasic}}==
{{trans|True BASIC}}
<langsyntaxhighlight lang="basic">
sub isPerfect(n)
if (n < 2) or mod(n, 2) = 1 then return false : endif
Line 4,386:
print
end
</syntaxhighlight>
</lang>
 
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">
<lang Zig>
const std = @import("std");
const expect = std.testing.expect;
Line 4,420:
expect(propersum(30) == 42);
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 4,427:
=={{header|zkl}}==
{{trans|D}}
<langsyntaxhighlight lang="zkl">fcn isPerfectNumber1(n)
{ n == [1..n-1].filter('wrap(i){ n % i == 0 }).sum(); }</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.