Aliquot sequence classifications: Difference between revisions
m
syntax highlighting fixup automation
(Aliquot sequence classifications in BASIC256) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 35:
{{trans|Python}}
<
R sum((1 .. (n + 1) I/ 2).filter(x -> @n % x == 0 & @n != x))
Line 72:
L(n) [11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488]
V (cls, seq) = aliquot(n)
print(‘#.: #.’.format(cls, seq))</
{{out}}
Line 103:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program aliquotSeq64.s */
Line 660:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
<pre>
Program 64 bits start
Line 691:
=={{header|ALGOL 68}}==
Assumes LONG INT is at least 64 bits, as in Algol 68G.
<
# aliquot sequence classification #
# maximum sequence length we consider #
Line 796:
print( ( newline ) )
OD
END</
{{out}}
<pre>
Line 827:
=={{header|AppleScript}}==
<
if (n < 2) then return 0
set sum to 1
Line 895:
set output to output as text
set AppleScript's text item delimiters to astid
return output</
{{output}}
<
1: terminating 1, 0
2: terminating 2, 1, 0
Line 922:
1064: cyclic 1064, 1336, 1184, 1210
1488: non-terminating 1488, 2480, 3472, 4464, 8432, 9424, 10416, 21328, 22320, 55056, 95728, 96720, 236592, 459792, 881392, 882384
1.535571778608E+13: non-terminating 1.535571778608E+13, 4.453466360112E+13, 1.449400874645E+14"</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<
/* ARM assembly Raspberry PI */
/* program aliquotSeq.s */
Line 1,470:
.include "../affichage.inc"
</syntaxhighlight>
<pre>
Program start
Line 1,500:
=={{header|AWK}}==
<
#!/bin/gawk -f
function sumprop(num, i,sum,root) {
Line 1,568:
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,601:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<
# by Jjuanhdez, 06/2022
Line 1,652:
next element
if clase = 0 then print " non-terminating"
end subroutine</
=={{header|C}}==
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>
#include<stdlib.h>
#include<string.h>
Line 1,730:
return 0;
}
</syntaxhighlight>
Input file, you can include 15355717786080 or similar numbers in this list but be prepared to wait for a very, very long time.:
<pre>
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>
#include<string.h>
#include<stdlib.h>
Line 1,893:
return 0;
}
</syntaxhighlight>
Input file, to emphasize the effectiveness of this approach, the last number in the file is 153557177860800, 10 times the special case mentioned in the task.
<pre>
Line 1,960:
=={{header|C++}}==
This one follows the trail blazed by the "Number Theoretic" C example above.
<
#include <iostream>
#include <string>
Line 2,029:
classify_aliquot_sequence(153557177860800);
return 0;
}</
{{out}}
Line 2,062:
=={{header|CLU}}==
{{trans|C++}}
<
% Remove leading and trailing whitespace (bigint$unparse adds a lot)
Line 2,157:
classify_aliquot_sequence(bigint$parse("15355717786080"))
classify_aliquot_sequence(bigint$parse("153557177860800"))
end start_up</
{{out}}
<pre>1: terminating, sequence: 1 0
Line 2,187:
=={{header|Common Lisp}}==
Uses the Lisp function '''proper-divisors-recursive''' from [[Proper_divisors#Common_Lisp|Task:Proper Divisors]].
<
(defparameter *klimit* (expt 2 47))
(defparameter *asht* (make-hash-table))
Line 2,253:
(aliquot (+ k 1)))
(dolist (k '(11 12 28 496 220 1184 12496 1264460 790 909 562 1064 1488 15355717786080))
(aliquot k)))</
{{out}}
<pre>CL-USER(45): (main)
Line 2,286:
=={{header|D}}==
{{trans|Python}}
<
auto properDivisors(in ulong n) pure nothrow @safe /*@nogc*/ {
Line 2,336:
790, 909, 562, 1064, 1488])
writefln("%s: %s", n.aliquot[]);
}</
{{out}}
<pre>Terminating: [1, 0]
Line 2,364:
=={{header|EchoLisp}}==
<
;; implementation of Floyd algorithm to find cycles in a graph
;; see Wikipedia https://en.wikipedia.org/wiki/Cycle_detection
Line 2,419:
(when (= x0 starter) (set! end starter)))
(writeln ...))
</syntaxhighlight>
{{out}}
<
(lib 'math)
(lib 'bigint)
Line 2,461:
1000 terminating
1000 1340 1516 1144 1376 1396 1054 674 340 416 466 236 184 176 196 203 37 1 0 0 ...
</syntaxhighlight>
=={{header|Elixir}}==
{{trans|Ruby}}
<
def divisors(1), do: []
def divisors(n), do: [1 | divisors(2,n,:math.sqrt(n))] |> Enum.sort
Line 2,514:
if n<10000000, do: :io.fwrite("~7w:~21s: ~p~n", [n, msg, s]),
else: :io.fwrite("~w: ~s: ~p~n", [n, msg, s])
end)</
{{out}}
Line 2,550:
=={{header|Factor}}==
For convenience, the term that caused termination is always included in the output sequence.
<
literals locals math math.functions math.primes.factors
math.ranges namespaces pair-rocket sequences sets ;
Line 2,603:
10 [1,b] test-cases append [ .classify ] each ;
MAIN: main</
{{out}}
<pre>
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.
<
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,791:
END DO
END !Done.
</syntaxhighlight>
=={{header|FreeBASIC}}==
{{trans|C}}
<
dim as ulongint result = 1, i
for i = 1 to power
Line 2,873:
for n as ubyte = 0 to 22
aliquotClassifier(nums(n))
next n</
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 2,985:
seq, aliquot := classifySequence(k)
fmt.Printf("%d: %-15s %s\n", k, aliquot, joinWithCommas(seq))
}</
{{out}}
Line 3,020:
=={{header|Haskell}}==
<
divisors n = filter ((0 ==) . (n `mod`)) [1 .. (n `div` 2)]
Line 3,060:
let cls n = let ali = take 16 $ aliquot n in (classify ali, ali)
mapM_ (print . cls) $ [1..10] ++
[11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488]</
{{out}}
<pre>(Terminating,[1,0])
Line 3,088:
=={{header|J}}==
Implementation:
<
aliquot=: +/@proper_divisors ::0:
rc_aliquot_sequence=: aliquot^:(i.16)&>
Line 3,102:
end.
)
rc_display_aliquot_sequence=: (rc_classify,' ',":)@:rc_aliquot_sequence</
Task example:
<
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,131:
cyclic 1064 1336 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210 1184 1210
non-terminating 1488 2480 3472 4464 8432 9424 10416 21328 22320 55056 95728 96720 236592 459792 881392 882384
non-terminating 15355717786080 44534663601120 144940087464480 471714103310688 1130798979186912 2688948041357088 6050151708497568 13613157922639968 35513546724070632 74727605255142168 162658586225561832 353930992506879768 642678347124409032 1125102611548462968 1977286128289819992 3415126495450394808</
=={{header|Java}}==
Translation of [[Aliquot_sequence_classifications#Python|Python]] via [[Aliquot_sequence_classifications#D|D]]
{{works with|Java|8}}
<
import java.util.Arrays;
import java.util.List;
Line 3,199:
Arrays.stream(arr).forEach(n -> aliquot(n, 16, 1L << 47));
}
}</
<pre>Terminating: [1, 0]
Line 3,229:
=={{header|jq}}==
{{works with|jq|1.4}}
<
# than jq 1.4
def until(cond; next):
Line 3,287:
790, 909, 562, 1064, 1488, 15355717786080) | pp);
task</
{{out}}
<
1: terminating: [1,0]
2: terminating: [2,1,0]
Line 3,314:
1064: cyclic back to 1184: [1064,1336,1184,1210]
1488: non-terminating: [1488,2480,3472,4464,8432,9424,10416,21328,22320,55056,95728,96720,236592,459792,881392,882384]
15355717786080: non-terminating: [15355717786080,44534663601120]</
=={{header|Julia}}==
'''Core Function'''
<
function aliquotclassifier{T<:Integer}(n::T)
a = T[n]
Line 3,343:
return ("Non-terminating", a)
end
</syntaxhighlight>
'''Supporting Functions'''
<
function pcontrib{T<:Integer}(p::T, a::T)
n = one(T)
Line 3,364:
dsum -= n
end
</syntaxhighlight>
'''Main'''
<
println("Classification Tests:")
Line 3,375:
println(@sprintf("%8d => ", i), @sprintf("%16s, ", class), a)
end
</syntaxhighlight>
{{out}}
Line 3,406:
=={{header|Kotlin}}==
<
data class Classification(val sequence: List<Long>, val aliquot: String)
Line 3,467:
val (seq, aliquot) = classifySequence(k)
println("$k: ${aliquot.padEnd(15)} $seq")
}</
{{out}}
Line 3,517:
There are no sociable sequences.
<syntaxhighlight lang=lb>
print "ROSETTA CODE - Aliquot sequence classifications"
[Start]
Line 3,562:
next
end function
</syntaxhighlight>
{{out}}
Line 3,631:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
NestList[If[# == 0, 0,
DivisorSum[#, # &, Function[div, div != #]]] &, n, 16];
Line 3,649:
Print[{#, class[seq[#]], notate[seq[#]] /. {0} -> 0}] & /@ {1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909,
562, 1064, 1488, 15355717786080};</
{{out}}
<pre>{1, Terminating, {1, 0}}
Line 3,677:
=={{header|Nim}}==
<
import math
import strformat
Line 3,778:
echo ""
echo fmt"Processed in {(getTime() - t0).inMilliseconds} ms."
</syntaxhighlight>
{{out}}
Line 3,813:
=={{header|Oforth}}==
<
import: quicksort
import: math
Line 3,849:
]
"non-terminating"
;</
{{out}}
Line 3,889:
=={{header|PARI/GP}}==
Define function aliquot(). Works with recent versions of PARI/GP >= 2.8:
<
{
my (L = List(x), M = Map(Mat([x,1])), k, m = "non-term.", n = x);
Line 3,906:
);
printf("%16d: %10s, %s\n", x, m, Vec(L));
}</
Output:
Line 3,938:
=={{header|Perl}}==
{{libheader|ntheory}}
<
sub aliquot {
Line 3,973:
my($class, @seq) = aliquot($n);
printf "%14d %10s [@seq]\n", $n, $class;
}</
{{out}}
<pre> 1 terminates [1 0]
Line 4,003:
=={{header|Phix}}==
Translated from the Python example
<!--<
<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,032:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%14d => %15s, {%s}\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">classification</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dseq</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 4,063:
=={{header|Picat}}==
{{trans|C++}}
<
Total = 1,
Power = 2,
Line 4,119:
nl
end.
</syntaxhighlight>
{{out}}
<pre>
Line 4,150:
{{works with|PowerShell|4.0}}<br/>
<b>Simple</b>
<
{
If ( $X -gt 1 )
Line 4,183:
(1..10).ForEach{ [string]$_ + " is " + ( Classify-AlliquotSequence -Sequence ( Get-AliquotSequence -K $_ -N 16 ) ) }
( 11, 12, 28, 496, 220, 1184, 790, 909, 562, 1064, 1488 ).ForEach{ [string]$_ + " is " + ( Classify-AlliquotSequence -Sequence ( Get-AliquotSequence -K $_ -N 16 ) ) }</
<b>Optimized</b>
<
{
If ( $X -gt 1 )
Line 4,247:
(1..10).ForEach{ [string]$_ + " is " + ( Classify-AlliquotSequence -Sequence ( Get-AliquotSequence -K $_ -N 16 ) ) }
( 11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488 ).ForEach{ [string]$_ + " is " + ( Classify-AlliquotSequence -Sequence ( Get-AliquotSequence -K $_ -N 16 ) ) }</
{{out}}
<pre>1 is terminating
Line 4,274:
===Version 3.0===
<
function Get-Aliquot
{
Line 4,365:
}
}
</syntaxhighlight>
<
$oneToTen = 1..10 | Get-Aliquot
$selected = 11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488 | Get-Aliquot
Line 4,372:
$numbers = $oneToTen, $selected
$numbers
</syntaxhighlight>
{{Out}}
<pre>
Line 4,405:
{{trans|C++}}
{{works with|SWI Prolog}}
<
divisor_sum(N, Total):-
divisor_sum_prime(N, 2, 2, Total1, 1, N1),
Line 4,473:
write_aliquot_sequence(N, Sequence, Class),
fail.
main.</
{{out}}
Line 4,505:
Importing [[Proper_divisors#Python:_From_prime_factors|Proper divisors from prime factors]]:
<
from functools import lru_cache
Line 4,545:
print()
for n in [11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488, 15355717786080]:
print('%s: %r' % aliquot(n))</
{{out}}
Line 4,577:
{{works with|QBasic|1.1}}
{{trans|Liberty BASIC}}
<
DECLARE SUB PrintAliquotClassifier (K!)
CLS
Line 4,632:
IF clase = 0 THEN COLOR 12: PRINT " non-terminating"
COLOR 7
END SUB</
{{out}}
<pre>Number 1 : 0 Terminating
Line 4,663:
'''fold-divisors''' is used from [[Proper_divisors#Racket]], but for the truly big numbers, we use divisors from math/number-theory.
<
(require "proper-divisors.rkt" math/number-theory)
Line 4,715:
(for ((i (in-list '(11 12 28 496 220 1184 12496 1264460 790 909 562 1064 1488 15355717786080))))
(report-aliquot-sequence-class i))</
{{out}}
Line 4,747:
(formerly Perl 6)
{{works with|rakudo|2018.10}}
<
my @l = x > 1;
(2 .. x.sqrt.floor).map: -> \d {
Line 4,780:
11, 12, 28, 496, 220, 1184, 12496, 1264460,
790, 909, 562, 1064, 1488,
15355717786080;</
{{out}}
<pre>1 terminating [1]
Line 4,818:
Both of the above limitations are imposed by this Rosetta Code task's restriction requirements: ''For the purposes of this task, ···''.
<
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,880:
if j*j==x then s= s + j /*Was X a square? If so, add √ X */
#.x= s /*memoize division sum for argument X.*/
return s /*return " " " " " */</
{{out|output|text= when using the default input:}}
(Shown at three-quarter size.)
Line 4,924:
=={{header|Ring}}==
<
# Project : Aliquot sequence classnifications
Line 5,003:
next
return pdtotal
</syntaxhighlight>
Output:
<pre>
Line 5,074:
{{trans|Python}}
<
return "terminating", [0] if n == 0
s = []
Line 5,102:
for n in [11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488, 15355717786080]
puts "%20s: %p" % aliquot(n)
end</
{{out}}
Line 5,134:
=={{header|Rust}}==
<
enum AliquotType { Terminating, Perfect, Amicable, Sociable, Aspiring, Cyclic, NonTerminating }
Line 5,178:
println!("{} {:?}", num, classify_aliquot(*num));
}
}</
{{out}}
<pre>
Line 5,208:
=={{header|Scala}}==
Put [[proper_divisors#Scala]] the full /Proper divisors for big (long) numbers/ section to the beginning:
<
val sum = properDivisors(n).sum
if (sum == 0) ("terminate", list ::: List(sum))
Line 5,227:
val result = numbers.map(i => createAliquotSeq(i, 0, List(i)))
result foreach { v => println(f"${v._2.head}%14d ${v._1}%10s [${v._2 mkString " "}]" ) }</
{{out}}
Line 5,257:
=={{header|Swift}}==
<
@inlinable
public func factors(sorted: Bool = true) -> [Self] {
Line 5,323:
print()
print("\(15355717786080): \(classifySequence(k: 15355717786080))")</
{{out}}
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.
<
if {$n == 1} {return 0}
set divs 1
Line 5,430:
puts [time {
puts [format "%8d -> %-16s : %s" $i {*}[al_classify $i]]
}]</
{{out}}
Line 5,464:
=={{header|VBA}}==
<
Private Type Aliquot
Line 5,530:
SumPDiv = t
End Function
</syntaxhighlight>
{{out}}
Line 5,560:
=={{header|Vlang}}==
{{trans|Go}}
<
const threshold = u64(1) << 47
Line 5,664:
seq, aliquot := classify_sequence(k)
println("$k: ${aliquot:-15} $seq")
}</
{{out}}
Line 5,703:
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
<
import "/math" for Int, Nums
import "/seq" for Lst
Line 5,755:
var c = classifySequence.call(k)
var seq = c.seq.map { |i| Conv.dec(i) }.toList // ensure 15 digit integer is printed in full
System.print("%(k): %(Fmt.s(-15, c.aliquot)) %(seq)")</
{{out}}
Line 5,791:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<
// by Galileo, 05/2022
Line 5,851:
if not n break
alliquot(n)
loop</
{{out}}
<pre>Integer: 1, Type: Terminating, Series: 1
Line 5,879:
=={{header|zkl}}==
<
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();</
Or, refactoring to remove saving the intermediate divisors (and adding white space):
<
Walker(fcn(rk){
k:=rk.value;
Line 5,892:
k
}.fp(Ref(k)))
}(10).walk(15).println();</
<
const MAX=(2).pow(47); // 140737488355328
ak,aks:=aliquot(k), ak.walk(16);
Line 5,914:
else "non-terminating" )
+ " " + aks.filter();
}</
<
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() });</
{{out}}
<pre>
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.
<
20 INPUT "Enter a number (0 to end): ";k: IF k>0 THEN GO SUB 2000: PRINT k;" ";s$: GO TO 20
40 STOP
Line 5,990:
2320 NEXT t
2330 LET s$="non-terminating (after 16 terms)"+s$
2340 RETURN</
{{out}}
<pre>Number classification sequence
|