Perfect numbers: Difference between revisions

m
m (→‎cheating: removed unused var)
(2 intermediate revisions by 2 users not shown)
Line 1,556:
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
funcfastfunc perf n .
for i = 1 to n - 1
while i < n
if n mod i = 0
sum += i
.
i += 1
.
return if sum = n
return 1
.
return 0
.
for i = 2 to 10000
if perf i = 1
printwrite i & " "
.
.
</syntaxhighlight>
{{out}}
<pre>
6 28 496 8128
</pre>
 
=={{header|Eiffel}}==
Line 1,625 ⟶ 1,634:
 
=={{header|Elena}}==
ELENA 46.x:
<syntaxhighlight lang="elena">import system'routines;
import system'math;
Line 1,633 ⟶ 1,642:
{
isPerfect()
= new Range(1, self - 1).selectBy::(n => (self.mod:(n) == 0).iif(n,0) ).summarize(new Integer()) == self;
}
public program()
{
for(int n := 1,; n < 10000,; n += 1)
{
if(n.isPerfect())
Line 4,660 ⟶ 4,669:
{{trans|D}}
Restricted to the first four perfect numbers as the fifth one is very slow to emerge.
<syntaxhighlight lang="ecmascriptwren">var isPerfect = Fn.new { |n|
if (n <= 2) return false
var tot = 1
Line 4,693 ⟶ 4,702:
{{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.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var isPerfect = Fn.new { |n|
2,083

edits