Aliquot sequence classifications: Difference between revisions
Content added Content deleted
(Add C# implementation) |
Walterpachl (talk | contribs) (→{{header|REXX}}: make it ooRexx compatible and readable) |
||
Line 5,006: | Line 5,006: | ||
Both of the above limitations are imposed by this Rosetta Code task's restriction requirements: ''For the purposes of this task, ···''. |
Both of the above limitations are imposed by this Rosetta Code task's restriction requirements: ''For the purposes of this task, ···''. |
||
<syntaxhighlight lang="rexx">/*REXX program classifies various |
<syntaxhighlight lang="rexx">/*REXX program classifies various positive integers For types of aliquot sequences.*/ |
||
Parse Arg low high LL /*obtain optional arguments from the CL*/ |
|||
high= |
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 |
|||
If LL='' Then |
|||
numeric digits 100 /*be able to compute the number: BIG */ |
|||
LL=11 12 28 496 220 1184 12496 1264460 790 909 562 1064 1488 15355717786080 |
|||
big= 2**47; NTlimit= 16 + 1 /*limits for a non─terminating sequence*/ |
|||
Numeric Digits 20 /*be able To compute the number: BIG */ |
|||
big=2**47 |
|||
digs= digits() /*used for align numbers for the output*/ |
|||
NTlimit=16+1 /*limit for a non-terminating sequence */ |
|||
Numeric Digits max(9,length(big)) /*be able To handle big numbers For // */ |
|||
say center('numbers from ' low " ───► " high ' (inclusive)', 153, "═") |
|||
digs=digits() /*used For align numbers For the output*/ |
|||
dsum.=. |
|||
end /*n*/ /* [↑] process a range of integers. */ |
|||
dsum.0=0 |
|||
say |
|||
dsum.1=0 /* dsum. are the proper divisor sums. */ |
|||
say center('first numbers for each classification', 153, "═") |
|||
Say 'Numbers from ' low ' ---> ' high ' (inclusive):' |
|||
class.= 0 /* [↓] ensure one number of each class*/ |
|||
Do n=low To high /* process specified range */ |
|||
Call classify n /* call subroutine To classify number. */ |
|||
End |
|||
_= what; upper _ /*obtain the class and uppercase it. */ |
|||
Say |
|||
class._= class._ + 1 /*bump counter for this class sequence.*/ |
|||
Say 'First numbers for each classification:' |
|||
if class._==1 then say right(q, digs)':' center(what, digs) $ |
|||
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 /*minus (-) sign indicates don't tell. */ |
|||
say center('classifications for specific numbers', 153, "═") |
|||
_=translate(what) /*obtain the class and uppercase it. */ |
|||
class._=class._+1 /*bump counter For this class sequence.*/ |
|||
If class._==1 Then /*first number of this class */ |
|||
Call out q,what,dd |
|||
exit /*stick a fork in it, we're all done. */ |
|||
End |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
Say /* [?] process Until all classes found*/ |
|||
Say 'Classifications for specific numbers:' |
|||
if #.a\==. then s= #.a /*Was this number been summed before?*/ |
|||
Do i=1 To words(LL) /* process a list of "special numbers" */ |
|||
Call classify word(LL,i) /*call subroutine To classify number. */ |
|||
End |
|||
$= s /*define the start of integer sequence.*/ |
|||
Exit /*stick a fork in it,we're all done. */ |
|||
out: |
|||
c.= 0 /*clear all cyclic sequences (to zero).*/ |
|||
Parse arg number,class,dd |
|||
c.s= 1 /*set the first cyclic sequence. */ |
|||
dd.='' |
|||
if $==a then what= 'perfect' /*check for a "perfect" number. */ |
|||
Do di=1 By 1 While length(dd)>50 |
|||
else do t=1 while s>0 /*loop until sum isn't 0 or > big.*/ |
|||
do dj=50 To 10 By -1 |
|||
m= s /*obtain the last number in sequence. */ |
|||
If substr(dd,dj,1)=' ' Then Leave |
|||
if #.m==. then s= sigma(m) /*Not defined? Then sum proper divisors*/ |
|||
End |
|||
else s= #.m /*use the previously found integer. */ |
|||
dd.di=left(dd,dj) |
|||
if m==s then if m>=0 then do; what= 'aspiring'; leave; end |
|||
dd=substr(dd,dj+1) |
|||
parse var $ . word2 . /*obtain the 2nd number in sequence. */ |
|||
End |
|||
if word2==a then do; what= 'amicable'; leave; end |
|||
dd.di=dd |
|||
$= $ s /*append a sum to the integer sequence.*/ |
|||
Say right(number,digs)':' center(class,digs) dd.1||conti(1) |
|||
if s==a then if t>3 then do; what= 'sociable'; leave; end |
|||
Do di=2 By 1 While dd.di>'' |
|||
if c.s then if m>0 then do; what= 'cyclic' ; leave; end |
|||
Say copies(' ',33)dd.di||conti(di) |
|||
c.s= 1 /*assign another possible cyclic number*/ |
|||
End |
|||
/* [↓] Rosetta Code task's limit: >16 */ |
|||
Return |
|||
if t>NTlimit then do; what= 'non─terminating'; leave; end |
|||
conti: |
|||
if s>big then do; what= 'NON─TERMINATING'; leave; end |
|||
Parse arg this |
|||
end /*t*/ /* [↑] only permit within reason. */ |
|||
next=this+1 |
|||
if aa>0 then say right(a, digs)':' center(what, digs) $ |
|||
If dd.next>'' Then Return '...' |
|||
return /* [↑] only display if AA is positive*/ |
|||
Else Return '' |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*---------------------------------------------------------------------------------*/ |
|||
sigma: procedure expose #. !.; parse arg x; if 11<2 then return 0; odd= x // 2 |
|||
classify: |
|||
s= 1 /* [↓] use EVEN or ODD integers. ___*/ |
|||
Parse Arg a 1 aa |
|||
do j=2+odd by 1+odd while j*j<x /*divide by all the integers up to √ X */ |
|||
a=abs(a) /*obtain number that's to be classified*/ |
|||
If dsum.a\==. Then |
|||
end /*j*/ /* [↓] adjust for square. ___*/ |
|||
s=dsum.a /*Was this number been summed before?*/ |
|||
Else |
|||
#.x= s /*memoize division sum for argument X.*/ |
|||
s=dsum(a) /*No,Then classify number the hard way */ |
|||
dsum.a=s /*define sum of the proper divisors. */ |
|||
dd=s /*define the start of integer sequence.*/ |
|||
what='terminating' /*assume this kind of classification. */ |
|||
c.=0 /*clear all cyclic sequences (to zero).*/ |
|||
c.s=1 /*set the first cyclic sequence. */ |
|||
If dd==a Then |
|||
what='perfect' /*check For a "perfect" number. */ |
|||
Else Do t=1 By 1 While s>0 /*loop Until sum isn't 0 or > big.*/ |
|||
m=s /*obtain the last number in sequence. */ |
|||
If dsum.m==. Then /*Not defined? */ |
|||
s=dsum(m) /* compute sum pro of per divisors */ |
|||
Else |
|||
s=dsum.m /*use the previously found integer. */ |
|||
If m==s Then |
|||
If m>=0 Then Do |
|||
what='aspiring' |
|||
Leave |
|||
End |
|||
If word(dd,2)=a Then Do |
|||
what='amicable' |
|||
Leave |
|||
End |
|||
dd=dd s /*append a sum To the integer sequence.*/ |
|||
If s==a Then |
|||
If t>3 Then Do |
|||
what='sociable' |
|||
Leave |
|||
End |
|||
If c.s Then |
|||
If m>0 Then Do |
|||
what='cyclic' |
|||
Leave |
|||
End |
|||
c.s=1 /*assign another possible cyclic number*/ |
|||
/* [?] Rosetta Code task's limit: >16 */ |
|||
If t>NTlimit Then Do |
|||
what='non-terminating' |
|||
Leave |
|||
End |
|||
If s>big Then Do |
|||
what='NON-TERMINATING' |
|||
Leave |
|||
End |
|||
End |
|||
If aa>0 Then /* display only if AA is positive */ |
|||
Call out a,what,dd |
|||
Return |
|||
/*---------------------------------------------------------------------------------*/ |
|||
dsum: Procedure Expose dsum. /* compute the sum of proper divisors */ |
|||
Parse Arg x |
|||
If x<2 Then |
|||
Return 0 |
|||
odd=x//2 |
|||
s=1 /* use EVEN or ODD integers. */ |
|||
Do j=2+odd by 1+odd While j*j<x /* divide by all the integers ) */ |
|||
/* up to but excluding sqrt(x) */ |
|||
If x//j==0 Then /* j is a divisor, so is x%j */ |
|||
s=s+j+x%j /*add the two divisors To the sum. */ |
|||
End |
|||
If j*j==x Then /* if x is a square */ |
|||
s=s+j /* add sqrt(X) */ |
|||
dsum.x=s /* memoize proper divisor sum of X */ |
|||
Return s /* return the proper divisor sum */ |
|||
</syntaxhighlight> |
|||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
<pre>Numbers from 1 ---> 10 (inclusive): |
|||
(Shown at three-quarter size.) |
|||
<pre style="font-size:75%"> |
|||
═════════════════════════════════════════════════════════numbers from 1 ───► 10 (inclusive)══════════════════════════════════════════════════════════ |
|||
1: terminating 0 |
1: terminating 0 |
||
2: terminating 1 0 |
2: terminating 1 0 |
||
Line 5,085: | Line 5,146: | ||
10: terminating 8 7 1 0 |
10: terminating 8 7 1 0 |
||
First numbers for each classification: |
|||
══════════════════════════════════════════════════════════first numbers for each classification══════════════════════════════════════════════════════════ |
|||
1: terminating 0 |
1: terminating 0 |
||
6: perfect 6 |
6: perfect 6 |
||
25: aspiring 6 |
25: aspiring 6 |
||
138: |
138: non-terminating 150 222 234 312 528 960 2088 3762 5598 6570 10746 ... |
||
13254 13830 19434 20886 21606 25098 26742 26754 |
|||
220: amicable 284 220 |
220: amicable 284 220 |
||
562: cyclic 284 220 284 |
562: cyclic 284 220 284 |
||
12496: sociable 14288 15472 14536 14264 12496 |
12496: sociable 14288 15472 14536 14264 12496 |
||
Classifications for specific numbers: |
|||
══════════════════════════════════════════════════════════classifications for specific numbers═══════════════════════════════════════════════════════════ |
|||
11: terminating 1 0 |
11: terminating 1 0 |
||
12: terminating 16 15 9 4 3 1 0 |
12: terminating 16 15 9 4 3 1 0 |
||
Line 5,107: | Line 5,169: | ||
562: cyclic 284 220 284 |
562: cyclic 284 220 284 |
||
1064: cyclic 1336 1184 1210 1184 |
1064: cyclic 1336 1184 1210 1184 |
||
1488: |
1488: non-terminating 2480 3472 4464 8432 9424 10416 21328 22320 55056 ... |
||
95728 96720 236592 459792 881392 882384 1474608 ... |
|||
15355717786080: NON─TERMINATING 44534663601120 144940087464480 |
|||
2461648 3172912 3173904 |
|||
15355717786080: NON-TERMINATING 44534663601120 144940087464480 |
|||
</pre> |
</pre> |
||