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
, 3 months agoAdd ABC
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Not a robot (talk | contribs) (Add ABC) |
||
(34 intermediate revisions by 14 users not shown) | |||
Line 28:
=={{header|11l}}==
{{trans|Kotlin}}
<syntaxhighlight lang="11l">F sum_proper_divisors(n)
R I n < 2 {0} E sum((1 .. n I/ 2).filter(it -> (@n % it) == 0))
Line 58:
For maximum compatibility, this program uses only the basic instruction set (S/360)
with 2 ASSIST macros (XDECO,XPRNT).
<syntaxhighlight lang="360asm">* Abundant, deficient and perfect number 08/05/2016
ABUNDEFI CSECT
USING ABUNDEFI,R13 set base register
Line 119:
</pre>
=={{header|8086 Assembly}}==
<syntaxhighlight lang="asm">LIMIT: equ 20000
cpu 8086
org 100h
Line 207:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang=
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program numberClassif64.s */
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.
<syntaxhighlight lang=
CARD i,j
Line 747 ⟶ 780:
[[http://rosettacode.org/wiki/Proper_divisors#Ada]].
<syntaxhighlight lang=
procedure ADB_Classification is
Line 792 ⟶ 825:
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">BEGIN # classify the numbers 1 : 20 000 as abudant, deficient or perfect #
INT abundant count := 0;
INT deficient count := 0;
INT perfect count := 0;
INT max number = 20 000;
# construct a table of the proper divisor sums #
Line 809 ⟶ 839:
# classify the numbers #
FOR n TO max number DO
ELIF pd sum = n THEN
ELSE # pd sum > n #
FI
OD;
print( ( "abundant ", whole( abundant count, 0 ), newline ) );
print( ( "deficient ", whole( deficient count, 0 ), newline ) );
print( ( "perfect ", whole( perfect count, 0 ), newline ) )
END
</syntaxhighlight>
{{out}}
<pre>
abundant 4953
deficient 15043
perfect 4
</pre>
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin % count abundant, perfect and deficient numbers up to 20 000 %
integer MAX_NUMBER;
MAX_NUMBER := 20000;
Line 885 ⟶ 893:
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">on aliquotSum(n)
if (n < 2) then return 0
set sum to 1
Line 920 ⟶ 928:
{{output}}
<syntaxhighlight lang="applescript">{deficient:15043, perfect:4, abundant:4953}</syntaxhighlight>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang=
/* ARM assembly Raspberry PI */
/* program numberClassif.s */
Line 1,386 ⟶ 1,394:
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">properDivisors: function [n]->
(factors n) -- n
Line 1,409 ⟶ 1,417:
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">Loop
{
m := A_index
Line 1,478 ⟶ 1,486:
=={{header|AWK}}==
works with GNU Awk 3.1.5 and with BusyBox v1.21.1
<syntaxhighlight lang=
#!/bin/gawk -f
function sumprop(num, i,sum,root) {
Line 1,527 ⟶ 1,535:
=={{header|Batch File}}==
As batch files aren't particularly well-suited to increasingly large arrays of data, this code will chew through processing power.
<syntaxhighlight lang="dos">
@echo off
setlocal enabledelayedexpansion
Line 1,564 ⟶ 1,572:
=={{header|BASIC}}==
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
<syntaxhighlight lang="basic">10 DEFINT A-Z: LM=20000
20 DIM P(LM)
30 FOR I=1 TO LM: P(I)=-32767: NEXT
Line 1,579 ⟶ 1,591:
PERFECT: 4
ABUNDANT: 4953</pre>
==={{header|BASIC256}}===
<syntaxhighlight lang="vb">deficient = 0
perfect = 0
abundant = 0
for n = 1 to 20000
sum = SumProperDivisors(n)
begin case
case sum < n
deficient += 1
case sum = n
perfect += 1
else
abundant += 1
end case
next
print "The classification of the numbers from 1 to 20,000 is as follows :"
print
print "Deficient = "; deficient
print "Perfect = "; perfect
print "Abundant = "; abundant
end
function SumProperDivisors(number)
if number < 2 then return 0
sum = 0
for i = 1 to number \ 2
if number mod i = 0 then sum += i
next i
return sum
end function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">100 cls
110 defic = 0
120 perfe = 0
130 abund = 0
140 for n = 1 to 20000
150 sump = SumProperDivisors(n)
160 if sump < n then
170 defic = defic+1
180 else
190 if sump = n then
200 perfe = perfe+1
210 else
220 if sump > n then abund = abund+1
230 endif
240 endif
250 next
260 print "The classification of the numbers from 1 to 20,000 is as follows :"
270 print
280 print "Deficient = ";defic
290 print "Perfect = ";perfe
300 print "Abundant = ";abund
310 end
320 function SumProperDivisors(number)
330 if number < 2 then SumProperDivisors = 0
340 sum = 0
350 for i = 1 to number/2
360 if number mod i = 0 then sum = sum+i
370 next i
380 SumProperDivisors = sum
390 end function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Public Sub Main()
Dim sum As Integer, deficient As Integer, perfect As Integer, abundant As Integer
For n As Integer = 1 To 20000
sum = SumProperDivisors(n)
If sum < n Then
deficient += 1
Else If sum = n Then
perfect += 1
Else
abundant += 1
Endif
Next
Print "The classification of the numbers from 1 to 20,000 is as follows : \n"
Print "Deficient = "; deficient
Print "Perfect = "; perfect
Print "Abundant = "; abundant
End
Function SumProperDivisors(number As Integer) As Integer
If number < 2 Then Return 0
Dim sum As Integer = 0
For i As Integer = 1 To number \ 2
If number Mod i = 0 Then sum += i
Next
Return sum
End Function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
The [[#BASIC|BASIC]] solution works without any changes.
==={{header|QBasic}}===
The [[#BASIC|BASIC]] solution works without any changes.
==={{header|Run BASIC}}===
<syntaxhighlight lang="vb">function sumProperDivisors(num)
if num > 1 then
sum = 1
root = sqr(num)
for i = 2 to root
if num mod i = 0 then
sum = sum + i
if (i*i) <> num then sum = sum + num / i
end if
next i
end if
sumProperDivisors = sum
end function
deficient = 0
perfect = 0
abundant = 0
print "The classification of the numbers from 1 to 20,000 is as follows :"
for n = 1 to 20000
sump = sumProperDivisors(n)
if sump < n then
deficient = deficient +1
else
if sump = n then
perfect = perfect +1
else
if sump > n then abundant = abundant +1
end if
end if
next n
print "Deficient = "; deficient
print "Perfect = "; perfect
print "Abundant = "; abundant</syntaxhighlight>
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">LET lm = 20000
DIM s(0)
MAT REDIM s(lm)
FOR i = 1 TO lm
LET s(i) = -32767
NEXT i
FOR i = 1 TO lm/2
FOR j = i+i TO lm STEP i
LET s(j) = s(j) +i
NEXT j
NEXT i
FOR i = 1 TO lm
LET x = i - 32767
IF s(i) < x THEN
LET d = d +1
ELSE
IF s(i) = x THEN
LET p = p +1
ELSE
LET a = a +1
END IF
END IF
NEXT i
PRINT "The classification of the numbers from 1 to 20,000 is as follows :"
PRINT
PRINT "Deficient ="; d
PRINT "Perfect ="; p
PRINT "Abundant ="; a
END</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
manifest $( maximum = 20000 $)
Line 1,621 ⟶ 1,821:
This is not a particularly efficient implementation, so unless you're using a compiler, you can expect it to take a good few minutes to complete. But you can always test with a shorter range of numbers by replacing the 20000 (<tt>"2":*8*</tt>) near the start of the first line.
<syntaxhighlight lang="befunge">p0"2":*8*>::2/\:2/\28*:*:**+>::28*:*:*/\28*:*:*%%#v_\:28*:*:*%v>00p:0`\0\`-1v
++\1-:1`#^_$:28*:*:*/\28*vv_^#<<<!%*:*:*82:-1\-1\<<<\+**:*:*82<+>*:*:**\2-!#+
v"There are "0\g00+1%*:*:<>28*:*:*/\28*:*:*/:0\`28*:*:**+-:!00g^^82!:g01\p01<
Line 1,632 ⟶ 1,832:
=={{header|Bracmat}}==
Two solutions are given. The first solution first decomposes the current number into a multiset of prime factors and then constructs the proper divisors. The second solution finds proper divisors by checking all candidates from 1 up to the square root of the given number. The first solution is a few times faster, because establishing the prime factors of a small enough number (less than 2^32 or less than 2^64, depending on the bitness of Bracmat) is fast.
<syntaxhighlight lang="bracmat">( clk$:?t0
& ( multiples
= prime multiplicity
Line 1,712 ⟶ 1,912:
=={{header|C}}==
<syntaxhighlight lang="c">
#include<stdio.h>
#define de 0
Line 1,770 ⟶ 1,970:
Third method:
:Uses a loop with a inner Enumerable.Range reaching to i / 2, only adding one divisor at a time.
<syntaxhighlight lang="csharp">using System;
using System.Linq;
Line 1,843 ⟶ 2,043:
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iostream>
#include <algorithm>
#include <vector>
Line 1,883 ⟶ 2,083:
=={{header|Ceylon}}==
<syntaxhighlight lang="ceylon">shared void run() {
function divisors(Integer int) =>
Line 1,903 ⟶ 2,103:
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">(defn pad-class
[n]
(let [divs (filter #(zero? (mod n %)) (range 1 n))
Line 1,923 ⟶ 2,123:
Example:
<syntaxhighlight lang="clojure">(count-classes 20000)
;=> {:perfect 4,
; :abundant 4953,
Line 1,929 ⟶ 2,129:
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Generate proper divisors from 1 to max
proper_divisors = proc (max: int) returns (array[int])
divs: array[int] := array[int]$fill(1, max, 0)
Line 1,971 ⟶ 2,171:
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">(defun number-class (n)
(let ((divisor-sum (sum-divisors n)))
(cond ((< divisor-sum n) :deficient)
Line 1,998 ⟶ 2,198:
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
const MAXIMUM := 20000;
Line 2,041 ⟶ 2,241:
=={{header|D}}==
<syntaxhighlight lang="d">void main() /*@safe*/ {
import std.stdio, std.algorithm, std.range;
Line 2,065 ⟶ 2,265:
See [[#Pascal]].
=={{header|Draco}}==
<syntaxhighlight lang="draco">/* Fill a given array such that for each N,
* P[n] is the sum of proper divisors of N */
proc nonrec propdivs([*] word p) void:
Line 2,111 ⟶ 2,311:
{{trans|C#}}
<syntaxhighlight lang="dyalect">func sieve(bound) {
var (a, d, p) = (0, 0, 0)
var sum = Array.Empty(bound + 1, 0)
Line 2,180 ⟶ 2,380:
<pre>Abundant: 4953, Deficient: 15043, Perfect: 4
Abundant: 4953, Deficient: 15043, Perfect: 4</pre>
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight lang=easylang>
func sumprop num .
if num < 2
return 0
.
i = 2
sum = 1
root = sqrt num
while i < root
if num mod i = 0
sum += i + num / i
.
i += 1
.
if num mod root = 0
sum += root
.
return sum
.
for j = 1 to 20000
sump = sumprop j
if sump < j
deficient += 1
elif sump = j
perfect += 1
else
abundant += 1
.
.
print "Perfect: " & perfect
print "Abundant: " & abundant
print "Deficient: " & deficient
</syntaxhighlight>
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
(lib 'math) ;; sum-divisors function
Line 2,213 ⟶ 2,450:
{{trans|Haskell}}
<syntaxhighlight lang="ela">open monad io number list
divisors n = filter ((0 ==) << (n `mod`)) [1 .. (n `div` 2)]
Line 2,232 ⟶ 2,469:
=={{header|Elena}}==
{{trans|C#}}
ELENA
<syntaxhighlight lang="elena">import extensions;
classifyNumbers(int bound, ref int abundant, ref int deficient, ref int perfect)
Line 2,242 ⟶ 2,479:
int[] sum := new int[](bound + 1);
for(int divisor := 1
{
for(int i := divisor + divisor
{
sum[i] := sum[i] + divisor
Line 2,250 ⟶ 2,487:
};
for(int i := 1
{
int t := sum[i];
Line 2,290 ⟶ 2,527:
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">defmodule Proper do
def divisors(1), do: []
def divisors(n), do: [1 | divisors(2,n,:math.sqrt(n))] |> Enum.sort
Line 2,316 ⟶ 2,553:
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
-module(properdivs).
-export([divs/1,sumdivs/1,class/1]).
Line 2,364 ⟶ 2,601:
===Erlang 2===
The version above is not tail-call recursive, and so cannot classify large ranges. Here is a more optimal solution.
<syntaxhighlight lang="erlang">
-module(proper_divisors).
-export([classify_range/2]).
Line 2,402 ⟶ 2,639:
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang=
let mutable a=0
let mutable b=0
Line 2,427 ⟶ 2,664:
An immutable solution.
<syntaxhighlight lang="fsharp">
let deficient, perfect, abundant = 0,1,2
Line 2,444 ⟶ 2,681:
=={{header|Factor}}==
<syntaxhighlight lang="factor">
USING: fry math.primes.factors math.ranges ;
: psum ( n -- m ) divisors but-last sum ;
Line 2,463 ⟶ 2,700:
=={{header|Forth}}==
{{works with|Gforth|0.7.3}}
<syntaxhighlight lang=
: SLOT ( x y -- 0|1|2) OVER OVER < -ROT > - 1+ ;
: CLASSIFY ( n -- n') \ 0 == deficient, 1 == perfect, 2 == abundant
Line 2,505 ⟶ 2,742:
Abundant 4953
<syntaxhighlight lang=
MODULE FACTORSTUFF !This protocol evades the need for multiple parameters, or COMMON, or one shapeless main line...
Concocted by R.N.McLean, MMXV.
Line 2,547 ⟶ 2,784:
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
' FreeBASIC v1.05.0 win64
Line 2,593 ⟶ 2,830:
=={{header|Frink}}==
<syntaxhighlight lang="frink">
d = new dict
for n = 1 to 20000
Line 2,612 ⟶ 2,849:
Abundant: 4953
</pre>
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn SumProperDivisors( number as long ) as long
long i, result, sum = 0
if number < 2 then exit fn = 0
for i = 1 to number / 2
if number mod i == 0 then sum += i
next
result = sum
end fn = result
void local fn NumberCategories( limit as long )
long i, sum, deficient = 0, perfect = 0, abundant = 0
for i = 1 to limit
sum = fn SumProperDivisors(i)
if sum < i then deficient++ : continue
if sum == i then perfect++ : continue
abundant++
next
printf @"\nClassification of integers from 1 to %ld is:\n", limit
printf @"Deficient = %ld\nPerfect = %ld\nAbundant = %ld", deficient, perfect, abundant
printf @"-----------------\nTotal = %ld\n", deficient + perfect + abundant
end fn
CFTimeInterval t
t = fn CACurrentMediaTime
fn NumberCategories( 20000 )
printf @"Compute time: %.3f ms",(fn CACurrentMediaTime-t)*1000
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Classification of integers from 1 to 20000 is:
Deficient = 15043
Perfect = 4
Abundant = 4953
-----------------
Total = 20000
Compute time: 1761.443 ms
</pre>
=={{header|GFA Basic}}==
<syntaxhighlight lang="text">
num_deficient%=0
num_perfect%=0
Line 2,671 ⟶ 2,955:
=={{header|Go}}==
<syntaxhighlight lang="go">package main
import "fmt"
Line 2,712 ⟶ 2,996:
=====Solution:=====
Uses the "factorize" closure from [[Factors of an integer]]
<syntaxhighlight lang=
def n = factors.pop()
def fSum = factors.sum()
Line 2,733 ⟶ 3,017:
=={{header|Haskell}}==
<syntaxhighlight lang=
divisors n = filter ((0 ==) . (n `mod`)) [1 .. (n `div` 2)]
Line 2,753 ⟶ 3,037:
Or, a little faster and more directly, as a single fold:
<syntaxhighlight lang="haskell">import Data.Numbers.Primes (primeFactors)
import Data.List (group, sort)
Line 2,779 ⟶ 3,063:
[[Proper divisors#J|Supporting implementation]]:
<syntaxhighlight lang=
properDivisors=: factors -. ]</syntaxhighlight>
We can subtract the sum of a number's proper divisors from itself to classify the number:
<syntaxhighlight lang=
1 1 2 1 4 0 6 1 5 2</syntaxhighlight>
Except, we are only concerned with the sign of this difference:
<syntaxhighlight lang=
1 1 1 1 1 0 1 1 1 1 1 _1 1 1 1 1 1 _1 1 _1 1 1 1 _1 1 1 1 0 1 _1</syntaxhighlight>
Also, we do not care about the individual classification but only about how many numbers fall in each category:
<syntaxhighlight lang=
15043 4 4953</syntaxhighlight>
Line 2,801 ⟶ 3,085:
How do we know which is which? We look at the unique values (which are arranged by their first appearance, scanning the list left to right):
<syntaxhighlight lang=
1 0 _1</syntaxhighlight>
Line 2,808 ⟶ 3,092:
=={{header|Java}}==
{{works with|Java|8}}
<syntaxhighlight lang="java">import java.util.stream.LongStream;
public class NumberClassifications {
Line 2,843 ⟶ 3,127:
===ES5===
<syntaxhighlight lang=
for (var ds=0, d=1, e=n/2+1; d<e; d+=1) if (n%d==0) ds+=d
dpa[ds<n ? 0 : ds==n ? 1 : 2]+=1
Line 2,849 ⟶ 3,133:
document.write('Deficient:',dpa[0], ', Perfect:',dpa[1], ', Abundant:',dpa[2], '<br>' )</syntaxhighlight>
'''Or:'''
<syntaxhighlight lang=
for (var ds=1, d=2, e=Math.sqrt(n); d<e; d+=1) if (n%d==0) ds+=d+n/d
if (n%e==0) ds+=e
Line 2,856 ⟶ 3,140:
document.write('Deficient:',dpa[0], ', Perfect:',dpa[1], ', Abundant:',dpa[2], '<br>' )</syntaxhighlight>
'''Or:'''
<syntaxhighlight lang=
var ps = {2:true, 3:true}
next: for (var n=5, i=2; n<=t; n+=i, i=6-i) {
Line 2,909 ⟶ 3,193:
===ES6===
{{Trans|Haskell}}
<syntaxhighlight lang=
'use strict';
Line 2,956 ⟶ 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}}==
{{works with|jq|1.4}}
The definition of proper_divisors is taken from [[Proper_divisors#jq]]:
<syntaxhighlight lang="jq"># unordered
def proper_divisors:
. as $n
Line 2,972 ⟶ 3,299:
end;</syntaxhighlight>
'''The task:'''
<syntaxhighlight lang="jq">def sum(stream): reduce stream as $i (0; . + $i);
def classify:
Line 2,981 ⟶ 3,308:
reduce (range(1; 20001) | classify) as $c ({}; .[$c] += 1 )</syntaxhighlight>
{{out}}
<syntaxhighlight lang="sh">$ jq -n -c -f AbundantDeficientPerfect.jq
{"deficient":15043,"perfect":4,"abundant":4953}</syntaxhighlight>
Line 2,987 ⟶ 3,314:
From Javascript ES5 entry.
<syntaxhighlight lang="javascript">/* Classify Deficient, Perfect and Abdundant integers */
function classifyDPA(stop:number, start:number=0, step:number=1):array {
var dpa = [1, 0, 0];
Line 3,022 ⟶ 3,349:
<code>divisorsum</code> calculates the sum of aliquot divisors. It uses <code>pcontrib</code> to calculate the contribution of each prime factor.
<syntaxhighlight lang=
function pcontrib(p::Int64, a::Int64)
n = one(p)
Line 3,047 ⟶ 3,374:
Use a three element array, <code>iclass</code>, rather than three separate variables to tally the classifications. Take advantage of the fact that the sign of <code>divisorsum(n) - n</code> depends upon its class to increment <code>iclass</code>. 1 is a difficult case, it is deficient by convention, so I manually add its contribution and start the accumulation with 2. All primes are deficient, so I test for those and tally accordingly, bypassing <code>divisorsum</code>.
<syntaxhighlight lang=
const L = 2*10^4
iclasslabel = ["Deficient", "Perfect", "Abundant"]
Line 3,074 ⟶ 3,401:
Abundant, 4953
</code>
=== Using Primes versions >= 0.5.4 ===
Recent revisions of the Primes package include a divisors() which returns divisors of n including 1 and n.
<syntaxhighlight lamg = "julia">using Primes
""" Return tuple of (perfect, abundant, deficient) counts from 1 up to nmax """
function per_abu_def_classify(nmax::Int)
results = [0, 0, 0]
for n in 1:nmax
results[sign(sum(divisors(n)) - 2 * n) + 2] += 1
end
return (perfect, abundant, deficient) = results
end
let MAX = 20_000
NPE, NAB, NDE = per_abu_def_classify(MAX)
println("$NPE perfect, $NAB abundant, and $NDE deficient numbers in 1:$MAX.")
end
</syntaxhighlight>{{out}}<pre>4 perfect, 4953 abundant, and 15043 deficient numbers in 1:20000.</pre>
=={{header|K}}==
{{works with|Kona}}
<syntaxhighlight lang="k">
/Classification of numbers into abundant, perfect and deficient
/ numclass.k
Line 3,089 ⟶ 3,436:
`0: ,"Abundant = ", $(#c[2])
</syntaxhighlight>
{{works with|ngn/k}}<syntaxhighlight lang="k">/Classification of numbers into abundant, perfect and deficient
/ numclass.k
/return 0,1 or -1 if perfect or abundant or deficient respectively
numclass: {s:(+/&~(!1+x)!\:x)-x; $[s>x;:1;$[s<x;:-1;:0]]}
/classify numbers from 1 to 20000 into respective groups
c: =numclass' 1+!20000
/print statistics
`0: ,"Deficient = ", $(#c[-1])
`0: ,"Perfect = ", $(#c[0])
`0: ,"Abundant = ", $(#c[1])
</syntaxhighlight>
(indentation optional, used to emphasize lines which are not comment lines)
{{out}}
<pre>
Line 3,094 ⟶ 3,460:
Perfect = 4
Abundant = 4953
</pre>
=={{header|Kotlin}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="scala">// version 1.1
fun sumProperDivisors(n: Int) =
Line 3,135 ⟶ 3,500:
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
print "ROSETTA CODE - Abundant, deficient and perfect number classifications"
print
Line 3,211 ⟶ 3,576:
=={{header|Lua}}==
===Summing the factors using modulo/division===
<syntaxhighlight lang="lua">function sumDivs (n)
if n < 2 then return 0 end
local sum, sr = 1, math.sqrt(n)
Line 3,237 ⟶ 3,603:
Deficient: 15043
Perfect: 4</pre>
===Summing the factors using a table===
{{Trans|ALGOL 68}}
<syntaxhighlight lang="lua">
do -- classify the numbers 1 : 20 000 as abudant, deficient or perfect
local abundantCount = 0
local deficientCount = 0
local perfectCount = 0
local maxNumber = 20000
-- construct a table of the proper divisor sums
local pds = {}
pds[ 1 ] = 0
for i = 2, maxNumber do pds[ i ] = 1 end
for i = 2, maxNumber do
for j = i + i, maxNumber, i do pds[ j ] = pds[ j ] + i end
end
-- classify the numbers
for n = 1, maxNumber do
local pdSum = pds[ n ]
if pdSum < n then
deficientCount = deficientCount + 1
elseif pdSum == n then
perfectCount = perfectCount + 1
else -- pdSum > n
abundantCount = abundantCount + 1
end
end
io.write( "abundant ", abundantCount, "\n" )
io.write( "deficient ", deficientCount, "\n" )
io.write( "perfect ", perfectCount, "\n" )
end
</syntaxhighlight>
{{out}}
<pre>
abundant 4953
deficient 15043
perfect 4
</pre>
=={{header|MAD}}==
<syntaxhighlight lang=
DIMENSION P(20000)
MAX = 20000
Line 3,267 ⟶ 3,671:
=={{header|Maple}}==
<syntaxhighlight lang=
if evalb(NumberTheory:-SumOfDivisors(n) < 2*n) then
return "Deficient";
Line 3,286 ⟶ 3,690:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=
StringJoin[
Line 3,297 ⟶ 3,701:
=={{header|MatLab}}==
<syntaxhighlight lang=
abundant=0; deficient=0; perfect=0; p=[];
for N=2:20000
Line 3,321 ⟶ 3,725:
Perfect 4
Abundant 4953
</pre>
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Given a number it returns wether it is perfect, deficient or abundant */
number_class(n):=if divsum(n)-n=n then "perfect" else if divsum(n)-n<n then "deficient" else if divsum(n)-n>n then "abundant"$
/* Function that displays the number of each kind below n */
classification_count(n):=block(makelist(number_class(i),i,1,n),
[[length(sublist(%%,lambda([x],x="deficient")))," deficient"],[length(sublist(%%,lambda([x],x="perfect")))," perfect"],[length(sublist(%%,lambda([x],x="abundant")))," abundant"]])$
/* Test case */
classification_count(20000);
</syntaxhighlight>
{{out}}
<pre>
[[15043," deficient"],[4," perfect"],[4953," abundant"]]
</pre>
=={{header|MiniScript}}==
{{Trans|Lua|Summing the factors using a table}}
<syntaxhighlight lang="miniscript">
// classify the numbers 1 : 20 000 as abudant, deficient or perfect
abundantCount = 0
deficientCount = 0
perfectCount = 0
maxNumber = 20000
// construct a table of the proper divisor sums
pds = [0] * ( maxNumber + 1 )
pds[ 1 ] = 0
for i in range( 2, maxNumber )
pds[ i ] = 1
end for
for i in range( 2, maxNumber )
for j in range( i + i, maxNumber, i )
pds[ j ] = pds[ j ] + i
end for
end for
// classify the numbers
for n in range( 1, maxNumber )
pdSum = pds[ n ]
if pdSum < n then
deficientCount = deficientCount + 1
else if pdSum == n then
perfectCount = perfectCount + 1
else // pdSum > n
abundantCount = abundantCount + 1
end if
end for
print "abundant " + abundantCount
print "deficient " + deficientCount
print "perfect " + perfectCount</syntaxhighlight>
{{out}}
<pre>
abundant 4953
deficient 15043
perfect 4
</pre>
=={{header|ML}}==
==={{header|mLite}}===
<syntaxhighlight lang="ocaml">fun proper
(number, count, limit, remainder, results) where (count > limit) = rev results
| (number, count, limit, remainder, results) =
Line 3,358 ⟶ 3,819:
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE ADP;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 3,407 ⟶ 3,868:
=={{header|NewLisp}}==
<syntaxhighlight lang=
;;; The list (1 .. n-1) of integers is generated
;;; then each non-divisor of n is replaced by 0
Line 3,437 ⟶ 3,898:
=={{header|Nim}}==
<syntaxhighlight lang="nim">
proc sumProperDivisors(number: int) : int =
if number < 2 : return 0
Line 3,475 ⟶ 3,936:
=={{header|Oforth}}==
<syntaxhighlight lang=
Integer method: properDivs -- []
Line 3,503 ⟶ 3,964:
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">classify(k)=
{
my(v=[0,0,0],t);
Line 3,517 ⟶ 3,978:
=={{header|Pascal}}==
==={{header|Free Pascal}}===
{{libheader|PrimTrial}}
search for "UNIT for prime decomposition".
<syntaxhighlight lang="pascal">program KindOfN; //[deficient,perfect,abundant]
{$IFDEF FPC}
{$ENDIF}
{$IFDEF WINDOWS} {$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils
{$IFDEF WINDOWS},Windows{$ENDIF}
//alternative copy and paste PrimeDecomp.inc for TIO.RUN
{$I PrimeDecomp.inc}
type
tKindIdx = 0..2;//[deficient,perfect,abundant];
tKind = array[tKindIdx] of Uint64;
procedure
var
pPrimeDecomp :tpPrimeFac;
SumOfKind : tKind;
n: NativeUInt;
c: NativeInt;
T0:Int64;
Begin
writeln('Limit: ',LIMIT);
T0 := GetTickCount64;
fillchar(SumOfKind,SizeOf(SumOfKind),#0);
n := 1;
Init_Sieve(n);
repeat
c := pPrimeDecomp^.pfSumOfDivs-2*n;
c := ORD(c>0)-ORD(c<0)+1;//sgn(c)+1
inc(SumOfKind[c]);
until n
writeln('deficient: ',SumOfKind[0]);
writeln('abundant: ',SumOfKind[2]);
writeln('perfect: ',SumOfKind[1]);
writeln('runtime ',T0/1000:0:3,' s');
writeln;
end;
Begin
InitSmallPrimes; //using PrimeDecomp.inc
GetKind(20000);
GetKind(10*1000*1000);
GetKind(524*1000*1000);
end.</syntaxhighlight>{{out|@TIO.RUN}}
<pre>Limit: 20000
deficient: 15043
abundant: 4953
perfect: 4
runtime 0.003 s
Limit: 1000000
deficient: 752451
abundant: 247545
perfect: 4
runtime 0.052 s
Limit: 524000000
deficient: 394250308
abundant: 129749687
perfect: 5
runtime 32.987 s
Real time: 33.203 s User time: 32.881 s Sys. time: 0.048 s CPU share: 99.17 %
</pre>
Line 3,763 ⟶ 4,057:
1 is classified as a [[wp:Deficient_number|deficient number]], 6 is a [[wp:Perfect_number|perfect number]], 12 is an [[wp:Abundant_number|abundant number]]. As per task spec, also showing the totals for the first 20,000 numbers.
<syntaxhighlight lang="perl">use ntheory qw/divisor_sum/;
my @type = <Perfect Abundant Deficient>;
say join "\n", map { sprintf "%2d %s", $_, $type[divisor_sum($_)-$_ <=> $_] } 1..12;
Line 3,787 ⟶ 4,081:
===Not using a module===
Everything as above, but done more slowly with <code>div_sum</code> providing sum of proper divisors.
<syntaxhighlight lang="perl">sub div_sum {
my($n) = @_;
my $sum = 0;
Line 3,801 ⟶ 4,095:
=={{header|Phix}}==
<!--<syntaxhighlight lang=
<span style="color: #004080;">integer</span> <span style="color: #000000;">deficient</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">perfect</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">abundant</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">N</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">20000</span> <span style="color: #008080;">do</span>
Line 3,821 ⟶ 4,115:
=={{header|Picat}}==
<syntaxhighlight lang=
Classes = new_map([deficient=0,perfect=0,abundant=0]),
foreach(N in 1..20_000)
Line 3,874 ⟶ 4,168:
=={{header|PicoLisp}}==
<syntaxhighlight lang=
(if (assoc Key (val Var))
(con @ (inc (cdr @)))
Line 3,923 ⟶ 4,217:
=={{header|PL/I}}==
<syntaxhighlight lang="pli">*process source xref;
apd: Proc Options(main);
p9a=time();
Line 3,988 ⟶ 4,282:
=={{header|PL/M}}==
<syntaxhighlight lang="pli">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 4,038 ⟶ 4,332:
=={{header|PowerShell}}==
{{works with|PowerShell|2}}
<syntaxhighlight lang=
function Get-ProperDivisorSum ( [int]$N )
{
Line 4,082 ⟶ 4,376:
===As a single function===
Using the <code>Get-ProperDivisorSum</code> as a helper function in an advanced function:
<syntaxhighlight lang=
function Get-NumberClassification
{
Line 4,140 ⟶ 4,434:
}
</syntaxhighlight>
<syntaxhighlight lang=
1..20000 | Get-NumberClassification
</syntaxhighlight>
Line 4,153 ⟶ 4,447:
=={{header|Processing}}==
<syntaxhighlight lang="processing">void setup() {
int deficient = 0, perfect = 0, abundant = 0;
for (int i = 1; i <= 20000; i++) {
Line 4,185 ⟶ 4,479:
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">
proper_divisors(1, []) :- !.
proper_divisors(N, [1|L]) :-
Line 4,239 ⟶ 4,533:
=={{header|PureBasic}}==
<syntaxhighlight lang=
EnableExplicit
Line 4,290 ⟶ 4,584:
===Python: Counter===
Importing [[Proper_divisors#Python:_From_prime_factors|Proper divisors from prime factors]]:
<syntaxhighlight lang="python">>>> from proper_divisors import proper_divs
>>> from collections import Counter
>>>
Line 4,317 ⟶ 4,611:
{{Works with|Python|3.7}}
In terms of a single fold:
<syntaxhighlight lang="python">'''Abundant, deficient and perfect number classifications'''
from itertools import accumulate, chain, groupby, product
Line 4,429 ⟶ 4,723:
and the main function could be rewritten in terms of an nthArrow abstraction:
<syntaxhighlight lang="python"># nthArrow :: (a -> b) -> Tuple -> Int -> Tuple
def nthArrow(f):
'''A simple function lifted to one which applies to a
Line 4,443 ⟶ 4,737:
as something like:
<syntaxhighlight lang="python"># deficientPerfectAbundantCountsUpTo :: Int -> (Int, Int, Int)
def deficientPerfectAbundantCountsUpTo(n):
'''Counts of deficient, perfect, and abundant
Line 4,464 ⟶ 4,758:
=== The Simple Way ===
<syntaxhighlight lang="python">pn = 0
an = 0
dn = 0
Line 4,497 ⟶ 4,791:
:Initialize the sum to 1 and start checking factors from 2 and up, which cuts one iteration from each factor checking loop, (a 19,999 iteration savings).<br>
Resulting optimized code is thirty five times faster than the simplified code, and not nearly as complicated as the ''Counter'' or ''Reduce'' methods (as this optimized method requires no imports, other than ''time'' for the performance comparison to ''the simple way'').
<syntaxhighlight lang="python">from time import time
st = time()
pn, an, dn = 0, 0, 0
Line 4,554 ⟶ 4,848:
<code>dpa</code> returns 0 if n is deficient, 1 if n is perfect and 2 if n is abundant.
<syntaxhighlight lang=
[ factors -1 pluck
Line 4,583 ⟶ 4,877:
{{Works with|R|3.3.2 and above}}
<syntaxhighlight lang="r">
# Abundant, deficient and perfect number classifications. 12/10/16 aev
require(numbers);
Line 4,615 ⟶ 4,909:
=={{header|Racket}}==
<syntaxhighlight lang="racket">#lang racket
(require math)
(define (proper-divisors n) (drop-right (divisors n) 1))
Line 4,641 ⟶ 4,935:
(formerly Perl 6)
{{Works with|rakudo|2018.12}}
<syntaxhighlight lang=
my @l = 1 if x > 1;
(2 .. x.sqrt.floor).map: -> \d {
Line 4,655 ⟶ 4,949:
=={{header|REXX}}==
===version 1===
<syntaxhighlight lang="rexx">/*REXX program counts the number of abundant/deficient/perfect numbers within a range.*/
parse arg low high . /*obtain optional arguments from the CL*/
high=word(high low 20000,1); low= word(low 1,1) /*obtain the LOW and HIGH values.*/
Line 4,693 ⟶ 4,987:
" 100k " " " '''20%''' "
" 1m " " " '''30%''' "
<syntaxhighlight lang="rexx">/*REXX program counts the number of abundant/deficient/perfect numbers within a range.*/
parse arg low high . /*obtain optional arguments from the CL*/
high=word(high low 20000,1); low=word(low 1, 1) /*obtain the LOW and HIGH values.*/
Line 4,724 ⟶ 5,018:
===version 2===
<syntaxhighlight lang="rexx">/* REXX */
Call time 'R'
cnt.=0
Line 4,783 ⟶ 5,077:
=={{header|Ring}}==
The following classifies the first few numbers of each type.
<syntaxhighlight lang="ring">
n = 30
perfect(n)
Line 4,799 ⟶ 5,094:
next
</syntaxhighlight>
===Task using modulo/division===
{{Trans|Lua|Summing the factors using modulo/division}}
<syntaxhighlight lang="ring">
a = 0
d = 0
p = 0
for n = 1 to 20000
Pn = sumDivs(n)
if Pn > n a = a + 1 ok
if Pn < n d = d + 1 ok
if Pn = n p = p + 1 ok
next
see "Abundant : " + a + nl
see "Deficient: " + d + nl
see "Perfect : " + p + nl
func sumDivs (n)
if n < 2 return 0
else
sum = 1
sr = sqrt(n)
for d = 2 to sr
if n % d = 0
sum = sum + d
if d != sr sum = sum + n / d ok
ok
next
return sum
ok
</syntaxhighlight>
{{out}}
<pre>
Abundant : 4953
Deficient: 15043
Perfect : 4
</pre>
===Task using a table===
{{Trans|Lus|Summiing the factors using a table}}
<syntaxhighlight lang="ring">
maxNumber = 20000
abundantCount = 0
deficientCount = 0
perfectCount = 0
pds = list( maxNumber )
pds[ 1 ] = 0
for i = 2 to maxNumber pds[ i ] = 1 next
for i = 2 to maxNumber
for j = i + i to maxNumber step i pds[ j ] = pds[ j ] + i next
next
for n = 1 to maxNumber
pdSum = pds[ n ]
if pdSum < n
deficientCount = deficientCount + 1
but pdSum = n
perfectCount = perfectCount + 1
else # pdSum > n
abundantCount = abundantCount + 1
ok
next
see "Abundant : " + abundantCount + nl
see "Deficient: " + deficientCount + nl
see "Perfect : " + perfectCount + nl
</syntaxhighlight>
{{out}}
<pre>
Abundant : 4953
Deficient: 15043
Perfect : 4
</pre>
=={{header|RPL}}==
{{works with|HP|49}}
≪ [1 0 0]
2 20000 '''FOR''' n
n DIVIS REVLIST TAIL <span style="color:grey">@ get the list of divisors of n excluding n</span>
0. + <span style="color:grey">@ avoid ∑LIST and SIGN errors when n is prime </span>
∑LIST n - SIGN 2 + <span style="color:grey">@ turn P(n)-n into 1, 2 or 3</span>
DUP2 GET 1 + PUT <span style="color:grey">@ increment appropriate array element</span>
'''NEXT'''
≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: [15042 4 4953]
</pre>
=={{header|Ruby}}==
{{Works with|ruby|2.7}}
With [[proper_divisors#Ruby]] in place:
<syntaxhighlight lang="ruby">res = (1 .. 20_000).map{|n| n.proper_divisors.sum <=> n }.tally
puts "Deficient: #{res[-1]} Perfect: #{res[0]} Abundant: #{res[1]}"
</syntaxhighlight>
Line 4,813 ⟶ 5,198:
With [[proper_divisors#Rust]] in place:
<syntaxhighlight lang="rust">fn main() {
// deficient starts at 1 because 1 is deficient but proper_divisors returns
// and empty Vec
Line 4,842 ⟶ 5,227:
=={{header|Scala}}==
<syntaxhighlight lang=
def classifier(i: Int) = properDivisors(i).sum compare i
val groups = (1 to 20000).groupBy( classifier )
Line 4,854 ⟶ 5,239:
=={{header|Scheme}}==
<syntaxhighlight lang="scheme">
(define (classify n)
(define (sum_of_factors x)
Line 4,882 ⟶ 5,267:
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
const func integer: sumProperDivisors (in integer: number) is func
Line 4,928 ⟶ 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}}==
<syntaxhighlight lang="ruby">func propdivsum(n) { n.sigma - n }
var h = Hash()
Line 4,942 ⟶ 5,350:
=={{header|Swift}}==
{{trans|C}}
<syntaxhighlight lang="swift">var deficients = 0 // sumPd < n
var perfects = 0 // sumPd = n
var abundants = 0 // sumPd > n
Line 4,987 ⟶ 5,395:
=={{header|Tcl}}==
<syntaxhighlight lang=
if {$n == 1} {return 0}
set divs 1
Line 5,072 ⟶ 5,480:
=={{header|uBasic/4tH}}==
This is about the limit of what is feasible with uBasic/4tH performance wise, since a full run takes over 5 minutes.
<syntaxhighlight lang="text">P = 0 : D = 0 : A = 0
For n= 1 to 20000
Line 5,130 ⟶ 5,538:
=={{header|Vala}}==
{{trans|C}}
<syntaxhighlight lang="vala">enum Classification {
DEFICIENT,
PERFECT,
Line 5,171 ⟶ 5,579:
=={{header|VBA}}==
<syntaxhighlight lang=
Option Explicit
Line 5,212 ⟶ 5,620:
=={{header|VBScript}}==
<syntaxhighlight lang=
Perfect = 0
Abundant = 0
Line 5,242 ⟶ 5,650:
=={{header|Visual Basic .NET}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Module Module1
Function SumProperDivisors(number As Integer) As Integer
Line 5,282 ⟶ 5,690:
Abundant = 4953</pre>
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn p_fac_sum(i int) int {
mut sum := 0
for p := 1; p <= i/2; p++ {
Line 5,321 ⟶ 5,729:
=={{header|VTL-2}}==
<syntaxhighlight lang=
20 I=1
30 :I)=0
Line 5,358 ⟶ 5,766:
=={{header|Wren}}==
===Using modulo/division===
{{libheader|Wren-math}}
<syntaxhighlight lang=
var d = 0
Line 5,383 ⟶ 5,792:
There are 4953 abundant numbers between 1 and 20000
There are 4 perfect numbers between 1 and 20000
</pre>
===Using a table===
Alternative version, computing a table of divisor sums.
{{Trans|Lua|Summing the factors using a table}}
<syntaxhighlight lang="wren">var maxNumber = 20000
var abundantCount = 0
var deficientCount = 0
var perfectCount = 0
var pds = []
pds.add(0) // element 0
pds.add(0) // element 1
for (i in 2..maxNumber) {
pds.add(1)
}
for (i in 2..maxNumber) {
var j = i + i
while (j <= maxNumber) {
pds[j] = pds[j] + i
j = j + i
}
}
for (n in 1..maxNumber) {
var pdSum = pds[n]
if (pdSum < n) {
deficientCount = deficientCount + 1
} else if (pdSum == n) {
perfectCount = perfectCount + 1
} else { // pdSum > n
abundantCount = abundantCount + 1
}
}
System.print("Abundant : %(abundantCount)")
System.print("Deficient: %(deficientCount)")
System.print("Perfect : %(perfectCount)")</syntaxhighlight>
{{out}}
<pre>
Abundant : 4953
Deficient: 15043
Perfect : 4
</pre>
=={{header|XPL0}}==
<syntaxhighlight lang=
[CntD:= 0; CntP:= 0; CntA:= 0;
for Num:= 1 to 20000 do
Line 5,412 ⟶ 5,865:
=={{header|Yabasic}}==
{{trans|AWK}}
<syntaxhighlight lang=
Deficient = 0
Line 5,450 ⟶ 5,903:
=={{header|zkl}}==
{{trans|D}}
<syntaxhighlight lang="zkl">fcn properDivs(n){ [1.. (n + 1)/2 + 1].filter('wrap(x){ n%x==0 and n!=x }) }
fcn classify(n){
Line 5,467 ⟶ 5,920:
=={{header|ZX Spectrum Basic}}==
Solution 1:
<syntaxhighlight lang="zxbasic"> 10 LET nd=1: LET np=0: LET na=0
20 FOR i=2 TO 20000
30 LET sum=1
Line 5,484 ⟶ 5,937:
Solution 2 (more efficient):
<syntaxhighlight lang="zxbasic"> 10 LET abundant=0: LET deficient=0: LET perfect=0
20 FOR j=1 TO 20000
30 GO SUB 120
|