Aliquot sequence classifications: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 35: Line 35:
{{trans|Python}}
{{trans|Python}}


<syntaxhighlight lang=11l>F pdsum(n)
<syntaxhighlight lang="11l">F pdsum(n)
R sum((1 .. (n + 1) I/ 2).filter(x -> @n % x == 0 & @n != x))
R sum((1 .. (n + 1) I/ 2).filter(x -> @n % x == 0 & @n != x))


Line 103: Line 103:
=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang=AArch64 Assembly>
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program aliquotSeq64.s */
/* program aliquotSeq64.s */
Line 691: Line 691:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Assumes LONG INT is at least 64 bits, as in Algol 68G.
Assumes LONG INT is at least 64 bits, as in Algol 68G.
<syntaxhighlight lang=algol68>BEGIN
<syntaxhighlight lang="algol68">BEGIN
# aliquot sequence classification #
# aliquot sequence classification #
# maximum sequence length we consider #
# maximum sequence length we consider #
Line 827: Line 827:
=={{header|AppleScript}}==
=={{header|AppleScript}}==


<syntaxhighlight lang=applescript>on aliquotSum(n)
<syntaxhighlight lang="applescript">on aliquotSum(n)
if (n < 2) then return 0
if (n < 2) then return 0
set sum to 1
set sum to 1
Line 898: Line 898:


{{output}}
{{output}}
<syntaxhighlight lang=applescript>"
<syntaxhighlight lang="applescript">"
1: terminating 1, 0
1: terminating 1, 0
2: terminating 2, 1, 0
2: terminating 2, 1, 0
Line 925: Line 925:
=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang=ARM Assembly>
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
/* program aliquotSeq.s */
/* program aliquotSeq.s */
Line 1,500: Line 1,500:
=={{header|AWK}}==
=={{header|AWK}}==


