September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -6,13 +6,13 @@ being K and subsequent members being the sum of the [[Proper divisors]] of the p
|
|||
:<br>There are several classifications for non termination:
|
||||
:* If the second term is K then all future terms are also K and so the sequence repeats from the first term with period 1 and K is called '''perfect'''.
|
||||
:* If the third term ''would'' be repeating K then the sequence repeats with period 2 and K is called '''amicable'''.
|
||||
:* If the N'th term ''would'' be repeating K for the first time, with N > 3 then the sequence repeats with period N - 1 and K is called '''sociable'''.
|
||||
:* If the N<sup>th</sup> term ''would'' be repeating K for the first time, with N > 3 then the sequence repeats with period N - 1 and K is called '''sociable'''.
|
||||
:<br>Perfect, amicable and sociable numbers eventually repeat the original number K; there are other repetitions...
|
||||
:* Some K have a sequence that eventually forms a periodic repetition of period 1 but of a number other than K, for example 95 which forms the sequence <code>95, 25, 6, 6, 6, ...</code> such K are called '''aspiring'''.
|
||||
:* K that have a sequence that eventually forms a periodic repetition of period >= 2 but of a number other than K, for example 562 which forms the sequence <code>562, 284, 220, 284, 220, ...</code> such K are called '''cyclic'''.
|
||||
|
||||
:<br>And finally:
|
||||
:* Some K form aliquot sequences that are not known to be either terminating or periodic. these K are to be called '''non-terminating'''. <br>For the purposes of this task, K is to be classed as non-terminating if it has not been otherwise classed after generating '''16''' terms or if any term of the sequence is greater than 2**47 = 140737488355328.
|
||||
:* Some K form aliquot sequences that are not known to be either terminating or periodic. these K are to be called '''non-terminating'''. <br>For the purposes of this task, K is to be classed as non-terminating if it has not been otherwise classed after generating '''16''' terms or if any term of the sequence is greater than 2**47 = 140,737,488,355,328.
|
||||
|
||||
|
||||
;Task:
|
||||
|
|
@ -23,7 +23,9 @@ being K and subsequent members being the sum of the [[Proper divisors]] of the p
|
|||
|
||||
Show all output on this page.
|
||||
|
||||
|
||||
;Cf.
|
||||
* [[Abundant, deficient and perfect number classifications]]. (Classifications from only the first two members of the whole sequence).
|
||||
* [[Proper divisors]]
|
||||
* [[Amicable pairs]]
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.LongStream;
|
||||
import static java.util.stream.LongStream.rangeClosed;
|
||||
|
||||
public class AliquotSequenceClassifications {
|
||||
|
||||
public static Long properDivsSum(long n) {
|
||||
return rangeClosed(1, (n + 1) / 2).filter(i -> n % i == 0 && n != i).sum();
|
||||
private static Long properDivsSum(long n) {
|
||||
return LongStream.rangeClosed(1, (n + 1) / 2).filter(i -> n % i == 0 && n != i).sum();
|
||||
}
|
||||
|
||||
static boolean aliquot(long n, int maxLen, long maxTerm) {
|
||||
|
|
@ -52,8 +53,9 @@ public class AliquotSequenceClassifications {
|
|||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
long[] arr = {11L, 12, 28, 496, 220, 1184, 12496, 1264460,
|
||||
790, 909, 562, 1064, 1488};
|
||||
long[] arr = {
|
||||
11, 12, 28, 496, 220, 1184, 12496, 1264460,
|
||||
790, 909, 562, 1064, 1488};
|
||||
|
||||
LongStream.rangeClosed(1, 10).forEach(n -> aliquot(n, 16, 1L << 47));
|
||||
System.out.println();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,62 @@
|
|||
// version 1.1.3
|
||||
|
||||
data class Classification(val sequence: List<Long>, val aliquot: String)
|
||||
|
||||
const val THRESHOLD = 1L shl 47
|
||||
|
||||
fun sumProperDivisors(n: Long): Long {
|
||||
if (n < 2L) return 0L
|
||||
val sqrt = Math.sqrt(n.toDouble()).toLong()
|
||||
var sum = 1L + (2L..sqrt)
|
||||
.filter { n % it == 0L }
|
||||
.map { it + n / it }
|
||||
.sum()
|
||||
if (sqrt * sqrt == n) sum -= sqrt
|
||||
return sum
|
||||
}
|
||||
|
||||
fun classifySequence(k: Long): Classification {
|
||||
require(k > 0)
|
||||
var last = k
|
||||
val seq = mutableListOf(k)
|
||||
while (true) {
|
||||
last = sumProperDivisors(last)
|
||||
seq.add(last)
|
||||
val n = seq.size
|
||||
val aliquot = when {
|
||||
last == 0L -> "Terminating"
|
||||
n == 2 && last == k -> "Perfect"
|
||||
n == 3 && last == k -> "Amicable"
|
||||
n >= 4 && last == k -> "Sociable[${n - 1}]"
|
||||
last == seq[n - 2] -> "Aspiring"
|
||||
last in seq.slice(1..n - 3) -> "Cyclic[${n - 1 - seq.indexOf(last)}]"
|
||||
n == 16 || last > THRESHOLD -> "Non-Terminating"
|
||||
else -> ""
|
||||
}
|
||||
if (aliquot != "") return Classification(seq, aliquot)
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println("Aliqot classifications - periods for Sociable/Cyclic in square brackets:\n")
|
||||
for (k in 1L..10) {
|
||||
val (seq, aliquot) = classifySequence(k)
|
||||
println("${"%2d".format(k)}: ${aliquot.padEnd(15)} $seq")
|
||||
}
|
||||
|
||||
val la = longArrayOf(
|
||||
11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488
|
||||
)
|
||||
println()
|
||||
|
||||
for (k in la) {
|
||||
val (seq, aliquot) = classifySequence(k)
|
||||
println("${"%7d".format(k)}: ${aliquot.padEnd(15)} $seq")
|
||||
}
|
||||
|
||||
println()
|
||||
|
||||
val k = 15355717786080L
|
||||
val (seq, aliquot) = classifySequence(k)
|
||||
println("$k: ${aliquot.padEnd(15)} $seq")
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
aliquot(x) =
|
||||
{
|
||||
my (L = List(x), M = Map(Mat([x,1])), k, m = "non-term.", n = x);
|
||||
|
||||
for (i = 2, 16, n = vecsum(divisors(n)) - n;
|
||||
if (n > 2^47, break,
|
||||
n == 0, m = "terminates"; break,
|
||||
mapisdefined(M, n, &k),
|
||||
m = if (k == 1,
|
||||
if (i == 2, "perfect",
|
||||
i == 3, "amicable",
|
||||
i > 3, concat("sociable-",i-1)),
|
||||
k < i-1, concat("cyclic-",i-k),
|
||||
"aspiring"); break,
|
||||
mapput(M, n, i); listput(L, n));
|
||||
);
|
||||
printf("%16d: %10s, %s\n", x, m, Vec(L));
|
||||
}
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
function Get-Aliquot
|
||||
{
|
||||
[CmdletBinding()]
|
||||
[OutputType([PScustomObject])]
|
||||
Param
|
||||
(
|
||||
[Parameter(Mandatory=$true,
|
||||
ValueFromPipeline=$true,
|
||||
ValueFromPipelineByPropertyName=$true)]
|
||||
[int]
|
||||
$InputObject
|
||||
)
|
||||
|
||||
Begin
|
||||
{
|
||||
function Get-NextAliquot ([int]$X)
|
||||
{
|
||||
if ($X -gt 1)
|
||||
{
|
||||
$nextAliquot = 1
|
||||
|
||||
if ($X -gt 2)
|
||||
{
|
||||
$xSquareRoot = [Math]::Sqrt($X)
|
||||
|
||||
2..$xSquareRoot | Where-Object {$X % $_ -eq 0} | ForEach-Object {$nextAliquot += $_ + $x / $_}
|
||||
|
||||
if ($xSquareRoot % 1 -eq 0) {$nextAliquot -= $xSquareRoot}
|
||||
}
|
||||
|
||||
$nextAliquot
|
||||
}
|
||||
}
|
||||
|
||||
function Get-AliquotSequence ([int]$K, [int]$N)
|
||||
{
|
||||
$X = $K
|
||||
$X
|
||||
$i = 1
|
||||
|
||||
while ($X -and $i -lt $N)
|
||||
{
|
||||
$i++
|
||||
$next = Get-NextAliquot $X
|
||||
|
||||
if ($next)
|
||||
{
|
||||
if ($X -eq $next)
|
||||
{
|
||||
$i..$N | ForEach-Object {$X}
|
||||
$i = $N
|
||||
}
|
||||
else
|
||||
{
|
||||
$X = $next
|
||||
$X
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$i = $N
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function Classify-AlliquotSequence ([int[]]$Sequence)
|
||||
{
|
||||
$k = $Sequence[0]
|
||||
|
||||
if ($Sequence[-1] -eq 0) {return "terminating"}
|
||||
if ($Sequence[-1] -eq 1) {return "terminating"}
|
||||
if ($Sequence[1] -eq $k) {return "perfect" }
|
||||
if ($Sequence[2] -eq $k) {return "amicable" }
|
||||
if ($Sequence[3..($Sequence.Count-1)] -contains $k) {return "sociable" }
|
||||
if ($Sequence[-1] -eq $Sequence[-2] ) {return "aspiring" }
|
||||
if ($Sequence.Count -gt ($Sequence | Select -Unique).Count ) {return "cyclic" }
|
||||
|
||||
return "non-terminating and non-repeating through N = $($Sequence.Count)"
|
||||
}
|
||||
}
|
||||
Process
|
||||
{
|
||||
$_ | ForEach-Object {
|
||||
[PSCustomObject]@{
|
||||
Number = $_
|
||||
Classification = (Classify-AlliquotSequence -Sequence (Get-AliquotSequence -K $_ -N 16))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
$oneToTen = 1..10 | Get-Aliquot
|
||||
$selected = 11, 12, 28, 496, 220, 1184, 12496, 1264460, 790, 909, 562, 1064, 1488 | Get-Aliquot
|
||||
|
||||
$numbers = $oneToTen, $selected
|
||||
$numbers
|
||||
|
|
@ -1,45 +1,44 @@
|
|||
/*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). */
|
||||
if L='' then L=11 12 28 496 220 1184 12496 1264460 790 909 562 1064 1488 15355717786080
|
||||
big=2**47; NTlimit=16+1 /*limit for a non─terminating sequence.*/
|
||||
numeric digits max(9, 1+length(big)) /*be able to handle big numbers for // */
|
||||
#.=.; #.0=0; #.1=0 /*#. are the proper divisor sums. */
|
||||
say center('numbers from ' low " to " high, 79, "═")
|
||||
|
||||
do n=low to high /*process (probably) some low numbers. */
|
||||
call classify n /*call a subroutine to classify number.*/
|
||||
if L='' then L=11 12 28 496 220 1184 12496 1264460 790 909 562 1064 1488 15355717786080
|
||||
numeric digits 20 /*be able to handle the number: BIG */
|
||||
big=2**47; NTlimit=16+1 /*limits for a non─terminating sequence*/
|
||||
numeric digits max(9, 1 + length(big) ) /*be able to handle big numbers for // */
|
||||
#.=.; #.0=0; #.1=0 /*#. are the proper divisor sums. */
|
||||
say center('numbers from ' low " to " high, 79, "═")
|
||||
do n=low to high; call classify n /*call a subroutine to classify number.*/
|
||||
end /*n*/ /* [↑] process a range of integers. */
|
||||
say
|
||||
say center('first numbers for each classification', 79, "═")
|
||||
class.=0 /* [↓] ensure one number of each class*/
|
||||
do q=1 until class.sociable\==0 /*the only one that has to be counted. */
|
||||
call classify -q /*the minus (-) sign indicates ¬ tell. */
|
||||
call classify -q /*minus (-) sign indicates don't tell. */
|
||||
_=what; upper _; class._=class._+1 /*bump counter for this class sequence.*/
|
||||
if class._==1 then call show_class q,$ /*only display the first occurrence. */
|
||||
end /*q*/ /* [↑] process until all classes found*/
|
||||
say
|
||||
if class._==1 then say right(q, digits()) 'is' center(what, 15) $
|
||||
end /*q*/ /* [↑] only display the 1st occurrence*/
|
||||
say /* [↑] process until all classes found*/
|
||||
say center('classifications for specific numbers', 79, "═")
|
||||
|
||||
do i=1 for words(L) /*L: is a list of "special numbers".*/
|
||||
call classify word(L,i) /*call a subroutine to classify number.*/
|
||||
end /*i*/ /* [↑] process a list of integers. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
classify: parse arg a 1 aa; a=abs(a) /*obtain number that's to be classified*/
|
||||
if #.a\==. then s=#.a /*Was this number been summed before?*/
|
||||
else s=sigma(a) /*No, then classify number the hard way*/
|
||||
classify: parse arg a 1 aa; a=abs(a) /*obtain number that's to be classified*/
|
||||
if #.a\==. then s=#.a /*Was this number been summed before?*/
|
||||
else s=sigma(a) /*No, then classify number the hard way*/
|
||||
#.a=s; $=s /*define sum of the proper divisors. */
|
||||
what='terminating' /*assume this kind of classification. */
|
||||
c.=0; c.s=1 /*clear all cyclic sequences; set 1st.*/
|
||||
if $==a then what='perfect' /*check for a "perfect" number. */
|
||||
else do t=1 while s\==0 /*loop until sum isn't 0 or > big.*/
|
||||
m=word($, words($)) /*obtain the last number in sequence. */
|
||||
m=s /*obtain the last number in sequence. */
|
||||
if #.m==. then s=sigma(m) /*Not defined? Then sum proper divisors*/
|
||||
else s=#.m /*use the previously found integer. */
|
||||
if m==s & m\==0 then do; what='aspiring' ; leave; end
|
||||
if word($,2)==a then do; what='amicable' ; leave; end
|
||||
$=$ s /*append a sum to the integer sequence.*/
|
||||
parse var $ . word2 . /* " " 2nd " " " */
|
||||
if word2==a then do; what='amicable' ; leave; end
|
||||
$=$ s /*append a sum to the integer sequence.*/
|
||||
if s==a & t>3 then do; what='sociable' ; leave; end
|
||||
if c.s & m\==0 then do; what='cyclic' ; leave; end
|
||||
c.s=1 /*assign another possible cyclic number*/
|
||||
|
|
@ -47,17 +46,14 @@ classify: parse arg a 1 aa; a=abs(a) /*obtain number that's to be cl
|
|||
if t>NTlimit then do; what='non-terminating'; leave; end
|
||||
if s>big then do; what='NON-TERMINATING'; leave; end
|
||||
end /*t*/ /* [↑] only permit within reason. */
|
||||
if aa>0 then call show_class a,$ /*only display if A is positive. */
|
||||
return
|
||||
if aa>0 then say right(a, digits() ) 'is' center(what, 15) $
|
||||
return /* [↑] only display if A is positive.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
show_class: say right(arg(1),digits()) 'is' center(what,15) arg(2); return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
sigma: procedure expose #.; parse arg x; if x<2 then return 0; odd=x//2
|
||||
sigma: procedure expose #.; parse arg x; if x<2 then return 0; odd=x//2
|
||||
s=1 /* [↓] use only EVEN | ODD ints. ___*/
|
||||
do j=2+odd by 1+odd while j*j<x /*divide by all the integers up to √ X */
|
||||
if x//j==0 then s=s + j + x%j /*add the two divisors to the sum. */
|
||||
end /*j*/ /* [↑] % is the REXX integer division*/
|
||||
/* [↓] adjust for square. ___*/
|
||||
if j*j==x then s=s+j /*Was X a square? If so, add √ X */
|
||||
#.x=s /*define divison sum for argument X.*/
|
||||
end /*j*/ /* [↓] adjust for square. ___*/
|
||||
if j*j==x then s=s + j /*Was X a square? If so, add √ X */
|
||||
#.x=s /*define division sum for argument X.*/
|
||||
return s /*return " " " " " */
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
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();
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
fcn aliquot(k){ //-->Walker
|
||||
Walker(fcn(rk){
|
||||
k:=rk.value;
|
||||
rk.set((1).reduce((k + 1)/2, fcn(s,n,k){
|
||||
s + (k%n==0 and k!=n and n) // s + False == s + 0
|
||||
},0,k));
|
||||
k
|
||||
}.fp(Ref(k)))
|
||||
}(10).walk(15).println();
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
fcn classify(k){
|
||||
const MAX=(2).pow(47); // 140737488355328
|
||||
ak,aks:=aliquot(k), ak.walk(16);
|
||||
_,a2,a3:=aks;
|
||||
if(a2==k) return("perfect");
|
||||
if(a3==k) return("amicable");
|
||||
aspiring:='wrap(){
|
||||
foreach n in (aks.len()-1){ if(aks[n]==aks[n+1]) return(True) }
|
||||
False
|
||||
};
|
||||
cyclic:='wrap(){
|
||||
foreach n in (aks.len()-1){ if(aks[n+1,*].holds(aks[n])) return(aks[n]) }
|
||||
False
|
||||
};
|
||||
(if(aks.filter1('==(0))!=False) "terminating"
|
||||
else if(n:=aks[1,*].filter1n('==(k))) "sociable of length " + (n+1)
|
||||
else if(aks.filter1('>(MAX))) "non-terminating"
|
||||
else if(aspiring()) "aspiring"
|
||||
else if((c:=cyclic())!=False) "cyclic on " + c
|
||||
else "non-terminating" )
|
||||
+ " " + aks.filter();
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
[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() });
|
||||
Loading…
Add table
Add a link
Reference in a new issue