Perfect numbers: Difference between revisions
Content added Content deleted
(add JavaScript) |
m (Fixed lang tags.) |
||
Line 11: | Line 11: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
⚫ | |||
⚫ | |||
⚫ | |||
Sum : Natural := 0; |
Sum : Natural := 0; |
||
begin |
begin |
||
Line 21: | Line 20: | ||
end loop; |
end loop; |
||
return Sum = N; |
return Sum = N; |
||
end Is_Perfect; |
end Is_Perfect;</lang> |
||
⚫ | |||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
<lang algol68>PROC is perfect = (INT candidate)BOOL: ( |
|||
INT sum :=1; |
|||
FOR f1 FROM 2 TO ENTIER ( sqrt(candidate)*(1+2*small real) ) WHILE |
|||
IF candidate MOD f1 = 0 THEN |
|||
sum +:= f1; |
|||
INT f2 = candidate OVER f1; |
|||
IF f2 > f1 THEN |
|||
sum +:= f2 |
|||
FI |
|||
FI; |
|||
# WHILE # sum <= candidate DO |
|||
SKIP |
|||
OD; |
|||
sum=candidate |
|||
); |
|||
# test # |
|||
FOR i FROM 2 TO 33550336 DO |
|||
IF is perfect(i) THEN print((i, new line)) FI |
|||
OD</lang> |
|||
OD |
|||
Output: |
Output: |
||
<lang algol68> +6 |
|||
+28 |
|||
+496 |
|||
+8128 |
|||
+33550336</lang> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
This will find the first 8 perfect numbers. |
This will find the first 8 perfect numbers. |
||
<lang |
<lang autohotkey>Loop, 30 { |
||
Loop, 30 { |
|||
If isMersennePrime(A_Index + 1) |
If isMersennePrime(A_Index + 1) |
||
res .= "Perfect number: " perfectNum(A_Index + 1) "`n" |
res .= "Perfect number: " perfectNum(A_Index + 1) "`n" |
||
Line 76: | Line 73: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
⚫ | |||
⚫ | |||
⚫ | |||
BEGIN{for(i=1;i<10000;i++)if(perf(i))print i}' |
BEGIN{for(i=1;i<10000;i++)if(perf(i))print i}' |
||
6 |
6 |
||
Line 129: | Line 125: | ||
return 0; |
return 0; |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 154: | Line 149: | ||
sum += i ; |
sum += i ; |
||
return ( ( sum == 2 * number ) || ( sum - number == number ) ) ; |
return ( ( sum == 2 * number ) || ( sum - number == number ) ) ; |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
⚫ | |||
<lang clojure> |
|||
⚫ | |||
(if (< n 4) |
(if (< n 4) |
||
'(1) |
'(1) |
||
Line 166: | Line 159: | ||
(defn perfect? [n] |
(defn perfect? [n] |
||
(== (reduce + (proper-divisors n)) n) |
(== (reduce + (proper-divisors n)) n) |
||
⚫ | |||
) |
|||
</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 176: | Line 168: | ||
=={{header|D}}== |
=={{header|D}}== |
||
Based on the Algol version: |
Based on the Algol version: |
||
<lang d> |
<lang d>import std.math: sqrt; |
||
import std.math: sqrt; |
|||
bool perfect(int n) { |
bool perfect(int n) { |
||
Line 200: | Line 191: | ||
if (perfect(n)) |
if (perfect(n)) |
||
printf("%d\n", n); |
printf("%d\n", n); |
||
⚫ | |||
} |
|||
</lang> |
|||
=={{header|E}}== |
=={{header|E}}== |
||
Line 216: | Line 206: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<lang erlang> |
<lang erlang>is_perfect(X) -> |
||
⚫ | |||
is_perfect(X) -> |
|||
⚫ | |||
</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<lang forth>: perfect? ( n -- ? ) |
|||
1 |
|||
over 2/ 1+ 2 ?do |
|||
over i mod 0= if i + then |
|||
loop |
|||
= ;</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
<lang fortran> |
<lang fortran>FUNCTION isPerfect(n) |
||
LOGICAL :: isPerfect |
|||
INTEGER, INTENT(IN) :: n |
|||
INTEGER :: i, factorsum |
|||
isPerfect = .FALSE. |
|||
factorsum = 1 |
|||
DO i = 2, INT(SQRT(REAL(n))) |
|||
IF(MOD(n, i) == 0) factorsum = factorsum + i + (n / i) |
|||
END DO |
|||
IF (factorsum == n) isPerfect = .TRUE. |
|||
END FUNCTION isPerfect</lang> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Line 259: | Line 247: | ||
8128</pre> |
8128</pre> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<lang haskell>perf n = n == sum [i | i <- [1..n-1], n `mod` i == 0]</lang> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
<lang j>is_perfect=: = [: +/ ((0=]|[)i.) # i.</lang> |
|||
The program defined above, like programs found here in other languages, assumes that the input will be a scalar positive integer. |
The program defined above, like programs found here in other languages, assumes that the input will be a scalar positive integer. |
||
Examples of use, including extensions beyond those assumptions: |
Examples of use, including extensions beyond those assumptions: |
||
is_perfect 33550336 |
<lang j> is_perfect 33550336 |
||
1 |
|||
}.I. is_perfect"0 i. 10000 |
|||
6 28 496 8128 |
|||
] zero_through_twentynine =. i. 3 10 |
|||
0 1 2 3 4 5 6 7 8 9 |
|||
10 11 12 13 14 15 16 17 18 19 |
|||
20 21 22 23 24 25 26 27 28 29 |
|||
is_pos_int=: 0&< *. ]=>. |
|||
(is_perfect"0 *. is_pos_int) zero_through_twentynine |
|||
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</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 331: | Line 319: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
<lang logo> |
<lang logo>to perfect? :n |
||
to perfect? :n |
|||
output equal? :n apply "sum filter [equal? 0 modulo :n ?] iseq 1 :n/2 |
output equal? :n apply "sum filter [equal? 0 modulo :n ?] iseq 1 :n/2 |
||
end |
end</lang> |
||
</lang> |
|||
=={{header|M4}}== |
=={{header|M4}}== |
||
<lang M4> |
<lang M4>define(`for', |
||
define(`for', |
|||
`ifelse($#,0,``$0'', |
`ifelse($#,0,``$0'', |
||
`ifelse(eval($2<=$3),1, |
`ifelse(eval($2<=$3),1, |
||
Line 357: | Line 342: | ||
for(`x',`2',`33550336', |
for(`x',`2',`33550336', |
||
`ifelse(isperfect(x),1,`x |
`ifelse(isperfect(x),1,`x |
||
')') |
')')</lang> |
||
</lang> |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |
||
Custom function: |
Custom function: |
||
<lang Mathematica> |
<lang Mathematica>PerfectQ[i_Integer] := Total[Divisors[i]] == 2 i</lang> |
||
PerfectQ[i_Integer] := Total[Divisors[i]] == 2 i |
|||
</lang> |
|||
Examples (testing 496, testing 128, finding all perfect numbers in 1...10000): |
Examples (testing 496, testing 128, finding all perfect numbers in 1...10000): |
||
<lang Mathematica> |
<lang Mathematica>PerfectQ[496] |
||
PerfectQ[128] |
|||
⚫ | |||
PerfectQ[128] |
|||
⚫ | |||
</lang> |
|||
gives back: |
gives back: |
||
<lang Mathematica> |
<lang Mathematica>True |
||
⚫ | |||
True |
|||
⚫ | |||
⚫ | |||
⚫ | |||
</lang> |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
< |
<lang maxscript>fn isPerfect n = |
||
( |
( |
||
local sum = 0 |
local sum = 0 |
||
Line 390: | Line 368: | ||
) |
) |
||
sum == n |
sum == n |
||
)</ |
)</lang> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
Line 440: | Line 418: | ||
=={{header|R}}== |
=={{header|R}}== |
||
<lang R> |
<lang R>is.perf <- function(n){ |
||
is.perf <- function(n){ |
|||
if (n==0|n==1) return(FALSE) |
if (n==0|n==1) return(FALSE) |
||
s <- seq (1,n-1) |
s <- seq (1,n-1) |
||
Line 451: | Line 428: | ||
# Usage - Warning High Memory Usage |
# Usage - Warning High Memory Usage |
||
is.perf(28) |
is.perf(28) |
||
sapply(c(6,28,496,8128,33550336),is.perf) |
sapply(c(6,28,496,8128,33550336),is.perf)</lang> |
||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<lang ruby>def perf(n) |
<lang ruby>def perf(n) |
||
Line 480: | Line 456: | ||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
<lang slate> |
<lang slate>n@(Integer traits) isPerfect |
||
n@(Integer traits) isPerfect |
|||
[ |
[ |
||
(((2 to: n // 2 + 1) select: [| :m | (n rem: m) isZero]) |
(((2 to: n // 2 + 1) select: [| :m | (n rem: m) isZero]) |
||
inject: 1 into: #+ `er) = n |
inject: 1 into: #+ `er) = n |
||
]. |
].</lang> |
||
</lang> |
|||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
Line 524: | Line 498: | ||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
<lang Ursala> |
<lang Ursala>#import std |
||
#import std |
|||
#import nat |
#import nat |
||