Perfect numbers: Difference between revisions

Content added Content deleted
m (→‎cheating: removed unused var)
m (→‎{{header|Wren}}: Minor tidy)
Line 4,660: Line 4,660:
{{trans|D}}
{{trans|D}}
Restricted to the first four perfect numbers as the fifth one is very slow to emerge.
Restricted to the first four perfect numbers as the fifth one is very slow to emerge.
<syntaxhighlight lang="ecmascript">var isPerfect = Fn.new { |n|
<syntaxhighlight lang="wren">var isPerfect = Fn.new { |n|
if (n <= 2) return false
if (n <= 2) return false
var tot = 1
var tot = 1
Line 4,693: Line 4,693:
{{libheader|Wren-math}}
{{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.
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="ecmascript">import "/math" for Int
<syntaxhighlight lang="wren">import "./math" for Int


var isPerfect = Fn.new { |n|
var isPerfect = Fn.new { |n|