Perfect numbers: Difference between revisions

m
<lang> needs a language
(+ second D entry)
m (<lang> needs a language)
Line 20:
return Sum = N;
end Is_Perfect;</lang>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Revision 1 - no extensions to language used}}
 
{{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]}}
<lang algol68>PROC is perfect = (INT candidate)BOOL: (
Line 107 ⟶ 106:
=={{header|C}}==
{{trans|D}}
 
<lang c>#include "stdio.h"
#include "math.h"
Line 135 ⟶ 133:
return 0;
}</lang>
Using functions from [[Factors of an integer#Prime factoring]]:
 
Using functions from [[Factors of an integer#Prime factoring]]:<lang c>int main()
{
int j;
Line 154 ⟶ 152:
=={{header|C sharp|C#}}==
{{trans|C++}}
<lang csharp>static void Main(string[] args)
static void Main(string[] args)
{
Console.WriteLine("Perfect numbers from 1 to 33550337:");
Line 178 ⟶ 175:
 
return sum == num ;
}</lang>
}
</lang>
===Version using Lambdas, will only work from version 3 of C# on===
<lang csharp>static void Main(string[] args)
static void Main(string[] args)
{
Console.WriteLine("Perfect numbers from 1 to 33550337:");
Line 198 ⟶ 193:
{
return Enumerable.Range(1, num - 1).Sum(n => num % n == 0 ? n : 0 ) == num;
}</lang>
}
</lang>
 
=={{header|C++}}==
Line 241 ⟶ 235:
=={{header|CoffeeScript}}==
Optimized version, for fun.
<lang coffeescript>is_perfect_number = (n) ->
is_perfect_number = (n) ->
do_factors_add_up_to n, 2*n
Line 290 ⟶ 283:
for n in known_perfects
throw Error("fail") unless is_perfect_number(n)
throw Error("fail") if is_perfect_number(n+1)</lang>
</lang>
output
<langpre>
> coffee perfect_numbers.coffee
6
Line 299 ⟶ 291:
496
8128
</langpre>
 
=={{header|Common Lisp}}==
Line 338 ⟶ 330:
return n == reduce!((s, i) => n % i ? s : s+i)(iota(1, n-1));
}
 
 
void main() {
Line 347 ⟶ 338:
 
=={{header|E}}==
 
<lang e>pragma.enable("accumulator")
def isPerfectNumber(x :int) {
Line 371 ⟶ 361:
 
: perfect? ( n -- ? ) [ divisors sum ] [ 2 * ] bi = ;</lang>
 
=={{header|Forth}}==
<lang forth>: perfect? ( n -- ? )
Line 393 ⟶ 384:
IF (factorsum == n) isPerfect = .TRUE.
END FUNCTION isPerfect</lang>
 
=={{header|GAP}}==
<lang gap>Filtered([1 .. 10000], n -> Sum(DivisorsInt(n)) = 2*n);
# [ 6, 28, 496, 8128 ]</lang>
 
=={{header|Go}}==
<lang go>package main
Line 446 ⟶ 439:
n > 4 && (n == (2..Math.sqrt(n)).findAll { n % it == 0 }.inject(1) { factorSum, i -> factorSum += i + n/i })
}</lang>
 
Test program:
<lang groovy>(0..10000).findAll { isPerfect(it) }.each { println it }</lang>
 
Output:
<pre>6
Line 455 ⟶ 446:
496
8128</pre>
 
=={{header|Haskell}}==
<lang haskell>perf n = n == sum [i | i <- [1..n-1], n `mod` i == 0]</lang>
Line 571 ⟶ 563:
=={{header|K}}==
{{trans|J}}
<lang K> perfect:{(x>2)&x=+/-1_{d:&~x!'!1+_sqrt x;d,_ x%|d}x}
<lang K>
perfect:{(x>2)&x=+/-1_{d:&~x!'!1+_sqrt x;d,_ x%|d}x}
perfect 33550336
1
Line 587 ⟶ 578:
(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>
</lang>
 
=={{header|LabVIEW}}==
{{VI solution|LabVIEW_Perfect_numbers.png}}
 
=={{header|Liberty BASIC}}==
<lang lb>for n =1 to 10000
Line 617 ⟶ 608:
output equal? :n apply "sum filter [equal? 0 modulo :n ?] iseq 1 :n/2
end</lang>
 
=={{header|Lua}}==
<lang Lua>function isPerfect(x)
Line 674 ⟶ 666:
 
=={{header|Objeck}}==
<lang objeck>bundle Default {
bundle Default {
class Test {
function : Main(args : String[]) ~ Nil {
Line 697 ⟶ 688:
}
}
}</lang>
}
</lang>
 
=={{header|OCaml}}==
Line 748 ⟶ 738:
if(s==0 || p==2,
print("(2^"p"-1)2^("p"-1)=\t"n1*n"\n")));
p++; n1=n+1; n=2*n+1)</lang>
</lang>
output
<pre>(2^2-1)2^(2-1)= 6
Line 786 ⟶ 775:
writeln (candidate, ' is a perfect number.');
end.</lang>
 
output
<pre>
Line 815 ⟶ 803:
$n == sum(0, grep {$n % $_ == 0} 1..$n-1);
}</lang>
 
=={{header|Perl 6}}==
<lang perl6>sub perf($n) { $n == [+] grep $n %% *, 1 .. floor $n/2 }</lang>
Line 846 ⟶ 835:
 
=={{header|PL/I}}==
<lang PL/I>perfect: procedure (n) returns (bit(1));
perfect: procedure (n) returns (bit(1));
declare n fixed;
declare sum fixed;
Line 857 ⟶ 845:
end;
return (sum=n);
end perfect;</lang>
</lang>
 
=={{header|PowerShell}}==
Line 877 ⟶ 864:
 
=={{header|Prolog}}==
 
===Classic approach===
Works with SWI-Prolog
 
<lang Prolog>tt_divisors(X, N, TT) :-
Q is X / N,
Line 894 ⟶ 879:
perfect_numbers(N, L) :-
numlist(2, N, LN),
include(perfect, LN, L).</lang>
</lang>
 
===Functionnal 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
 
<lang Prolog>:- use_module(library(lambda)).
 
Line 936 ⟶ 919:
%% f_compose_1(Pred1, Pred2, Pred1(Pred2)).
%
f_compose_1(F,G, \X^Z^(call(G,X,Y), call(F,Y,Z))).</lang>
</lang>
 
=={{header|PureBasic}}==
Line 978 ⟶ 960:
 
=={{header|REBOL}}==
<lang rebol>perfect?: func [n [integer!] /local sum] [
perfect?: func [n [integer!] /local sum] [
sum: 0
repeat i (n - 1) [
Line 987 ⟶ 968:
]
sum = n
]</lang K>
]
</lang>
 
=={{header|REXX}}==
===version 1===
<lang rexx>/*REXX program to test if a number is perfect. */
<lang rexx>
/*REXX program to test if a number is perfect. */
 
arg low high .
Line 1,024 ⟶ 1,003:
end
 
return sum==x /*if the sum matches X, perfect! */</lang>
</lang>
Output (using the defaults):
<pre>
<pre style="height:15ex;overflow:scroll">
6 is a perfect number.
28 is a perfect number.
Line 1,034 ⟶ 1,012:
</pre>
===version 2===
<lang rexx>/*REXX program to test if a number is perfect. */
<lang rexx>
/*REXX program to test if a number is perfect. */
 
arg low high .
Line 1,078 ⟶ 1,055:
end
 
return sum==x /*if the sum matches X, perfect! */</lang>
</lang>
Output (using the defaults):
<pre>
<pre style="height:15ex;overflow:scroll">
6 is a perfect number.
28 is a perfect number.
Line 1,151 ⟶ 1,127:
end for;
end func;</lang>
 
Output:
<pre>
Line 1,169 ⟶ 1,144:
 
=={{header|Smalltalk}}==
 
<lang smalltalk>Integer extend [
 
Anonymous user