Jump to content

Aliquot sequence classifications: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 35:
{{trans|Python}}
 
<syntaxhighlight lang="11l">F pdsum(n)
R sum((1 .. (n + 1) I/ 2).filter(x -> @n % x == 0 & @n != x))
 
Line 103:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang=AArch64"aarch64 Assemblyassembly">
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program aliquotSeq64.s */
Line 691:
=={{header|ALGOL 68}}==
Assumes LONG INT is at least 64 bits, as in Algol 68G.
<syntaxhighlight lang="algol68">BEGIN
# aliquot sequence classification #
# maximum sequence length we consider #
Line 827:
=={{header|AppleScript}}==
 
<syntaxhighlight lang="applescript">on aliquotSum(n)
if (n < 2) then return 0
set sum to 1
Line 898:
 
{{output}}
<syntaxhighlight lang="applescript">"
1: terminating 1, 0
2: terminating 2, 1, 0
Line 925:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang=ARM"arm Assemblyassembly">
/* ARM assembly Raspberry PI */
/* program aliquotSeq.s */
Line 1,500:
=={{header|AWK}}==
 
<syntaxhighlight lang="awk">
#!/bin/gawk -f
function sumprop(num, i,sum,root) {
Line 1,601:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang=BASIC256"basic256"># Rosetta Code problem: http://rosettacode.org/wiki/Aliquot_sequence_classifications
# by Jjuanhdez, 06/2022
 
Line 1,658:
===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.
<syntaxhighlight lang=C"c">
#include<stdlib.h>
#include<string.h>
Line 1,791:
===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.
<syntaxhighlight lang=C"c">
#include<string.h>
#include<stdlib.h>
Line 1,960:
=={{header|C++}}==
This one follows the trail blazed by the "Number Theoretic" C example above.
<syntaxhighlight lang="cpp">#include <cstdint>
#include <iostream>
#include <string>
Line 2,062:
=={{header|CLU}}==
{{trans|C++}}
<syntaxhighlight lang="clu">% This program uses the 'bigint' cluster from PCLU's 'misc.lib'
 
% Remove leading and trailing whitespace (bigint$unparse adds a lot)
Line 2,187:
=={{header|Common Lisp}}==
Uses the Lisp function '''proper-divisors-recursive''' from [[Proper_divisors#Common_Lisp|Task:Proper Divisors]].
<syntaxhighlight lang="lisp">(defparameter *nlimit* 16)
(defparameter *klimit* (expt 2 47))
(defparameter *asht* (make-hash-table))
Line 2,286:
=={{header|D}}==
{{trans|Python}}
<syntaxhighlight lang="d">import std.stdio, std.range, std.algorithm, std.typecons, std.conv;
 
auto properDivisors(in ulong n) pure nothrow @safe /*@nogc*/ {
Line 2,364:
 
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
;; implementation of Floyd algorithm to find cycles in a graph
;; see Wikipedia https://en.wikipedia.org/wiki/Cycle_detection
Line 2,421:
</syntaxhighlight>
{{out}}
<syntaxhighlight lang="scheme">
(lib 'math)
(lib 'bigint)
Line 2,465:
=={{header|Elixir}}==
{{trans|Ruby}}
<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,550:
=={{header|Factor}}==
For convenience, the term that caused termination is always included in the output sequence.
<syntaxhighlight lang="factor">USING: combinators combinators.short-circuit formatting kernel
literals locals math math.functions math.primes.factors
math.ranges namespaces pair-rocket sequences sets ;
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.
 
<syntaxhighlight lang=Fortran"fortran">
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,795:
=={{header|FreeBASIC}}==
{{trans|C}}
<syntaxhighlight lang="freebasic">function raiseTo( bas as ulongint, power as ulongint ) as ulongint
dim as ulongint result = 1, i
for i = 1 to power
Line 2,877:
=={{header|Go}}==
{{trans|Kotlin}}
<syntaxhighlight lang="go">package main
 
import (
Line 3,020:
 
=={{header|Haskell}}==
<syntaxhighlight lang=Haskell"haskell">divisors :: (Integral a) => a -> [a]
divisors n = filter ((0 ==) . (n `mod`)) [1 .. (n `div` 2)]
 
Line 3,088:
=={{header|J}}==
Implementation:
<syntaxhighlight lang=J"j">proper_divisors=: [: */@>@}:@,@{ [: (^ i.@>:)&.>/ 2 p: x:
aliquot=: +/@proper_divisors ::0:
rc_aliquot_sequence=: aliquot^:(i.16)&>
Line 3,105:
 
Task example:
<syntaxhighlight lang=J"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 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Line 3,136:
Translation of [[Aliquot_sequence_classifications#Python|Python]] via [[Aliquot_sequence_classifications#D|D]]
{{works with|Java|8}}
<syntaxhighlight lang="java">import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Line 3,229:
=={{header|jq}}==
{{works with|jq|1.4}}
<syntaxhighlight lang="jq"># "until" is available in more recent versions of jq
# than jq 1.4
def until(cond; next):
Line 3,289:
task</syntaxhighlight>
{{out}}
<syntaxhighlight lang="sh">$ jq -n -r -f aliquot.jq
1: terminating: [1,0]
2: terminating: [2,1,0]
Line 3,318:
=={{header|Julia}}==
'''Core Function'''
<syntaxhighlight lang=Julia"julia">
function aliquotclassifier{T<:Integer}(n::T)
a = T[n]
Line 3,346:
 
'''Supporting Functions'''
<syntaxhighlight lang=Julia"julia">
function pcontrib{T<:Integer}(p::T, a::T)
n = one(T)
Line 3,367:
 
'''Main'''
<syntaxhighlight lang=Julia"julia">using Printf
 
println("Classification Tests:")
Line 3,406:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.3
 
data class Classification(val sequence: List<Long>, val aliquot: String)
Line 3,517:
 
There are no sociable sequences.
<syntaxhighlight lang="lb">
print "ROSETTA CODE - Aliquot sequence classifications"
[Start]
Line 3,631:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">seq[n_] :=
NestList[If[# == 0, 0,
DivisorSum[#, # &, Function[div, div != #]]] &, n, 16];
Line 3,677:
 
=={{header|Nim}}==
<syntaxhighlight lang=Nim"nim">
import math
import strformat
Line 3,813:
=={{header|Oforth}}==
 
<syntaxhighlight lang="oforth">import: mapping
import: quicksort
import: math
Line 3,889:
=={{header|PARI/GP}}==
Define function aliquot(). Works with recent versions of PARI/GP >= 2.8:
<syntaxhighlight lang="parigp">aliquot(x) =
{
my (L = List(x), M = Map(Mat([x,1])), k, m = "non-term.", n = x);
Line 3,938:
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl">use ntheory qw/divisor_sum/;
 
sub aliquot {
Line 4,003:
=={{header|Phix}}==
Translated from the Python example
<!--<syntaxhighlight lang=Phix"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: #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:
=={{header|Picat}}==
{{trans|C++}}
<syntaxhighlight lang=Picat"picat">divisor_sum(N) = R =>
Total = 1,
Power = 2,
Line 4,150:
{{works with|PowerShell|4.0}}<br/>
<b>Simple</b>
<syntaxhighlight lang="powershell">function Get-NextAliquot ( [int]$X )
{
If ( $X -gt 1 )
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>
<b>Optimized</b>
<syntaxhighlight lang="powershell">function Get-NextAliquot ( [int]$X )
{
If ( $X -gt 1 )
Line 4,274:
 
===Version 3.0===
<syntaxhighlight lang=PowerShell"powershell">
function Get-Aliquot
{
Line 4,366:
}
</syntaxhighlight>
<syntaxhighlight lang=PowerShell"powershell">
$oneToTen = 1..10 | Get-Aliquot
$selected = 11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488 | Get-Aliquot
Line 4,405:
{{trans|C++}}
{{works with|SWI Prolog}}
<syntaxhighlight lang="prolog">% See https://en.wikipedia.org/wiki/Divisor_function
divisor_sum(N, Total):-
divisor_sum_prime(N, 2, 2, Total1, 1, N1),
Line 4,505:
Importing [[Proper_divisors#Python:_From_prime_factors|Proper divisors from prime factors]]:
 
<syntaxhighlight lang="python">from proper_divisors import proper_divs
from functools import lru_cache
 
Line 4,577:
{{works with|QBasic|1.1}}
{{trans|Liberty BASIC}}
<syntaxhighlight lang=QBasic"qbasic">DECLARE FUNCTION PDtotal! (n!)
DECLARE SUB PrintAliquotClassifier (K!)
CLS
Line 4,663:
'''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
(require "proper-divisors.rkt" math/number-theory)
 
Line 4,747:
(formerly Perl 6)
{{works with|rakudo|2018.10}}
<syntaxhighlight lang=perl6"raku" line>sub propdivsum (\x) {
my @l = x > 1;
(2 .. x.sqrt.floor).map: -> \d {
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, ···''.
<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*/
high= word(high low 10,1); low= word(low 1,1) /*obtain the LOW and HIGH (range). */
Line 4,924:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : Aliquot sequence classnifications
 
Line 5,074:
{{trans|Python}}
 
<syntaxhighlight lang="ruby">def aliquot(n, maxlen=16, maxterm=2**47)
return "terminating", [0] if n == 0
s = []
Line 5,134:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">#[derive(Debug)]
enum AliquotType { Terminating, Perfect, Amicable, Sociable, Aspiring, Cyclic, NonTerminating }
 
Line 5,208:
=={{header|Scala}}==
Put [[proper_divisors#Scala]] the full /Proper divisors for big (long) numbers/ section to the beginning:
<syntaxhighlight lang=Scala"scala">def createAliquotSeq(n: Long, step: Int, list: List[Long]): (String, List[Long]) = {
val sum = properDivisors(n).sum
if (sum == 0) ("terminate", list ::: List(sum))
Line 5,257:
=={{header|Swift}}==
 
<syntaxhighlight lang="swift">extension BinaryInteger {
@inlinable
public func factors(sorted: Bool = true) -> [Self] {
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.
 
<syntaxhighlight lang=Tcl"tcl">proc ProperDivisors {n} {
if {$n == 1} {return 0}
set divs 1
Line 5,464:
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">Option Explicit
 
Private Type Aliquot
Line 5,560:
=={{header|Vlang}}==
{{trans|Go}}
<syntaxhighlight lang="vlang">import math
const threshold = u64(1) << 47
Line 5,703:
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
<syntaxhighlight lang="ecmascript">import "/fmt" for Conv, Fmt
import "/math" for Int, Nums
import "/seq" for Lst
Line 5,791:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang=Yabasic"yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Aliquot_sequence_classifications
// by Galileo, 05/2022
 
Line 5,879:
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">fcn properDivs(n){ [1.. (n + 1)/2 + 1].filter('wrap(x){ n%x==0 and n!=x }) }
fcn aliquot(k){ //-->Walker
Walker(fcn(rk){ k:=rk.value; if(k)rk.set(properDivs(k).sum()); k }.fp(Ref(k)))
}(10).walk(15).println();</syntaxhighlight>
Or, refactoring to remove saving the intermediate divisors (and adding white space):
<syntaxhighlight lang="zkl">fcn aliquot(k){ //-->Walker
Walker(fcn(rk){
k:=rk.value;
Line 5,893:
}.fp(Ref(k)))
}(10).walk(15).println();</syntaxhighlight>
<syntaxhighlight lang="zkl">fcn classify(k){
const MAX=(2).pow(47); // 140737488355328
ak,aks:=aliquot(k), ak.walk(16);
Line 5,915:
+ " " + aks.filter();
}</syntaxhighlight>
<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)
.pump(fcn(k){ "%6d is %s".fmt(k,classify(k)).println() });</syntaxhighlight>
Line 5,950:
{{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.
<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
40 STOP
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.