Perfect numbers: Difference between revisions

m
(→‎{{header|Phix}}: use pygments, extended range and added cheat version)
imported>Arakov
(4 intermediate revisions by 2 users not shown)
Line 1,625:
 
=={{header|Elena}}==
ELENA 46.x:
<syntaxhighlight lang="elena">import system'routines;
import system'math;
Line 1,633:
{
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 3,127:
</pre>
=== cheating ===
{{trans|PicotPicat}}
<syntaxhighlight lang="phix">
include mpfr.e
atom t0 = time(), t1 = t0+1
mpz n = mpz_init(), m = mpz_init()
sequence validp = {2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607,
Line 3,137:
756839, 859433, 1257787, 1398269, 2976221, 3021377, 6972593,
13466917, 20996011, 24036583, 25964951, 30402457, 32582657,
37156667, 42643801, 43112609, 57885161},
74207281, 77232917, 82589933}
if platform()=JS then validp = validp[1..35] end if -- (keep it under 5s)
for p in validp do
Line 3,199 ⟶ 3,200:
43112609 50,076,715,684,982,3...,909,221,145,378,816 (25,956,377 digits) (24s)
57885161 169,296,395,301,618,...,179,626,270,130,176 (34,850,340 digits) (29s)
74207281 45,112,996,270,669,0...,008,557,930,315,776 (44,677,235 digits) (36s)
"29.4s"
77232917 10,920,015,213,433,6...,001,402,016,301,056 (46,498,850 digits) (43s)
82589933 1,108,477,798,641,48...,798,007,191,207,936 (49,724,095 digits) (50s)
"2950.4s6s"
</pre>
 
Line 4,656 ⟶ 4,660:
{{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,689 ⟶ 4,693:
{{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|
Anonymous user