Aliquot sequence classifications: Difference between revisions
→{{header|REXX}}: make it ooRexx compatible and readable
(Add C# implementation) |
Walterpachl (talk | contribs) (→{{header|REXX}}: make it ooRexx compatible and readable) |
||
Line 5,006:
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
high=
low=word(low 1,1) /*obtain the LOW and HIGH (range). */
If LL='' Then
LL=11 12 28 496 220 1184 12496 1264460 790 909 562 1064 1488 15355717786080
big=2**47
Numeric Digits max(9,length(big)) /*be able To handle big numbers For // */
digs=digits()
dsum.=.
dsum.0=0
dsum.1=0 /* dsum. are the proper divisor sums. */
Say 'Numbers from ' low ' ---> ' high ' (inclusive):'
Do n=low To high
Call classify n
End
Say
Say 'First numbers for each classification:'
class.=0
Call classify -q /*minus (-) sign indicates don't tell. */
_=translate(what)
class._=class._+1
If class._==1 Then
Call out q,what,dd
End
Say 'Classifications for specific numbers:'
Do i=1 To words(LL)
Call classify
End
Exit
out:
Parse arg number,class,dd
dd.=''
Do di=1 By 1 While length(dd)>50
do dj=50 To 10 By -1
If substr(dd,dj,1)=' ' Then Leave
End
dd.di=left(dd,dj)
dd=substr(dd,dj+1)
End
dd.di=dd
Say right(number,digs)':' center(class,digs) dd.1||conti(1)
Do di=2 By 1 While dd.di>''
Say copies(' ',33)dd.di||conti(di)
End
Return
conti:
Parse arg this
next=this+1
If dd.next>'' Then Return '...'
Else Return ''
/*---------------------------------------------------------------------------------*/
classify:
Parse Arg a 1 aa
a=abs(a)
If dsum.a\==. Then
Else
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:}}
<pre>Numbers from 1 ---> 10 (inclusive):
1: terminating 0
2: terminating 1 0
Line 5,085 ⟶ 5,146:
10: terminating 8 7 1 0
First numbers for each classification:
1: terminating 0
6: perfect 6
25: aspiring 6
138:
13254 13830 19434 20886 21606 25098 26742 26754
220: amicable 284 220
562: cyclic 284 220 284
12496: sociable 14288 15472 14536 14264 12496
Classifications for specific numbers:
11: terminating 1 0
12: terminating 16 15 9 4 3 1 0
Line 5,107 ⟶ 5,169:
562: cyclic 284 220 284
1064: cyclic 1336 1184 1210 1184
1488:
95728 96720 236592 459792 881392 882384 1474608 ...
2461648 3172912 3173904
15355717786080: NON-TERMINATING 44534663601120 144940087464480
</pre>
|