Abundant, deficient and perfect number classifications: Difference between revisions
Abundant, deficient and perfect number classifications (view source)
Revision as of 16:24, 14 March 2024
, 2 months agoAdd ABC
(added Easylang) |
Not a robot (talk | contribs) (Add ABC) |
||
(6 intermediate revisions by 4 users not shown) | |||
Line 678:
</pre>
=={{header|ABC}}==
<syntaxhighlight lang="abc">PUT 0 IN deficient
PUT 0 IN perfect
PUT 0 IN abundant
HOW TO FIND PROPER DIVISOR SUMS UP TO limit:
SHARE p
PUT {} IN p
FOR i IN {0..limit}: PUT 0 IN p[i]
FOR i IN {1..floor (limit/2)}:
PUT i+i IN j
WHILE j <= limit:
PUT p[j]+i IN p[j]
PUT j+i IN j
HOW TO CLASSIFY n:
SHARE deficient, perfect, abundant, p
SELECT:
p[n] < n: PUT deficient+1 IN deficient
p[n] = n: PUT perfect+1 IN perfect
p[n] > n: PUT abundant+1 IN abundant
PUT 20000 IN limit
FIND PROPER DIVISOR SUMS UP TO limit
FOR n IN {1..limit}: CLASSIFY n
WRITE deficient, "deficient"/
WRITE perfect, "perfect"/
WRITE abundant, "abundant"/</syntaxhighlight>
{{out}}
<Pre>15043 deficient
4 perfect
4953 abundant</Pre>
=={{header|Action!}}==
Because of the memory limitation on the non-expanded Atari 8-bit computer the array containing Proper Divisor Sums is generated and used twice for the first and the second half of numbers separately.
Line 2,436 ⟶ 2,469:
=={{header|Elena}}==
{{trans|C#}}
ELENA
<syntaxhighlight lang="elena">import extensions;
Line 2,446 ⟶ 2,479:
int[] sum := new int[](bound + 1);
for(int divisor := 1
{
for(int i := divisor + divisor
{
sum[i] := sum[i] + divisor
Line 2,454 ⟶ 2,487:
};
for(int i := 1
{
int t := sum[i];
Line 3,207 ⟶ 3,240:
perfect: 4
abundant: 4953</pre>
{{Trans|Lua}}
<syntaxhighlight lang="javascript">
// classify the numbers 1 : 20 000 as abudant, deficient or perfect
"use strict"
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 ] += i }
}
// classify the numbers
for( let n = 1; n <= maxNumber; n ++ )
{
if( pds[ n ] < n )
{
deficientCount ++
}
else if( pds[ n ] == n )
{
perfectCount ++
}
else // pds[ n ] > n
{
abundantCount ++
}
}
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}}==
Line 5,237 ⟶ 5,313:
Abundant: 4953
</pre>
=={{header|SETL}}==
<syntaxhighlight lang="setl">program classifications;
P := properdivisorsums(20000);
print("Deficient:", #[n : n in [1..#P] | P(n) < n]);
print(" Perfect:", #[n : n in [1..#P] | P(n) = n]);
print(" Abundant:", #[n : n in [1..#P] | P(n) > n]);
proc properdivisorsums(n);
p := [0];
loop for i in [1..n] do
loop for j in [i*2, i*3..n] do
p(j) +:= i;
end loop;
end loop;
return p;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>Deficient: 15043
Perfect: 4
Abundant: 4953</pre>
=={{header|Sidef}}==
Line 5,669 ⟶ 5,768:
===Using modulo/division===
{{libheader|Wren-math}}
<syntaxhighlight lang="
var d = 0
Line 5,699 ⟶ 5,798:
{{Trans|Lua|Summing the factors using a table}}
<syntaxhighlight lang="
var abundantCount = 0
var deficientCount = 0
var perfectCount = 0
var pds
pds.add(
pds.add(
for (
pds.add(
}
for (
var j = i + i
while
pds[
j = j + i
}
}
for (
var pdSum = pds[
if
deficientCount = deficientCount + 1
} else if (
perfectCount
} else { // pdSum > n
abundantCount
}
}
System.print(
System.print(
System.print(
{{out}}
<pre>
Abundant :
Deficient:
Perfect : 4
</pre>
|