<syntaxhighlight lang=awk>
<syntaxhighlight lang="awk">
#!/bin/gawk -f
#!/bin/gawk -f
function sumprop(num, i,sum,root) {
function sumprop(num, i,sum,root) {
Line 1,601: Line 1,601:
=={{header|BASIC256}}==
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<syntaxhighlight lang=BASIC256># Rosetta Code problem: http://rosettacode.org/wiki/Aliquot_sequence_classifications
<syntaxhighlight lang="basic256"># Rosetta Code problem: http://rosettacode.org/wiki/Aliquot_sequence_classifications
# by Jjuanhdez, 06/2022
# by Jjuanhdez, 06/2022


Line 1,658: Line 1,658:
===Brute Force===
===Brute Force===
The following implementation is a brute force method which takes a very, very long time for 15355717786080. To be fair to C, that's also true for many of the other implementations on this page which also implement the brute force method. See the next implementation for the best solution.
The following implementation is a brute force method which takes a very, very long time for 15355717786080. To be fair to C, that's also true for many of the other implementations on this page which also implement the brute force method. See the next implementation for the best solution.
<syntaxhighlight lang=C>
<syntaxhighlight lang="c">
#include<stdlib.h>
#include<stdlib.h>
#include<string.h>
#include<string.h>
Line 1,791: Line 1,791:
===Number Theoretic===
===Number Theoretic===
The following implementation, based on Number Theory, is the best solution for such a problem. All cases are handled, including 15355717786080, with all the numbers being processed and the output written to console practically instantaneously. The above brute force implementation is the original one and it remains to serve as a comparison of the phenomenal difference the right approach can make to a problem.
The following implementation, based on Number Theory, is the best solution for such a problem. All cases are handled, including 15355717786080, with all the numbers being processed and the output written to console practically instantaneously. The above brute force implementation is the original one and it remains to serve as a comparison of the phenomenal difference the right approach can make to a problem.
<syntaxhighlight lang=C>
<syntaxhighlight lang="c">
#include<string.h>
#include<string.h>
#include<stdlib.h>
#include<stdlib.h>
Line 1,960: Line 1,960:
=={{header|C++}}==
=={{header|C++}}==
This one follows the trail blazed by the "Number Theoretic" C example above.
This one follows the trail blazed by the "Number Theoretic" C example above.
<syntaxhighlight lang=cpp>#include <cstdint>
<syntaxhighlight lang="cpp">#include <cstdint>
#include <iostream>
#include <iostream>
#include <string>
#include <string>
Line 2,062: Line 2,062:
=={{header|CLU}}==
=={{header|CLU}}==
{{trans|C++}}
{{trans|C++}}
<syntaxhighlight lang=clu>% This program uses the 'bigint' cluster from PCLU's 'misc.lib'
<syntaxhighlight lang="clu">% This program uses the 'bigint' cluster from PCLU's 'misc.lib'


% Remove leading and trailing whitespace (bigint$unparse adds a lot)
% Remove leading and trailing whitespace (bigint$unparse adds a lot)
Line 2,187: Line 2,187:
=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Uses the Lisp function '''proper-divisors-recursive''' from [[Proper_divisors#Common_Lisp|Task:Proper Divisors]].
Uses the Lisp function '''proper-divisors-recursive''' from [[Proper_divisors#Common_Lisp|Task:Proper Divisors]].
<syntaxhighlight lang=lisp>(defparameter *nlimit* 16)
<syntaxhighlight lang="lisp">(defparameter *nlimit* 16)
(defparameter *klimit* (expt 2 47))
(defparameter *klimit* (expt 2 47))
(defparameter *asht* (make-hash-table))
(defparameter *asht* (make-hash-table))
Line 2,286: Line 2,286:
=={{header|D}}==
=={{header|D}}==
{{trans|Python}}
{{trans|Python}}
<syntaxhighlight lang=d>import std.stdio, std.range, std.algorithm, std.typecons, std.conv;
<syntaxhighlight lang="d">import std.stdio, std.range, std.algorithm, std.typecons, std.conv;


auto properDivisors(in ulong n) pure nothrow @safe /*@nogc*/ {
auto properDivisors(in ulong n) pure nothrow @safe /*@nogc*/ {
Line 2,364: Line 2,364:


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<syntaxhighlight lang=scheme>
<syntaxhighlight lang="scheme">
;; implementation of Floyd algorithm to find cycles in a graph
;; implementation of Floyd algorithm to find cycles in a graph
;; see Wikipedia https://en.wikipedia.org/wiki/Cycle_detection
;; see Wikipedia https://en.wikipedia.org/wiki/Cycle_detection
Line 2,421: Line 2,421:
</syntaxhighlight>
</syntaxhighlight>
{{out}}
{{out}}
<syntaxhighlight lang=scheme>
<syntaxhighlight lang="scheme">
(lib 'math)
(lib 'math)
(lib 'bigint)
(lib 'bigint)
Line 2,465: Line 2,465:
=={{header|Elixir}}==
=={{header|Elixir}}==
{{trans|Ruby}}
{{trans|Ruby}}
<syntaxhighlight lang=elixir>defmodule Proper do
<syntaxhighlight lang="elixir">defmodule Proper do
def divisors(1), do: []
def divisors(1), do: []
def divisors(n), do: [1 | divisors(2,n,:math.sqrt(n))] |> Enum.sort
def divisors(n), do: [1 | divisors(2,n,:math.sqrt(n))] |> Enum.sort
Line 2,550: Line 2,550:
=={{header|Factor}}==
=={{header|Factor}}==
For convenience, the term that caused termination is always included in the output sequence.
For convenience, the term that caused termination is always included in the output sequence.
<syntaxhighlight lang=factor>USING: combinators combinators.short-circuit formatting kernel
<syntaxhighlight lang="factor">USING: combinators combinators.short-circuit formatting kernel
literals locals math math.functions math.primes.factors
literals locals math math.functions math.primes.factors
math.ranges namespaces pair-rocket sequences sets ;
math.ranges namespaces pair-rocket sequences sets ;
Line 2,667: Line 2,667:
A more flexible syntax (such as Algol's) would enable the double scan of the TRAIL array to be avoided, as in if TRAIL[I:=MinLoc(Abs(TRAIL(1:L) - SF))] = SF then... That is, find the first index of array TRAIL such that ABS(TRAIL(1:L) - SF) is minimal, save that index in I, then access that element of TRAIL and test if it is equal to SF. The INDEX function could be use to find the first match, except that it is defined only for character variables. Alternatively, use an explicit DO-loop to search for equality, thus not employing fancy syntax, and not having to wonder if the ANY function will stop on the first match rather than wastefully continue the testing for all array elements. The modern style in manual writing is to employ vaguely general talk about arrays and omit specific details.
A more flexible syntax (such as Algol's) would enable the double scan of the TRAIL array to be avoided, as in if TRAIL[I:=MinLoc(Abs(TRAIL(1:L) - SF))] = SF then... That is, find the first index of array TRAIL such that ABS(TRAIL(1:L) - SF) is minimal, save that index in I, then access that element of TRAIL and test if it is equal to SF. The INDEX function could be use to find the first match, except that it is defined only for character variables. Alternatively, use an explicit DO-loop to search for equality, thus not employing fancy syntax, and not having to wonder if the ANY function will stop on the first match rather than wastefully continue the testing for all array elements. The modern style in manual writing is to employ vaguely general talk about arrays and omit specific details.


<syntaxhighlight lang=Fortran>
<syntaxhighlight lang="fortran">
MODULE FACTORSTUFF !This protocol evades the need for multiple parameters, or COMMON, or one shapeless main line...
MODULE FACTORSTUFF !This protocol evades the need for multiple parameters, or COMMON, or one shapeless main line...
Concocted by R.N.McLean, MMXV.
Concocted by R.N.McLean, MMXV.
Line 2,795: Line 2,795:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|C}}
{{trans|C}}
<syntaxhighlight lang=freebasic>function raiseTo( bas as ulongint, power as ulongint ) as ulongint
<syntaxhighlight lang="freebasic">function raiseTo( bas as ulongint, power as ulongint ) as ulongint
dim as ulongint result = 1, i
dim as ulongint result = 1, i
for i = 1 to power
for i = 1 to power
Line 2,877: Line 2,877:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 3,020: Line 3,020:


=={{header|Haskell}}==
=={{header|Haskell}}==
<syntaxhighlight lang=Haskell>divisors :: (Integral a) => a -> [a]
<syntaxhighlight lang="haskell">divisors :: (Integral a) => a -> [a]
divisors n = filter ((0 ==) . (n `mod`)) [1 .. (n `div` 2)]
divisors n = filter ((0 ==) . (n `mod`)) [1 .. (n `div` 2)]


Line 3,088: Line 3,088:
=={{header|J}}==
=={{header|J}}==
Implementation:
Implementation:
<syntaxhighlight lang=J>proper_divisors=: [: */@>@}:@,@{ [: (^ i.@>:)&.>/ 2 p: x:
<syntaxhighlight lang="j">proper_divisors=: [: */@>@}:@,@{ [: (^ i.@>:)&.>/ 2 p: x:
aliquot=: +/@proper_divisors ::0:
aliquot=: +/@proper_divisors ::0:
rc_aliquot_sequence=: aliquot^:(i.16)&>
rc_aliquot_sequence=: aliquot^:(i.16)&>
Line 3,105: Line 3,105:


Task example:
Task example:
<syntaxhighlight lang=J> rc_display_aliquot_sequence&> >: i.10
<syntaxhighlight lang="j"> rc_display_aliquot_sequence&> >: i.10
terminate 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
terminate 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
terminate 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
terminate 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Line 3,136: Line 3,136:
Translation of [[Aliquot_sequence_classifications#Python|Python]] via [[Aliquot_sequence_classifications#D|D]]
Translation of [[Aliquot_sequence_classifications#Python|Python]] via [[Aliquot_sequence_classifications#D|D]]
{{works with|Java|8}}
{{works with|Java|8}}
<syntaxhighlight lang=java>import java.util.ArrayList;
<syntaxhighlight lang="java">import java.util.ArrayList;
import java.util.Arrays;
import java.util.Arrays;
import java.util.List;
import java.util.List;
Line 3,229: Line 3,229:
=={{header|jq}}==
=={{header|jq}}==
{{works with|jq|1.4}}
{{works with|jq|1.4}}
<syntaxhighlight lang=jq># "until" is available in more recent versions of jq
<syntaxhighlight lang="jq"># "until" is available in more recent versions of jq
# than jq 1.4
# than jq 1.4
def until(cond; next):
def until(cond; next):
Line 3,289: Line 3,289:
task</syntaxhighlight>
task</syntaxhighlight>
{{out}}
{{out}}
<syntaxhighlight lang=sh>$ jq -n -r -f aliquot.jq
<syntaxhighlight lang="sh">$ jq -n -r -f aliquot.jq
1: terminating: [1,0]
1: terminating: [1,0]
2: terminating: [2,1,0]
2: terminating: [2,1,0]
Line 3,318: Line 3,318:
=={{header|Julia}}==
=={{header|Julia}}==
'''Core Function'''
'''Core Function'''
<syntaxhighlight lang=Julia>
<syntaxhighlight lang="julia">
function aliquotclassifier{T<:Integer}(n::T)
function aliquotclassifier{T<:Integer}(n::T)
a = T[n]
a = T[n]
Line 3,346: Line 3,346:


'''Supporting Functions'''
'''Supporting Functions'''
<syntaxhighlight lang=Julia>
<syntaxhighlight lang="julia">
function pcontrib{T<:Integer}(p::T, a::T)
function pcontrib{T<:Integer}(p::T, a::T)
n = one(T)
n = one(T)
Line 3,367: Line 3,367:


'''Main'''
'''Main'''
<syntaxhighlight lang=Julia>using Printf
<syntaxhighlight lang="julia">using Printf


println("Classification Tests:")
println("Classification Tests:")
Line 3,406: Line 3,406:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<syntaxhighlight lang=scala>// version 1.1.3
<syntaxhighlight lang="scala">// version 1.1.3


data class Classification(val sequence: List<Long>, val aliquot: String)
data class Classification(val sequence: List<Long>, val aliquot: String)
Line 3,517: Line 3,517:


There are no sociable sequences.
There are no sociable sequences.
<syntaxhighlight lang=lb>
<syntaxhighlight lang="lb">
print "ROSETTA CODE - Aliquot sequence classifications"
print "ROSETTA CODE - Aliquot sequence classifications"
[Start]
[Start]
Line 3,631: Line 3,631:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica>seq[n_] :=
<syntaxhighlight lang="mathematica">seq[n_] :=
NestList[If[# == 0, 0,
NestList[If[# == 0, 0,
DivisorSum[#, # &, Function[div, div != #]]] &, n, 16];
DivisorSum[#, # &, Function[div, div != #]]] &, n, 16];
Line 3,677: Line 3,677:


=={{header|Nim}}==
=={{header|Nim}}==
<syntaxhighlight lang=Nim>
<syntaxhighlight lang="nim">
import math
import math
import strformat
import strformat
Line 3,813: Line 3,813:
=={{header|Oforth}}==
=={{header|Oforth}}==


<syntaxhighlight lang=oforth>import: mapping
<syntaxhighlight lang="oforth">import: mapping
import: quicksort
import: quicksort
import: math
import: math
Line 3,889: Line 3,889:
=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
Define function aliquot(). Works with recent versions of PARI/GP >= 2.8:
Define function aliquot(). Works with recent versions of PARI/GP >= 2.8:
<syntaxhighlight lang=parigp>aliquot(x) =
<syntaxhighlight lang="parigp">aliquot(x) =
{
{
my (L = List(x), M = Map(Mat([x,1])), k, m = "non-term.", n = x);
my (L = List(x), M = Map(Mat([x,1])), k, m = "non-term.", n = x);
Line 3,938: Line 3,938:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<syntaxhighlight lang=perl>use ntheory qw/divisor_sum/;
<syntaxhighlight lang="perl">use ntheory qw/divisor_sum/;


sub aliquot {
sub aliquot {
Line 4,003: Line 4,003:
=={{header|Phix}}==
=={{header|Phix}}==
Translated from the Python example
Translated from the Python example
<!--<syntaxhighlight lang=Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">function</span> <span style="color: #000000;">aliquot</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">aliquot</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">}</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">}</span>
Line 4,063: Line 4,063:
=={{header|Picat}}==
=={{header|Picat}}==
{{trans|C++}}
{{trans|C++}}
<syntaxhighlight lang=Picat>divisor_sum(N) = R =>
<syntaxhighlight lang="picat">divisor_sum(N) = R =>
Total = 1,
Total = 1,
Power = 2,
Power = 2,
Line 4,150: Line 4,150:
{{works with|PowerShell|4.0}}<br/>
{{works with|PowerShell|4.0}}<br/>
<b>Simple</b>
<b>Simple</b>
<syntaxhighlight lang=powershell>function Get-NextAliquot ( [int]$X )
<syntaxhighlight lang="powershell">function Get-NextAliquot ( [int]$X )
{
{
If ( $X -gt 1 )
If ( $X -gt 1 )
Line 4,185: Line 4,185:
( 11, 12, 28, 496, 220, 1184, 790, 909, 562, 1064, 1488 ).ForEach{ [string]$_ + " is " + ( Classify-AlliquotSequence -Sequence ( Get-AliquotSequence -K $_ -N 16 ) ) }</syntaxhighlight>
( 11, 12, 28, 496, 220, 1184, 790, 909, 562, 1064, 1488 ).ForEach{ [string]$_ + " is " + ( Classify-AlliquotSequence -Sequence ( Get-AliquotSequence -K $_ -N 16 ) ) }</syntaxhighlight>
<b>Optimized</b>
<b>Optimized</b>
<syntaxhighlight lang=powershell>function Get-NextAliquot ( [int]$X )
<syntaxhighlight lang="powershell">function Get-NextAliquot ( [int]$X )
{
{
If ( $X -gt 1 )
If ( $X -gt 1 )
Line 4,274: Line 4,274:


===Version 3.0===
===Version 3.0===
<syntaxhighlight lang=PowerShell>
<syntaxhighlight lang="powershell">
function Get-Aliquot
function Get-Aliquot
{
{
Line 4,366: Line 4,366:
}
}
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang=PowerShell>
<syntaxhighlight lang="powershell">
$oneToTen = 1..10 | Get-Aliquot
$oneToTen = 1..10 | Get-Aliquot
$selected = 11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488 | Get-Aliquot
$selected = 11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488 | Get-Aliquot
Line 4,405: Line 4,405:
{{trans|C++}}
{{trans|C++}}
{{works with|SWI Prolog}}
{{works with|SWI Prolog}}
<syntaxhighlight lang=prolog>% See https://en.wikipedia.org/wiki/Divisor_function
<syntaxhighlight lang="prolog">% See https://en.wikipedia.org/wiki/Divisor_function
divisor_sum(N, Total):-
divisor_sum(N, Total):-
divisor_sum_prime(N, 2, 2, Total1, 1, N1),
divisor_sum_prime(N, 2, 2, Total1, 1, N1),
Line 4,505: Line 4,505:
Importing [[Proper_divisors#Python:_From_prime_factors|Proper divisors from prime factors]]:
Importing [[Proper_divisors#Python:_From_prime_factors|Proper divisors from prime factors]]:


<syntaxhighlight lang=python>from proper_divisors import proper_divs
<syntaxhighlight lang="python">from proper_divisors import proper_divs
from functools import lru_cache
from functools import lru_cache


Line 4,577: Line 4,577:
{{works with|QBasic|1.1}}
{{works with|QBasic|1.1}}
{{trans|Liberty BASIC}}
{{trans|Liberty BASIC}}
<syntaxhighlight lang=QBasic>DECLARE FUNCTION PDtotal! (n!)
<syntaxhighlight lang="qbasic">DECLARE FUNCTION PDtotal! (n!)
DECLARE SUB PrintAliquotClassifier (K!)
DECLARE SUB PrintAliquotClassifier (K!)
CLS
CLS
Line 4,663: Line 4,663:
'''fold-divisors''' is used from [[Proper_divisors#Racket]], but for the truly big numbers, we use divisors from math/number-theory.
'''fold-divisors''' is used from [[Proper_divisors#Racket]], but for the truly big numbers, we use divisors from math/number-theory.


<syntaxhighlight lang=racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(require "proper-divisors.rkt" math/number-theory)
(require "proper-divisors.rkt" math/number-theory)


Line 4,747: Line 4,747:
(formerly Perl 6)
(formerly Perl 6)
{{works with|rakudo|2018.10}}
{{works with|rakudo|2018.10}}
<syntaxhighlight lang=perl6>sub propdivsum (\x) {
<syntaxhighlight lang="raku" line>sub propdivsum (\x) {
my @l = x > 1;
my @l = x > 1;
(2 .. x.sqrt.floor).map: -> \d {
(2 .. x.sqrt.floor).map: -> \d {
Line 4,818: Line 4,818:


Both of the above limitations are imposed by this Rosetta Code task's restriction requirements: &nbsp; ''For the purposes of this task, ···''.
Both of the above limitations are imposed by this Rosetta Code task's restriction requirements: &nbsp; ''For the purposes of this task, ···''.
<syntaxhighlight lang=rexx>/*REXX program classifies various positive integers for types of aliquot sequences. */
<syntaxhighlight lang="rexx">/*REXX program classifies various positive integers for types of aliquot sequences. */
parse arg low high $L /*obtain optional arguments from the CL*/
parse arg low high $L /*obtain optional arguments from the CL*/
high= word(high low 10,1); low= word(low 1,1) /*obtain the LOW and HIGH (range). */
high= word(high low 10,1); low= word(low 1,1) /*obtain the LOW and HIGH (range). */
Line 4,924: Line 4,924:


=={{header|Ring}}==
=={{header|Ring}}==
<syntaxhighlight lang=ring>
<syntaxhighlight lang="ring">
# Project : Aliquot sequence classnifications
# Project : Aliquot sequence classnifications


Line 5,074: Line 5,074:
{{trans|Python}}
{{trans|Python}}


<syntaxhighlight lang=ruby>def aliquot(n, maxlen=16, maxterm=2**47)
<syntaxhighlight lang="ruby">def aliquot(n, maxlen=16, maxterm=2**47)
return "terminating", [0] if n == 0
return "terminating", [0] if n == 0
s = []
s = []
Line 5,134: Line 5,134:


=={{header|Rust}}==
=={{header|Rust}}==
<syntaxhighlight lang=rust>#[derive(Debug)]
<syntaxhighlight lang="rust">#[derive(Debug)]
enum AliquotType { Terminating, Perfect, Amicable, Sociable, Aspiring, Cyclic, NonTerminating }
enum AliquotType { Terminating, Perfect, Amicable, Sociable, Aspiring, Cyclic, NonTerminating }


Line 5,208: Line 5,208:
=={{header|Scala}}==
=={{header|Scala}}==
Put [[proper_divisors#Scala]] the full /Proper divisors for big (long) numbers/ section to the beginning:
Put [[proper_divisors#Scala]] the full /Proper divisors for big (long) numbers/ section to the beginning:
<syntaxhighlight lang=Scala>def createAliquotSeq(n: Long, step: Int, list: List[Long]): (String, List[Long]) = {
<syntaxhighlight lang="scala">def createAliquotSeq(n: Long, step: Int, list: List[Long]): (String, List[Long]) = {
val sum = properDivisors(n).sum
val sum = properDivisors(n).sum
if (sum == 0) ("terminate", list ::: List(sum))
if (sum == 0) ("terminate", list ::: List(sum))
Line 5,257: Line 5,257:
=={{header|Swift}}==
=={{header|Swift}}==


<syntaxhighlight lang=swift>extension BinaryInteger {
<syntaxhighlight lang="swift">extension BinaryInteger {
@inlinable
@inlinable
public func factors(sorted: Bool = true) -> [Self] {
public func factors(sorted: Bool = true) -> [Self] {
Line 5,358: Line 5,358:
This solution creates an iterator from a coroutine to generate aliquot sequences. al_classify uses a "RESULT" exception to achieve some unusual control flow.
This solution creates an iterator from a coroutine to generate aliquot sequences. al_classify uses a "RESULT" exception to achieve some unusual control flow.


<syntaxhighlight lang=Tcl>proc ProperDivisors {n} {
<syntaxhighlight lang="tcl">proc ProperDivisors {n} {
if {$n == 1} {return 0}
if {$n == 1} {return 0}
set divs 1
set divs 1
Line 5,464: Line 5,464:


=={{header|VBA}}==
=={{header|VBA}}==
<syntaxhighlight lang=vb>Option Explicit
<syntaxhighlight lang="vb">Option Explicit


Private Type Aliquot
Private Type Aliquot
Line 5,560: Line 5,560:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|Go}}
{{trans|Go}}
<syntaxhighlight lang=vlang>import math
<syntaxhighlight lang="vlang">import math
const threshold = u64(1) << 47
const threshold = u64(1) << 47
Line 5,703: Line 5,703:
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
{{libheader|Wren-seq}}
<syntaxhighlight lang=ecmascript>import "/fmt" for Conv, Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Conv, Fmt
import "/math" for Int, Nums
import "/math" for Int, Nums
import "/seq" for Lst
import "/seq" for Lst
Line 5,791: Line 5,791:
=={{header|Yabasic}}==
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<syntaxhighlight lang=Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Aliquot_sequence_classifications
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Aliquot_sequence_classifications
// by Galileo, 05/2022
// by Galileo, 05/2022


Line 5,879: Line 5,879:


=={{header|zkl}}==
=={{header|zkl}}==
<syntaxhighlight lang=zkl>fcn properDivs(n){ [1.. (n + 1)/2 + 1].filter('wrap(x){ n%x==0 and n!=x }) }
<syntaxhighlight lang="zkl">fcn properDivs(n){ [1.. (n + 1)/2 + 1].filter('wrap(x){ n%x==0 and n!=x }) }
fcn aliquot(k){ //-->Walker
fcn aliquot(k){ //-->Walker
Walker(fcn(rk){ k:=rk.value; if(k)rk.set(properDivs(k).sum()); k }.fp(Ref(k)))
Walker(fcn(rk){ k:=rk.value; if(k)rk.set(properDivs(k).sum()); k }.fp(Ref(k)))
}(10).walk(15).println();</syntaxhighlight>
}(10).walk(15).println();</syntaxhighlight>
Or, refactoring to remove saving the intermediate divisors (and adding white space):
Or, refactoring to remove saving the intermediate divisors (and adding white space):
<syntaxhighlight lang=zkl>fcn aliquot(k){ //-->Walker
<syntaxhighlight lang="zkl">fcn aliquot(k){ //-->Walker
Walker(fcn(rk){
Walker(fcn(rk){
k:=rk.value;
k:=rk.value;
Line 5,893: Line 5,893:
}.fp(Ref(k)))
}.fp(Ref(k)))
}(10).walk(15).println();</syntaxhighlight>
}(10).walk(15).println();</syntaxhighlight>
<syntaxhighlight lang=zkl>fcn classify(k){
<syntaxhighlight lang="zkl">fcn classify(k){
const MAX=(2).pow(47); // 140737488355328
const MAX=(2).pow(47); // 140737488355328
ak,aks:=aliquot(k), ak.walk(16);
ak,aks:=aliquot(k), ak.walk(16);
Line 5,915: Line 5,915:
+ " " + aks.filter();
+ " " + aks.filter();
}</syntaxhighlight>
}</syntaxhighlight>
<syntaxhighlight lang=zkl>[1..10].pump(fcn(k){ "%6d is %s".fmt(k,classify(k)).println() });
<syntaxhighlight lang="zkl">[1..10].pump(fcn(k){ "%6d is %s".fmt(k,classify(k)).println() });
T(11,12,28,496,220,1184,12496,1264460,790,909,562,1064,1488)
T(11,12,28,496,220,1184,12496,1264460,790,909,562,1064,1488)
.pump(fcn(k){ "%6d is %s".fmt(k,classify(k)).println() });</syntaxhighlight>
.pump(fcn(k){ "%6d is %s".fmt(k,classify(k)).println() });</syntaxhighlight>
Line 5,950: Line 5,950:
{{trans|AWK}}
{{trans|AWK}}
This program is correct. However, a bug in the ROM of the ZX Spectrum makes the number 909 of an erroneous result. However, the same program running on Sam BASIC (a superset of Sinclair BASIC that ran on the computer Sam Coupé) provides the correct results.
This program is correct. However, a bug in the ROM of the ZX Spectrum makes the number 909 of an erroneous result. However, the same program running on Sam BASIC (a superset of Sinclair BASIC that ran on the computer Sam Coupé) provides the correct results.
<syntaxhighlight lang=zxbasic>10 PRINT "Number classification sequence"
<syntaxhighlight lang="zxbasic">10 PRINT "Number classification sequence"
20 INPUT "Enter a number (0 to end): ";k: IF k>0 THEN GO SUB 2000: PRINT k;" ";s$: GO TO 20
20 INPUT "Enter a number (0 to end): ";k: IF k>0 THEN GO SUB 2000: PRINT k;" ";s$: GO TO 20
40 STOP
40 STOP