Abundant, deficient and perfect number classifications: Difference between revisions

Content added Content deleted
(→‎{{header|Wren}}: Minor tidy & corrected alternative version.)
(→‎{{header|JavaScript}}: Added another alternative, translated from Lua)
Line 3,207:
perfect: 4
abundant: 4953</pre>
 
{{Trans|Lua}}
<syntaxhighlight lang="javascript">
// classify the numbers 1 : 20 000 as abudant, deficient or perfect
let abundantCount = 0
let deficientCount = 0
let perfectCount = 0
const maxNumber = 20000
// construct a table of the proper divisor sums
let pds = []
pds[ 1 ] = 0
for( let i = 2; i <= maxNumber; i ++ ){ pds[ i ] = 1 }
for( let i = 2; i <= maxNumber; i ++ )
{
for( let j = i + i; j <= maxNumber; j+= i ){ pds[ j ] = pds[ j ] + i }
}
// classify the numbers
for( let n = 1; n <= maxNumber; n ++ )
{
if( pds[ n ] < n )
{
deficientCount = deficientCount + 1
}
else if( pds[ n ] == n )
{
perfectCount = perfectCount + 1
}
else // pds[ n ] > n
{
abundantCount = abundantCount + 1
}
}
console.log( "abundant " + abundantCount.toString() )
console.log( "deficient " + deficientCount.toString() )
console.log( "perfect " + perfectCount.toString() )
</syntaxhighlight>
{{out}}
<pre>
abundant 4953
deficient 15043
perfect 4
</pre>
 
=={{header|jq}}==