Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Abundant-odd-numbers/00-META.yaml
Normal file
2
Task/Abundant-odd-numbers/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Abundant_odd_numbers
|
||||
25
Task/Abundant-odd-numbers/00-TASK.txt
Normal file
25
Task/Abundant-odd-numbers/00-TASK.txt
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
An [[wp:Abundant_number|Abundant number]] is a number '''n''' for which the ''sum of divisors'' '''σ(n) > 2n''',
|
||||
<br>or, equivalently, the ''sum of proper divisors'' (or aliquot sum) '''s(n) > n'''.
|
||||
|
||||
|
||||
;E.G.:
|
||||
'''12''' is abundant, it has the proper divisors '''1,2,3,4 <small>&</small> 6''' which sum to '''16''' ( > '''12''' or '''n''');
|
||||
<br> or alternately, has the sigma sum of '''1,2,3,4,6 <small>&</small> 12''' which sum to '''28''' ( > '''24''' or '''2n''').
|
||||
|
||||
|
||||
Abundant numbers are common, though '''even''' abundant numbers seem to be much more common than '''odd''' abundant numbers.
|
||||
|
||||
To make things more interesting, this task is specifically about finding ''odd abundant numbers''.
|
||||
|
||||
|
||||
;Task
|
||||
*Find and display here: at least the first 25 abundant odd numbers and either their proper divisor sum or sigma sum.
|
||||
*Find and display here: the one thousandth abundant odd number and either its proper divisor sum or sigma sum.
|
||||
*Find and display here: the first abundant odd number greater than one billion (10<sup>9</sup>) and either its proper divisor sum or sigma sum.
|
||||
|
||||
|
||||
;References:
|
||||
:* [[oeis:A005231|OEIS:A005231: Odd abundant numbers (odd numbers n whose sum of divisors exceeds 2n)]]
|
||||
:* American Journal of Mathematics, Vol. 35, No. 4 (Oct., 1913), pp. 413-422 - Finiteness of the Odd Perfect and Primitive Abundant Numbers with n Distinct Prime Factors (LE Dickson)
|
||||
<br><br>
|
||||
|
||||
41
Task/Abundant-odd-numbers/11l/abundant-odd-numbers.11l
Normal file
41
Task/Abundant-odd-numbers/11l/abundant-odd-numbers.11l
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
V oddNumber = 1
|
||||
V aCount = 0
|
||||
V dSum = 0
|
||||
|
||||
F divisorSum(n)
|
||||
V sum = 1
|
||||
V i = Int(sqrt(n) + 1)
|
||||
|
||||
L(d) 2 .< i
|
||||
I n % d == 0
|
||||
sum += d
|
||||
V otherD = n I/ d
|
||||
I otherD != d
|
||||
sum += otherD
|
||||
R sum
|
||||
|
||||
print(‘The first 25 abundant odd numbers:’)
|
||||
L aCount < 25
|
||||
dSum = divisorSum(oddNumber)
|
||||
I dSum > oddNumber
|
||||
aCount++
|
||||
print(‘#5 proper divisor sum: #.’.format(oddNumber, dSum))
|
||||
oddNumber += 2
|
||||
|
||||
L aCount < 1000
|
||||
dSum = divisorSum(oddNumber)
|
||||
I dSum > oddNumber
|
||||
aCount++
|
||||
oddNumber += 2
|
||||
print("\n1000th abundant odd number:")
|
||||
print(‘ ’(oddNumber - 2)‘ proper divisor sum: ’dSum)
|
||||
|
||||
oddNumber = 1000000001
|
||||
V found = 0B
|
||||
L !found
|
||||
dSum = divisorSum(oddNumber)
|
||||
I dSum > oddNumber
|
||||
found = 1B
|
||||
print("\nFirst abundant odd number > 1 000 000 000:")
|
||||
print(‘ ’oddNumber‘ proper divisor sum: ’dSum)
|
||||
oddNumber += 2
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
* Abundant odd numbers 18/09/2019
|
||||
ABUNODDS CSECT
|
||||
USING ABUNODDS,R13 base register
|
||||
B 72(R15) skip savearea
|
||||
DC 17F'0' savearea
|
||||
SAVE (14,12) save previous context
|
||||
ST R13,4(R15) link backward
|
||||
ST R15,8(R13) link forward
|
||||
LR R13,R15 set addressability
|
||||
LA R8,0 n=0
|
||||
LA R6,3 i=3
|
||||
DO WHILE=(C,R8,LT,NN1) do i=3 by 2 until n>=nn1
|
||||
BAL R14,SIGMA s=sigma(i)
|
||||
IF CR,R9,GT,R6 THEN if s>i then
|
||||
LA R8,1(R8) n++
|
||||
BAL R14,PRINT print results
|
||||
ENDIF , endif
|
||||
LA R6,2(R6) i+=2
|
||||
ENDDO , enddo i
|
||||
LA R8,0 n=0
|
||||
LA R6,3 i=3
|
||||
XR R1,R1 f=false
|
||||
DO WHILE=(C,R1,EQ,=F'0') do i=3 by 2 while not f
|
||||
BAL R14,SIGMA s=sigma(i)
|
||||
IF CR,R9,GT,R6 THEN if s>i then
|
||||
LA R8,1(R8) n++
|
||||
IF C,R8,GE,NN2 THEN if n>=nn2 then
|
||||
BAL R14,PRINT print results
|
||||
LA R1,1 f=true
|
||||
ENDIF , endif
|
||||
ENDIF , endif
|
||||
LA R6,2(R6) i+=2
|
||||
ENDDO , enddo i
|
||||
LA R8,0 n=0
|
||||
L R6,NN3 i=mm3
|
||||
LA R6,1(R6) +1
|
||||
XR R1,R1 f=false
|
||||
DO WHILE=(C,R1,EQ,=F'0') do i=nn3+1 by 2 while not f
|
||||
BAL R14,SIGMA s=sigma(i)
|
||||
IF CR,R9,GT,R6 THEN if s>i then
|
||||
BAL R14,PRINT print results
|
||||
LA R1,1 f=true
|
||||
ENDIF , endif
|
||||
LA R6,2(R6) i+=2
|
||||
ENDDO , enddo i
|
||||
L R13,4(0,R13) restore previous savearea pointer
|
||||
RETURN (14,12),RC=0 restore registers from calling save
|
||||
SIGMA CNOP 0,4 ---- subroutine sigma
|
||||
LA R9,1 s=1
|
||||
LA R7,3 j=3
|
||||
LR R5,R7 j
|
||||
MR R4,R7 j*j
|
||||
DO WHILE=(CR,R5,LT,R6) do j=3 by 2 while j*j<i
|
||||
LR R4,R6 i
|
||||
SRDA R4,32 ~
|
||||
DR R4,R7 i/j
|
||||
IF LTR,R4,Z,R4 THEN if mod(i,j)=0 then
|
||||
AR R9,R7 s+j
|
||||
LR R4,R6 i
|
||||
SRDA R4,32 ~
|
||||
DR R4,R7 i/j
|
||||
AR R9,R5 s=s+j+i/j
|
||||
ENDIF , endif
|
||||
LA R7,2(R7) j+=2
|
||||
LR R5,R7 j
|
||||
MR R4,R7 j*j
|
||||
ENDDO , enddo j
|
||||
IF CR,R5,EQ,R6 THEN if j*j=i then
|
||||
AR R9,R7 s=s+j
|
||||
ENDIF , endif
|
||||
BR R14 ---- end of subroutine sigma
|
||||
PRINT CNOP 0,4 ---- subroutine print
|
||||
XDECO R8,XDEC edit n
|
||||
MVC BUF(4),XDEC+8 output n
|
||||
XDECO R6,BUF+14 edit & output i
|
||||
XDECO R9,BUF+33 edit & output s
|
||||
XPRNT BUF,L'BUF print buffer
|
||||
BR R14 ---- end of subroutine print
|
||||
NN1 DC F'25' nn1=25
|
||||
NN2 DC F'1000' nn2=1000
|
||||
NN3 DC F'1000000000' nn3=1000000000
|
||||
BUF DC CL80'.... - number=............ sigma=............'
|
||||
XDEC DS CL12 temp for edit
|
||||
REGEQU equate registers
|
||||
END ABUNODDS
|
||||
|
|
@ -0,0 +1,547 @@
|
|||
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
|
||||
/* program abundant64.s */
|
||||
|
||||
/*******************************************/
|
||||
/* Constantes file */
|
||||
/*******************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly*/
|
||||
.include "../includeConstantesARM64.inc"
|
||||
|
||||
.equ NBDIVISORS, 1000
|
||||
|
||||
/*******************************************/
|
||||
/* Initialized data */
|
||||
/*******************************************/
|
||||
.data
|
||||
szMessStartPgm: .asciz "Program start \n"
|
||||
szMessEndPgm: .asciz "Program normal end.\n"
|
||||
szMessErrorArea: .asciz "\033[31mError : area divisors too small.\n"
|
||||
szMessError: .asciz "\033[31mError !!!\n"
|
||||
szMessErrGen: .asciz "Error end program.\n"
|
||||
szMessNbPrem: .asciz "This number is prime !!!.\n"
|
||||
szMessOverflow: .asciz "Dépassement de capacité vérification premier.\n"
|
||||
szMessResultFact: .asciz "// "
|
||||
|
||||
szCarriageReturn: .asciz "\n"
|
||||
|
||||
/* datas message display */
|
||||
szMessEntete: .asciz "The first 25 abundant odd numbers are:\n"
|
||||
szMessResult: .asciz "Number : @ sum : @ \n"
|
||||
|
||||
szMessEntete1: .asciz "The 1000 odd abundant number :\n"
|
||||
szMessEntete2: .asciz "First odd abundant number > 1000000000 :\n"
|
||||
/*******************************************/
|
||||
/* UnInitialized data */
|
||||
/*******************************************/
|
||||
.bss
|
||||
.align 4
|
||||
sZoneConv: .skip 24
|
||||
tbZoneDecom: .skip 16 * NBDIVISORS // facteur 8 octets nombre 8 octets
|
||||
/*******************************************/
|
||||
/* code section */
|
||||
/*******************************************/
|
||||
.text
|
||||
.global main
|
||||
main: // program start
|
||||
ldr x0,qAdrszMessStartPgm // display start message
|
||||
bl affichageMess
|
||||
|
||||
ldr x0,qAdrszMessEntete // display result message
|
||||
bl affichageMess
|
||||
mov x2,#1
|
||||
mov x3,#0
|
||||
1:
|
||||
mov x0,x2 // number
|
||||
bl testAbundant
|
||||
cmp x0,#1
|
||||
bne 3f
|
||||
add x3,x3,#1
|
||||
mov x0,x2
|
||||
mov x4,x1 // save sum
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl conversion10 // convert ascii string
|
||||
ldr x0,qAdrszMessResult
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl strInsertAtCharInc // and put in message
|
||||
mov x5,x0
|
||||
mov x0,x4 // sum
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl conversion10 // convert ascii string
|
||||
mov x0,x5
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl strInsertAtCharInc // and put in message
|
||||
|
||||
bl affichageMess
|
||||
3:
|
||||
add x2,x2,#2
|
||||
cmp x3,#25
|
||||
blt 1b
|
||||
|
||||
/* 1000 abundant number */
|
||||
ldr x0,qAdrszMessEntete1
|
||||
bl affichageMess
|
||||
mov x2,#1
|
||||
mov x3,#0
|
||||
4:
|
||||
mov x0,x2 // number
|
||||
bl testAbundant
|
||||
cmp x0,#1
|
||||
bne 6f
|
||||
add x3,x3,#1
|
||||
6:
|
||||
cmp x3,#1000
|
||||
cinc x2,x2,lt // add two
|
||||
cinc x2,x2,lt
|
||||
blt 4b
|
||||
mov x0,x2
|
||||
mov x4,x1 // save sum
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl conversion10 // convert ascii string
|
||||
ldr x0,qAdrszMessResult
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl strInsertAtCharInc // and put in message
|
||||
mov x5,x0
|
||||
mov x0,x4 // sum
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl conversion10 // convert ascii string
|
||||
mov x0,x5
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl strInsertAtCharInc // and put in message
|
||||
|
||||
bl affichageMess
|
||||
|
||||
/* abundant number>1000000000 */
|
||||
ldr x0,qAdrszMessEntete2
|
||||
bl affichageMess
|
||||
ldr x2,iN10P9
|
||||
add x2,x2,#1
|
||||
mov x3,#0
|
||||
7:
|
||||
mov x0,x2 // number
|
||||
bl testAbundant
|
||||
cmp x0,#1
|
||||
beq 8f
|
||||
add x2,x2,#2
|
||||
b 7b
|
||||
8:
|
||||
mov x0,x2
|
||||
mov x4,x1 // save sum
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl conversion10 // convert ascii string
|
||||
ldr x0,qAdrszMessResult
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl strInsertAtCharInc // and put in message
|
||||
mov x5,x0
|
||||
mov x0,x4 // sum
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl conversion10 // convert ascii string
|
||||
mov x0,x5
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl strInsertAtCharInc // and put in message
|
||||
|
||||
bl affichageMess
|
||||
|
||||
|
||||
|
||||
ldr x0,qAdrszMessEndPgm // display end message
|
||||
bl affichageMess
|
||||
b 100f
|
||||
99: // display error message
|
||||
ldr x0,qAdrszMessError
|
||||
bl affichageMess
|
||||
100: // standard end of the program
|
||||
mov x0, #0 // return code
|
||||
mov x8, #EXIT // request to exit program
|
||||
svc 0 // perform system call
|
||||
qAdrszMessStartPgm: .quad szMessStartPgm
|
||||
qAdrszMessEndPgm: .quad szMessEndPgm
|
||||
qAdrszMessError: .quad szMessError
|
||||
qAdrszCarriageReturn: .quad szCarriageReturn
|
||||
qAdrtbZoneDecom: .quad tbZoneDecom
|
||||
qAdrszMessEntete: .quad szMessEntete
|
||||
qAdrszMessEntete1: .quad szMessEntete1
|
||||
qAdrszMessEntete2: .quad szMessEntete2
|
||||
qAdrszMessResult: .quad szMessResult
|
||||
qAdrsZoneConv: .quad sZoneConv
|
||||
iN10P9: .quad 1000000000
|
||||
/******************************************************************/
|
||||
/* test if number is abundant number */
|
||||
/******************************************************************/
|
||||
/* x0 contains the number */
|
||||
/* x0 return 1 if abundant number else return 0 */
|
||||
/* x1 return sum */
|
||||
testAbundant:
|
||||
stp x2,lr,[sp,-16]! // save registres
|
||||
stp x3,x4,[sp,-16]! // save registres
|
||||
stp x5,x6,[sp,-16]! // save registres
|
||||
mov x6,x0 // save number
|
||||
ldr x1,qAdrtbZoneDecom
|
||||
bl decompFact // create area of divisors
|
||||
cmp x0,#1 // no divisors
|
||||
ble 99f
|
||||
lsl x5,x6,#1 // abondant number ?
|
||||
cmp x5,x2
|
||||
bgt 99f // no -> end
|
||||
mov x0,#1
|
||||
sub x1,x2,x6 // sum
|
||||
b 100f
|
||||
99:
|
||||
mov x0,0
|
||||
100:
|
||||
ldp x5,x6,[sp],16 // restaur des 2 registres
|
||||
ldp x3,x4,[sp],16 // restaur des 2 registres
|
||||
ldp x2,lr,[sp],16 // restaur des 2 registres
|
||||
ret
|
||||
/******************************************************************/
|
||||
/* decomposition en facteur */
|
||||
/******************************************************************/
|
||||
/* x0 contient le nombre à decomposer */
|
||||
decompFact:
|
||||
stp x3,lr,[sp,-16]! // save registres
|
||||
stp x4,x5,[sp,-16]! // save registres
|
||||
stp x6,x7,[sp,-16]! // save registres
|
||||
stp x8,x9,[sp,-16]! // save registres
|
||||
stp x10,x11,[sp,-16]! // save registres
|
||||
mov x5,x1
|
||||
mov x8,x0 // save number
|
||||
bl isPrime // prime ?
|
||||
cmp x0,#1
|
||||
beq 98f // yes is prime
|
||||
mov x1,#1
|
||||
str x1,[x5] // first factor
|
||||
mov x12,#1 // divisors sum
|
||||
mov x11,#1 // number odd divisors
|
||||
mov x4,#1 // indice divisors table
|
||||
mov x1,#2 // first divisor
|
||||
mov x6,#0 // previous divisor
|
||||
mov x7,#0 // number of same divisors
|
||||
2:
|
||||
mov x0,x8 // dividende
|
||||
udiv x2,x0,x1 // x1 divisor x2 quotient x3 remainder
|
||||
msub x3,x2,x1,x0
|
||||
cmp x3,#0
|
||||
bne 5f // if remainder <> zero -> no divisor
|
||||
mov x8,x2 // else quotient -> new dividende
|
||||
cmp x1,x6 // same divisor ?
|
||||
beq 4f // yes
|
||||
mov x7,x4 // number factors in table
|
||||
mov x9,#0 // indice
|
||||
21:
|
||||
ldr x10,[x5,x9,lsl #3 ] // load one factor
|
||||
mul x10,x1,x10 // multiply
|
||||
str x10,[x5,x7,lsl #3] // and store in the table
|
||||
tst x10,#1 // divisor odd ?
|
||||
cinc x11,x11,ne
|
||||
add x12,x12,x10
|
||||
add x7,x7,#1 // and increment counter
|
||||
add x9,x9,#1
|
||||
cmp x9,x4
|
||||
blt 21b
|
||||
mov x4,x7
|
||||
mov x6,x1 // new divisor
|
||||
b 7f
|
||||
4: // same divisor
|
||||
sub x9,x4,#1
|
||||
mov x7,x4
|
||||
41:
|
||||
ldr x10,[x5,x9,lsl #3 ]
|
||||
cmp x10,x1
|
||||
sub x13,x9,1
|
||||
csel x9,x13,x9,ne
|
||||
bne 41b
|
||||
sub x9,x4,x9
|
||||
42:
|
||||
ldr x10,[x5,x9,lsl #3 ]
|
||||
mul x10,x1,x10
|
||||
str x10,[x5,x7,lsl #3] // and store in the table
|
||||
tst x10,#1 // divsor odd ?
|
||||
cinc x11,x11,ne
|
||||
add x12,x12,x10
|
||||
add x7,x7,#1 // and increment counter
|
||||
add x9,x9,#1
|
||||
cmp x9,x4
|
||||
blt 42b
|
||||
mov x4,x7
|
||||
b 7f // and loop
|
||||
|
||||
/* not divisor -> increment next divisor */
|
||||
5:
|
||||
cmp x1,#2 // if divisor = 2 -> add 1
|
||||
add x13,x1,#1 // add 1
|
||||
add x14,x1,#2 // else add 2
|
||||
csel x1,x13,x14,eq
|
||||
b 2b
|
||||
|
||||
/* divisor -> test if new dividende is prime */
|
||||
7:
|
||||
mov x3,x1 // save divisor
|
||||
cmp x8,#1 // dividende = 1 ? -> end
|
||||
beq 10f
|
||||
mov x0,x8 // new dividende is prime ?
|
||||
mov x1,#0
|
||||
bl isPrime // the new dividende is prime ?
|
||||
cmp x0,#1
|
||||
bne 10f // the new dividende is not prime
|
||||
|
||||
cmp x8,x6 // else dividende is same divisor ?
|
||||
beq 9f // yes
|
||||
mov x7,x4 // number factors in table
|
||||
mov x9,#0 // indice
|
||||
71:
|
||||
ldr x10,[x5,x9,lsl #3 ] // load one factor
|
||||
mul x10,x8,x10 // multiply
|
||||
str x10,[x5,x7,lsl #3] // and store in the table
|
||||
tst x10,#1 // divsor odd ?
|
||||
cinc x11,x11,ne
|
||||
add x12,x12,x10
|
||||
add x7,x7,#1 // and increment counter
|
||||
add x9,x9,#1
|
||||
cmp x9,x4
|
||||
blt 71b
|
||||
mov x4,x7
|
||||
mov x7,#0
|
||||
b 11f
|
||||
9:
|
||||
sub x9,x4,#1
|
||||
mov x7,x4
|
||||
91:
|
||||
ldr x10,[x5,x9,lsl #3 ]
|
||||
cmp x10,x8
|
||||
sub x13,x9,#1
|
||||
csel x9,x13,x9,ne
|
||||
bne 91b
|
||||
sub x9,x4,x9
|
||||
92:
|
||||
ldr x10,[x5,x9,lsl #3 ]
|
||||
mul x10,x8,x10
|
||||
str x10,[x5,x7,lsl #3] // and store in the table
|
||||
tst x10,#1 // divisor odd ?
|
||||
cinc x11,x11,ne
|
||||
add x12,x12,x10
|
||||
add x7,x7,#1 // and increment counter
|
||||
add x9,x9,#1
|
||||
cmp x9,x4
|
||||
blt 92b
|
||||
mov x4,x7
|
||||
b 11f
|
||||
|
||||
10:
|
||||
mov x1,x3 // current divisor = new divisor
|
||||
cmp x1,x8 // current divisor > new dividende ?
|
||||
ble 2b // no -> loop
|
||||
|
||||
/* end decomposition */
|
||||
11:
|
||||
mov x0,x4 // return number of table items
|
||||
mov x2,x12 // return sum
|
||||
mov x1,x11 // return number of odd divisor
|
||||
mov x3,#0
|
||||
str x3,[x5,x4,lsl #3] // store zéro in last table item
|
||||
b 100f
|
||||
|
||||
|
||||
98:
|
||||
//ldr x0,qAdrszMessNbPrem
|
||||
//bl affichageMess
|
||||
mov x0,#1 // return code
|
||||
b 100f
|
||||
99:
|
||||
ldr x0,qAdrszMessError
|
||||
bl affichageMess
|
||||
mov x0,#-1 // error code
|
||||
b 100f
|
||||
|
||||
|
||||
100:
|
||||
ldp x10,x11,[sp],16 // restaur des 2 registres
|
||||
ldp x8,x9,[sp],16 // restaur des 2 registres
|
||||
ldp x6,x7,[sp],16 // restaur des 2 registres
|
||||
ldp x4,x5,[sp],16 // restaur des 2 registres
|
||||
ldp x3,lr,[sp],16 // restaur des 2 registres
|
||||
ret // retour adresse lr x30
|
||||
qAdrszMessErrGen: .quad szMessErrGen
|
||||
qAdrszMessNbPrem: .quad szMessNbPrem
|
||||
/***************************************************/
|
||||
/* Verification si un nombre est premier */
|
||||
/***************************************************/
|
||||
/* x0 contient le nombre à verifier */
|
||||
/* x0 retourne 1 si premier 0 sinon */
|
||||
isPrime:
|
||||
stp x1,lr,[sp,-16]! // save registres
|
||||
stp x2,x3,[sp,-16]! // save registres
|
||||
mov x2,x0
|
||||
sub x1,x0,#1
|
||||
cmp x2,0
|
||||
beq 99f // retourne zéro
|
||||
cmp x2,2 // pour 1 et 2 retourne 1
|
||||
ble 2f
|
||||
mov x0,#2
|
||||
bl moduloPux64
|
||||
bcs 100f // erreur overflow
|
||||
cmp x0,#1
|
||||
bne 99f // Pas premier
|
||||
cmp x2,3
|
||||
beq 2f
|
||||
mov x0,#3
|
||||
bl moduloPux64
|
||||
blt 100f // erreur overflow
|
||||
cmp x0,#1
|
||||
bne 99f
|
||||
|
||||
cmp x2,5
|
||||
beq 2f
|
||||
mov x0,#5
|
||||
bl moduloPux64
|
||||
bcs 100f // erreur overflow
|
||||
cmp x0,#1
|
||||
bne 99f // Pas premier
|
||||
|
||||
cmp x2,7
|
||||
beq 2f
|
||||
mov x0,#7
|
||||
bl moduloPux64
|
||||
bcs 100f // erreur overflow
|
||||
cmp x0,#1
|
||||
bne 99f // Pas premier
|
||||
|
||||
cmp x2,11
|
||||
beq 2f
|
||||
mov x0,#11
|
||||
bl moduloPux64
|
||||
bcs 100f // erreur overflow
|
||||
cmp x0,#1
|
||||
bne 99f // Pas premier
|
||||
|
||||
cmp x2,13
|
||||
beq 2f
|
||||
mov x0,#13
|
||||
bl moduloPux64
|
||||
bcs 100f // erreur overflow
|
||||
cmp x0,#1
|
||||
bne 99f // Pas premier
|
||||
2:
|
||||
cmn x0,0 // carry à zero pas d'erreur
|
||||
mov x0,1 // premier
|
||||
b 100f
|
||||
99:
|
||||
cmn x0,0 // carry à zero pas d'erreur
|
||||
mov x0,#0 // Pas premier
|
||||
100:
|
||||
ldp x2,x3,[sp],16 // restaur des 2 registres
|
||||
ldp x1,lr,[sp],16 // restaur des 2 registres
|
||||
ret // retour adresse lr x30
|
||||
|
||||
/**************************************************************/
|
||||
/********************************************************/
|
||||
/* Calcul modulo de b puissance e modulo m */
|
||||
/* Exemple 4 puissance 13 modulo 497 = 445 */
|
||||
/********************************************************/
|
||||
/* x0 nombre */
|
||||
/* x1 exposant */
|
||||
/* x2 modulo */
|
||||
moduloPux64:
|
||||
stp x1,lr,[sp,-16]! // save registres
|
||||
stp x3,x4,[sp,-16]! // save registres
|
||||
stp x5,x6,[sp,-16]! // save registres
|
||||
stp x7,x8,[sp,-16]! // save registres
|
||||
stp x9,x10,[sp,-16]! // save registres
|
||||
cbz x0,100f
|
||||
cbz x1,100f
|
||||
mov x8,x0
|
||||
mov x7,x1
|
||||
mov x6,1 // resultat
|
||||
udiv x4,x8,x2
|
||||
msub x9,x4,x2,x8 // contient le reste
|
||||
1:
|
||||
tst x7,1
|
||||
beq 2f
|
||||
mul x4,x9,x6
|
||||
umulh x5,x9,x6
|
||||
//cbnz x5,99f
|
||||
mov x6,x4
|
||||
mov x0,x6
|
||||
mov x1,x5
|
||||
bl divisionReg128U
|
||||
cbnz x1,99f // overflow
|
||||
mov x6,x3
|
||||
2:
|
||||
mul x8,x9,x9
|
||||
umulh x5,x9,x9
|
||||
mov x0,x8
|
||||
mov x1,x5
|
||||
bl divisionReg128U
|
||||
cbnz x1,99f // overflow
|
||||
mov x9,x3
|
||||
lsr x7,x7,1
|
||||
cbnz x7,1b
|
||||
mov x0,x6 // result
|
||||
cmn x0,0 // carry à zero pas d'erreur
|
||||
b 100f
|
||||
99:
|
||||
ldr x1,qAdrszMessOverflow
|
||||
bl afficheErreur
|
||||
cmp x0,0 // carry à un car erreur
|
||||
mov x0,-1 // code erreur
|
||||
|
||||
100:
|
||||
ldp x9,x10,[sp],16 // restaur des 2 registres
|
||||
ldp x7,x8,[sp],16 // restaur des 2 registres
|
||||
ldp x5,x6,[sp],16 // restaur des 2 registres
|
||||
ldp x3,x4,[sp],16 // restaur des 2 registres
|
||||
ldp x1,lr,[sp],16 // restaur des 2 registres
|
||||
ret // retour adresse lr x30
|
||||
qAdrszMessOverflow: .quad szMessOverflow
|
||||
/***************************************************/
|
||||
/* division d un nombre de 128 bits par un nombre de 64 bits */
|
||||
/***************************************************/
|
||||
/* x0 contient partie basse dividende */
|
||||
/* x1 contient partie haute dividente */
|
||||
/* x2 contient le diviseur */
|
||||
/* x0 retourne partie basse quotient */
|
||||
/* x1 retourne partie haute quotient */
|
||||
/* x3 retourne le reste */
|
||||
divisionReg128U:
|
||||
stp x6,lr,[sp,-16]! // save registres
|
||||
stp x4,x5,[sp,-16]! // save registres
|
||||
mov x5,#0 // raz du reste R
|
||||
mov x3,#128 // compteur de boucle
|
||||
mov x4,#0 // dernier bit
|
||||
1:
|
||||
lsl x5,x5,#1 // on decale le reste de 1
|
||||
tst x1,1<<63 // test du bit le plus à gauche
|
||||
lsl x1,x1,#1 // on decale la partie haute du quotient de 1
|
||||
beq 2f
|
||||
orr x5,x5,#1 // et on le pousse dans le reste R
|
||||
2:
|
||||
tst x0,1<<63
|
||||
lsl x0,x0,#1 // puis on decale la partie basse
|
||||
beq 3f
|
||||
orr x1,x1,#1 // et on pousse le bit de gauche dans la partie haute
|
||||
3:
|
||||
orr x0,x0,x4 // position du dernier bit du quotient
|
||||
mov x4,#0 // raz du bit
|
||||
cmp x5,x2
|
||||
blt 4f
|
||||
sub x5,x5,x2 // on enleve le diviseur du reste
|
||||
mov x4,#1 // dernier bit à 1
|
||||
4:
|
||||
// et boucle
|
||||
subs x3,x3,#1
|
||||
bgt 1b
|
||||
lsl x1,x1,#1 // on decale le quotient de 1
|
||||
tst x0,1<<63
|
||||
lsl x0,x0,#1 // puis on decale la partie basse
|
||||
beq 5f
|
||||
orr x1,x1,#1
|
||||
5:
|
||||
orr x0,x0,x4 // position du dernier bit du quotient
|
||||
mov x3,x5
|
||||
100:
|
||||
ldp x4,x5,[sp],16 // restaur des 2 registres
|
||||
ldp x6,lr,[sp],16 // restaur des 2 registres
|
||||
ret // retour adresse lr x30
|
||||
/********************************************************/
|
||||
/* File Include fonctions */
|
||||
/********************************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly */
|
||||
.include "../includeARM64.inc"
|
||||
76
Task/Abundant-odd-numbers/ALGOL-68/abundant-odd-numbers.alg
Normal file
76
Task/Abundant-odd-numbers/ALGOL-68/abundant-odd-numbers.alg
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
BEGIN
|
||||
# find some abundant odd numbers - numbers where the sum of the proper #
|
||||
# divisors is bigger than the number #
|
||||
# itself #
|
||||
|
||||
# returns the sum of the proper divisors of n #
|
||||
PROC divisor sum = ( INT n )INT:
|
||||
BEGIN
|
||||
INT sum := 1;
|
||||
FOR d FROM 2 TO ENTIER sqrt( n ) DO
|
||||
IF n MOD d = 0 THEN
|
||||
sum +:= d;
|
||||
IF INT other d := n OVER d;
|
||||
other d /= d
|
||||
THEN
|
||||
sum +:= other d
|
||||
FI
|
||||
FI
|
||||
OD;
|
||||
sum
|
||||
END # divisor sum # ;
|
||||
# find numbers required by the task #
|
||||
BEGIN
|
||||
# first 25 odd abundant numbers #
|
||||
INT odd number := 1;
|
||||
INT a count := 0;
|
||||
INT d sum := 0;
|
||||
print( ( "The first 25 abundant odd numbers:", newline ) );
|
||||
WHILE a count < 25 DO
|
||||
IF ( d sum := divisor sum( odd number ) ) > odd number THEN
|
||||
a count +:= 1;
|
||||
print( ( whole( odd number, -6 )
|
||||
, " proper divisor sum: "
|
||||
, whole( d sum, 0 )
|
||||
, newline
|
||||
)
|
||||
)
|
||||
FI;
|
||||
odd number +:= 2
|
||||
OD;
|
||||
# 1000th odd abundant number #
|
||||
WHILE a count < 1 000 DO
|
||||
IF ( d sum := divisor sum( odd number ) ) > odd number THEN
|
||||
a count := a count + 1
|
||||
FI;
|
||||
odd number +:= 2
|
||||
OD;
|
||||
print( ( "1000th abundant odd number:"
|
||||
, newline
|
||||
, " "
|
||||
, whole( odd number - 2, 0 )
|
||||
, " proper divisor sum: "
|
||||
, whole( d sum, 0 )
|
||||
, newline
|
||||
)
|
||||
);
|
||||
# first odd abundant number > one billion #
|
||||
odd number := 1 000 000 001;
|
||||
BOOL found := FALSE;
|
||||
WHILE NOT found DO
|
||||
IF ( d sum := divisor sum( odd number ) ) > odd number THEN
|
||||
found := TRUE;
|
||||
print( ( "First abundant odd number > 1 000 000 000:"
|
||||
, newline
|
||||
, " "
|
||||
, whole( odd number, 0 )
|
||||
, " proper divisor sum: "
|
||||
, whole( d sum, 0 )
|
||||
, newline
|
||||
)
|
||||
)
|
||||
FI;
|
||||
odd number +:= 2
|
||||
OD
|
||||
END
|
||||
END
|
||||
75
Task/Abundant-odd-numbers/ALGOL-W/abundant-odd-numbers.alg
Normal file
75
Task/Abundant-odd-numbers/ALGOL-W/abundant-odd-numbers.alg
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
begin
|
||||
% find some abundant odd numbers - numbers where the sum of the proper %
|
||||
% divisors is bigger than the number %
|
||||
% itself %
|
||||
|
||||
% computes the sum of the divisors of v using the prime %
|
||||
% factorisation %
|
||||
integer procedure divisor_sum( integer value v ) ; begin
|
||||
integer total, power, n, p;
|
||||
total := 1; power := 2; n := v;
|
||||
% Deal with powers of 2 first %
|
||||
while not odd( n ) do begin
|
||||
total := total + power;
|
||||
power := power * 2;
|
||||
n := n div 2
|
||||
end while_not_odd_n ;
|
||||
% Odd prime factors up to the square root %
|
||||
p := 3;
|
||||
while ( p * p ) <= n do begin
|
||||
integer sum;
|
||||
sum := 1;
|
||||
power := p;
|
||||
while n rem p = 0 do begin
|
||||
sum := sum + power;
|
||||
power := power * p;
|
||||
n := n div p
|
||||
end while_n_rem_p_eq_0 ;
|
||||
p := p + 2;
|
||||
total := total * sum
|
||||
end while_p_x_p_le_n ;
|
||||
% If n > 1 then it's prime %
|
||||
if n > 1 then total := total * ( n + 1 );
|
||||
total
|
||||
end divisor_sum ;
|
||||
% returns the sum of the proper divisors of v %
|
||||
integer procedure divisorSum( integer value v ) ;
|
||||
if v < 2 then 0 else divisor_sum( v ) - v;
|
||||
% find numbers required by the task %
|
||||
begin
|
||||
integer aCount, oddNumber, dSum;
|
||||
logical foundOddAn;
|
||||
% first 25 odd abundant numbers %
|
||||
oddNumber := 1;
|
||||
aCount := 0;
|
||||
write( "The first 25 abundant odd numbers:" );
|
||||
while aCount < 25 do begin
|
||||
dSum := divisorSum( oddNumber );
|
||||
if dSum > oddNumber then begin
|
||||
aCount := aCount + 1;
|
||||
write( i_w := 6, oddNumber, " proper divisor sum: ", dSum )
|
||||
end if_dSum_gt_oddNumber ;
|
||||
oddNumber := oddNumber + 2
|
||||
end while_aCount_lt_1000 ;
|
||||
% 1000th odd abundant number %
|
||||
while aCount < 1000 do begin
|
||||
dSum := divisorSum( oddNumber );
|
||||
if dSum > oddNumber then aCount := aCount + 1;
|
||||
oddNumber := oddNumber + 2
|
||||
end while_aCount_lt_1000 ;
|
||||
write( "1000th abundant odd number: " );
|
||||
write( oddNumber - 2, " proper divisor sum: ", dSum );
|
||||
% first odd abundant number > one billion %
|
||||
oddNumber := 1000000001;
|
||||
foundOddAn := false;
|
||||
while not foundOddAn do begin
|
||||
dSum := divisorSum( oddNumber );
|
||||
if dSum > oddNumber then begin
|
||||
foundOddAn := true;
|
||||
write( "First abundant odd number > 1000000000: " );
|
||||
write( oddNumber, " proper divisor sum: ", dSum )
|
||||
end if_dSum_gt_oddNumber ;
|
||||
oddNumber := oddNumber + 2
|
||||
end while_not_foundOddAn ;
|
||||
end
|
||||
end.
|
||||
516
Task/Abundant-odd-numbers/ARM-Assembly/abundant-odd-numbers.arm
Normal file
516
Task/Abundant-odd-numbers/ARM-Assembly/abundant-odd-numbers.arm
Normal file
|
|
@ -0,0 +1,516 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program abundant.s */
|
||||
|
||||
/* REMARK 1 : this program use routines in a include file
|
||||
see task Include a file language arm assembly
|
||||
for the routine affichageMess conversion10
|
||||
see at end of this program the instruction include */
|
||||
/* for constantes see task include a file in arm assembly */
|
||||
/************************************/
|
||||
/* Constantes */
|
||||
/************************************/
|
||||
.include "../constantes.inc"
|
||||
|
||||
.equ NBDIVISORS, 1000
|
||||
|
||||
/*******************************************/
|
||||
/* Initialized data */
|
||||
/*******************************************/
|
||||
.data
|
||||
szMessStartPgm: .asciz "Program start \n"
|
||||
szMessEndPgm: .asciz "Program normal end.\n"
|
||||
szMessErrorArea: .asciz "\033[31mError : area divisors too small.\n"
|
||||
szMessError: .asciz "\033[31mError !!!\n"
|
||||
szMessErrGen: .asciz "Error end program.\n"
|
||||
szMessNbPrem: .asciz "This number is prime !!!.\n"
|
||||
szMessResultFact: .asciz "@ "
|
||||
|
||||
szCarriageReturn: .asciz "\n"
|
||||
|
||||
/* datas message display */
|
||||
szMessEntete: .asciz "The first 25 abundant odd numbers are:\n"
|
||||
szMessResult: .asciz "Number : @ sum : @ \n"
|
||||
|
||||
szMessEntete1: .asciz "The 1000 odd abundant number :\n"
|
||||
szMessEntete2: .asciz "First odd abundant number > 1000000000 :\n"
|
||||
/*******************************************/
|
||||
/* UnInitialized data */
|
||||
/*******************************************/
|
||||
.bss
|
||||
.align 4
|
||||
sZoneConv: .skip 24
|
||||
tbZoneDecom: .skip 4 * NBDIVISORS // facteur 4 octets
|
||||
/*******************************************/
|
||||
/* code section */
|
||||
/*******************************************/
|
||||
.text
|
||||
.global main
|
||||
main: @ program start
|
||||
ldr r0,iAdrszMessStartPgm @ display start message
|
||||
bl affichageMess
|
||||
|
||||
ldr r0,iAdrszMessEntete @ display result message
|
||||
bl affichageMess
|
||||
mov r2,#1
|
||||
mov r3,#0
|
||||
1:
|
||||
mov r0,r2 @ number
|
||||
bl testAbundant
|
||||
cmp r0,#1
|
||||
bne 3f
|
||||
add r3,#1
|
||||
mov r0,r2
|
||||
mov r4,r1 @ save sum
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl conversion10 @ convert ascii string
|
||||
ldr r0,iAdrszMessResult
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl strInsertAtCharInc @ and put in message
|
||||
mov r5,r0
|
||||
mov r0,r4 @ sum
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl conversion10 @ convert ascii string
|
||||
mov r0,r5
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl strInsertAtCharInc @ and put in message
|
||||
|
||||
bl affichageMess
|
||||
3:
|
||||
add r2,r2,#2
|
||||
cmp r3,#25
|
||||
blt 1b
|
||||
|
||||
/* 1000 abundant number */
|
||||
ldr r0,iAdrszMessEntete1
|
||||
bl affichageMess
|
||||
mov r2,#1
|
||||
mov r3,#0
|
||||
4:
|
||||
mov r0,r2 @ number
|
||||
bl testAbundant
|
||||
cmp r0,#1
|
||||
bne 6f
|
||||
add r3,#1
|
||||
6:
|
||||
cmp r3,#1000
|
||||
addlt r2,r2,#2
|
||||
blt 4b
|
||||
mov r0,r2
|
||||
mov r4,r1 @ save sum
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl conversion10 @ convert ascii string
|
||||
ldr r0,iAdrszMessResult
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl strInsertAtCharInc @ and put in message
|
||||
mov r5,r0
|
||||
mov r0,r4 @ sum
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl conversion10 @ convert ascii string
|
||||
mov r0,r5
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl strInsertAtCharInc @ and put in message
|
||||
|
||||
bl affichageMess
|
||||
|
||||
/* abundant number>1000000000 */
|
||||
ldr r0,iAdrszMessEntete2
|
||||
bl affichageMess
|
||||
ldr r2,iN10P9
|
||||
add r2,#1
|
||||
mov r3,#0
|
||||
7:
|
||||
mov r0,r2 @ number
|
||||
bl testAbundant
|
||||
cmp r0,#1
|
||||
beq 8f
|
||||
add r2,r2,#2
|
||||
b 7b
|
||||
8:
|
||||
mov r0,r2
|
||||
mov r4,r1 @ save sum
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl conversion10 @ convert ascii string
|
||||
ldr r0,iAdrszMessResult
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl strInsertAtCharInc @ and put in message
|
||||
mov r5,r0
|
||||
mov r0,r4 @ sum
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl conversion10 @ convert ascii string
|
||||
mov r0,r5
|
||||
ldr r1,iAdrsZoneConv
|
||||
bl strInsertAtCharInc @ and put in message
|
||||
|
||||
bl affichageMess
|
||||
|
||||
|
||||
|
||||
ldr r0,iAdrszMessEndPgm @ display end message
|
||||
bl affichageMess
|
||||
b 100f
|
||||
99: @ display error message
|
||||
ldr r0,iAdrszMessError
|
||||
bl affichageMess
|
||||
100: @ standard end of the program
|
||||
mov r0, #0 @ return code
|
||||
mov r7, #EXIT @ request to exit program
|
||||
svc 0 @ perform system call
|
||||
iAdrszMessStartPgm: .int szMessStartPgm
|
||||
iAdrszMessEndPgm: .int szMessEndPgm
|
||||
iAdrszMessError: .int szMessError
|
||||
iAdrszCarriageReturn: .int szCarriageReturn
|
||||
iAdrtbZoneDecom: .int tbZoneDecom
|
||||
iAdrszMessEntete: .int szMessEntete
|
||||
iAdrszMessEntete1: .int szMessEntete1
|
||||
iAdrszMessEntete2: .int szMessEntete2
|
||||
iAdrszMessResult: .int szMessResult
|
||||
iAdrsZoneConv: .int sZoneConv
|
||||
iN10P9: .int 1000000000
|
||||
/******************************************************************/
|
||||
/* test if number is abundant number */
|
||||
/******************************************************************/
|
||||
/* r0 contains the number */
|
||||
/* r0 return 1 if Zumkeller number else return 0 */
|
||||
testAbundant:
|
||||
push {r2-r6,lr} @ save registers
|
||||
mov r6,r0 @ save number
|
||||
ldr r1,iAdrtbZoneDecom
|
||||
bl decompFact @ create area of divisors
|
||||
cmp r0,#1 @ no divisors
|
||||
movle r0,#0
|
||||
ble 100f
|
||||
lsl r5,r6,#1 @ abondant number ?
|
||||
cmp r5,r2
|
||||
movgt r0,#0
|
||||
bgt 100f @ no -> end
|
||||
mov r0,#1
|
||||
sub r1,r2,r6 @ sum
|
||||
100:
|
||||
pop {r2-r6,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
|
||||
|
||||
|
||||
/******************************************************************/
|
||||
/* factor decomposition */
|
||||
/******************************************************************/
|
||||
/* r0 contains number */
|
||||
/* r1 contains address of divisors area */
|
||||
/* r0 return divisors items in table */
|
||||
/* r1 return the number of odd divisors */
|
||||
/* r2 return the sum of divisors */
|
||||
decompFact:
|
||||
push {r3-r8,lr} @ save registers
|
||||
mov r5,r1
|
||||
mov r8,r0 @ save number
|
||||
bl isPrime @ prime ?
|
||||
cmp r0,#1
|
||||
beq 98f @ yes is prime
|
||||
mov r1,#1
|
||||
str r1,[r5] @ first factor
|
||||
mov r12,#1 @ divisors sum
|
||||
mov r11,#1 @ number odd divisors
|
||||
mov r4,#1 @ indice divisors table
|
||||
mov r1,#2 @ first divisor
|
||||
mov r6,#0 @ previous divisor
|
||||
mov r7,#0 @ number of same divisors
|
||||
2:
|
||||
mov r0,r8 @ dividende
|
||||
bl division @ r1 divisor r2 quotient r3 remainder
|
||||
cmp r3,#0
|
||||
bne 5f @ if remainder <> zero -> no divisor
|
||||
mov r8,r2 @ else quotient -> new dividende
|
||||
cmp r1,r6 @ same divisor ?
|
||||
beq 4f @ yes
|
||||
mov r7,r4 @ number factors in table
|
||||
mov r9,#0 @ indice
|
||||
21:
|
||||
ldr r10,[r5,r9,lsl #2 ] @ load one factor
|
||||
mul r10,r1,r10 @ multiply
|
||||
str r10,[r5,r7,lsl #2] @ and store in the table
|
||||
tst r10,#1 @ divisor odd ?
|
||||
addne r11,#1
|
||||
add r12,r10
|
||||
add r7,r7,#1 @ and increment counter
|
||||
add r9,r9,#1
|
||||
cmp r9,r4
|
||||
blt 21b
|
||||
mov r4,r7
|
||||
mov r6,r1 @ new divisor
|
||||
b 7f
|
||||
4: @ same divisor
|
||||
sub r9,r4,#1
|
||||
mov r7,r4
|
||||
41:
|
||||
ldr r10,[r5,r9,lsl #2 ]
|
||||
cmp r10,r1
|
||||
subne r9,#1
|
||||
bne 41b
|
||||
sub r9,r4,r9
|
||||
42:
|
||||
ldr r10,[r5,r9,lsl #2 ]
|
||||
mul r10,r1,r10
|
||||
str r10,[r5,r7,lsl #2] @ and store in the table
|
||||
tst r10,#1 @ divsor odd ?
|
||||
addne r11,#1
|
||||
add r12,r10
|
||||
add r7,r7,#1 @ and increment counter
|
||||
add r9,r9,#1
|
||||
cmp r9,r4
|
||||
blt 42b
|
||||
mov r4,r7
|
||||
b 7f @ and loop
|
||||
|
||||
/* not divisor -> increment next divisor */
|
||||
5:
|
||||
cmp r1,#2 @ if divisor = 2 -> add 1
|
||||
addeq r1,#1
|
||||
addne r1,#2 @ else add 2
|
||||
b 2b
|
||||
|
||||
/* divisor -> test if new dividende is prime */
|
||||
7:
|
||||
mov r3,r1 @ save divisor
|
||||
cmp r8,#1 @ dividende = 1 ? -> end
|
||||
beq 10f
|
||||
mov r0,r8 @ new dividende is prime ?
|
||||
mov r1,#0
|
||||
bl isPrime @ the new dividende is prime ?
|
||||
cmp r0,#1
|
||||
bne 10f @ the new dividende is not prime
|
||||
|
||||
cmp r8,r6 @ else dividende is same divisor ?
|
||||
beq 9f @ yes
|
||||
mov r7,r4 @ number factors in table
|
||||
mov r9,#0 @ indice
|
||||
71:
|
||||
ldr r10,[r5,r9,lsl #2 ] @ load one factor
|
||||
mul r10,r8,r10 @ multiply
|
||||
str r10,[r5,r7,lsl #2] @ and store in the table
|
||||
tst r10,#1 @ divsor odd ?
|
||||
addne r11,#1
|
||||
add r12,r10
|
||||
add r7,r7,#1 @ and increment counter
|
||||
add r9,r9,#1
|
||||
cmp r9,r4
|
||||
blt 71b
|
||||
mov r4,r7
|
||||
mov r7,#0
|
||||
b 11f
|
||||
9:
|
||||
sub r9,r4,#1
|
||||
mov r7,r4
|
||||
91:
|
||||
ldr r10,[r5,r9,lsl #2 ]
|
||||
cmp r10,r8
|
||||
subne r9,#1
|
||||
bne 91b
|
||||
sub r9,r4,r9
|
||||
92:
|
||||
ldr r10,[r5,r9,lsl #2 ]
|
||||
mul r10,r8,r10
|
||||
str r10,[r5,r7,lsl #2] @ and store in the table
|
||||
tst r10,#1 @ divisor odd ?
|
||||
addne r11,#1
|
||||
add r12,r10
|
||||
add r7,r7,#1 @ and increment counter
|
||||
add r9,r9,#1
|
||||
cmp r9,r4
|
||||
blt 92b
|
||||
mov r4,r7
|
||||
b 11f
|
||||
|
||||
10:
|
||||
mov r1,r3 @ current divisor = new divisor
|
||||
cmp r1,r8 @ current divisor > new dividende ?
|
||||
ble 2b @ no -> loop
|
||||
|
||||
/* end decomposition */
|
||||
11:
|
||||
mov r0,r4 @ return number of table items
|
||||
mov r2,r12 @ return sum
|
||||
mov r1,r11 @ return number of odd divisor
|
||||
mov r3,#0
|
||||
str r3,[r5,r4,lsl #2] @ store zéro in last table item
|
||||
b 100f
|
||||
|
||||
|
||||
98:
|
||||
//ldr r0,iAdrszMessNbPrem
|
||||
//bl affichageMess
|
||||
mov r0,#1 @ return code
|
||||
b 100f
|
||||
99:
|
||||
ldr r0,iAdrszMessError
|
||||
bl affichageMess
|
||||
mov r0,#-1 @ error code
|
||||
b 100f
|
||||
100:
|
||||
pop {r3-r8,lr} @ restaur registers
|
||||
bx lr
|
||||
iAdrszMessNbPrem: .int szMessNbPrem
|
||||
/***************************************************/
|
||||
/* check if a number is prime */
|
||||
/***************************************************/
|
||||
/* r0 contains the number */
|
||||
/* r0 return 1 if prime 0 else */
|
||||
@2147483647
|
||||
@4294967297
|
||||
@131071
|
||||
isPrime:
|
||||
push {r1-r6,lr} @ save registers
|
||||
cmp r0,#0
|
||||
beq 90f
|
||||
cmp r0,#17
|
||||
bhi 1f
|
||||
cmp r0,#3
|
||||
bls 80f @ for 1,2,3 return prime
|
||||
cmp r0,#5
|
||||
beq 80f @ for 5 return prime
|
||||
cmp r0,#7
|
||||
beq 80f @ for 7 return prime
|
||||
cmp r0,#11
|
||||
beq 80f @ for 11 return prime
|
||||
cmp r0,#13
|
||||
beq 80f @ for 13 return prime
|
||||
cmp r0,#17
|
||||
beq 80f @ for 17 return prime
|
||||
1:
|
||||
tst r0,#1 @ even ?
|
||||
beq 90f @ yes -> not prime
|
||||
mov r2,r0 @ save number
|
||||
sub r1,r0,#1 @ exposant n - 1
|
||||
mov r0,#3 @ base
|
||||
bl moduloPuR32 @ compute base power n - 1 modulo n
|
||||
cmp r0,#1
|
||||
bne 90f @ if <> 1 -> not prime
|
||||
|
||||
mov r0,#5
|
||||
bl moduloPuR32
|
||||
cmp r0,#1
|
||||
bne 90f
|
||||
|
||||
mov r0,#7
|
||||
bl moduloPuR32
|
||||
cmp r0,#1
|
||||
bne 90f
|
||||
|
||||
mov r0,#11
|
||||
bl moduloPuR32
|
||||
cmp r0,#1
|
||||
bne 90f
|
||||
|
||||
mov r0,#13
|
||||
bl moduloPuR32
|
||||
cmp r0,#1
|
||||
bne 90f
|
||||
|
||||
mov r0,#17
|
||||
bl moduloPuR32
|
||||
cmp r0,#1
|
||||
bne 90f
|
||||
80:
|
||||
mov r0,#1 @ is prime
|
||||
b 100f
|
||||
90:
|
||||
mov r0,#0 @ no prime
|
||||
100: @ fin standard de la fonction
|
||||
pop {r1-r6,lr} @ restaur des registres
|
||||
bx lr @ retour de la fonction en utilisant lr
|
||||
/********************************************************/
|
||||
/* Calcul modulo de b puissance e modulo m */
|
||||
/* Exemple 4 puissance 13 modulo 497 = 445 */
|
||||
/* */
|
||||
/********************************************************/
|
||||
/* r0 nombre */
|
||||
/* r1 exposant */
|
||||
/* r2 modulo */
|
||||
/* r0 return result */
|
||||
moduloPuR32:
|
||||
push {r1-r7,lr} @ save registers
|
||||
cmp r0,#0 @ verif <> zero
|
||||
beq 100f
|
||||
cmp r2,#0 @ verif <> zero
|
||||
beq 100f @ TODO: vérifier les cas d erreur
|
||||
1:
|
||||
mov r4,r2 @ save modulo
|
||||
mov r5,r1 @ save exposant
|
||||
mov r6,r0 @ save base
|
||||
mov r3,#1 @ start result
|
||||
|
||||
mov r1,#0 @ division de r0,r1 par r2
|
||||
bl division32R
|
||||
mov r6,r2 @ base <- remainder
|
||||
2:
|
||||
tst r5,#1 @ exposant even or odd
|
||||
beq 3f
|
||||
umull r0,r1,r6,r3
|
||||
mov r2,r4
|
||||
bl division32R
|
||||
mov r3,r2 @ result <- remainder
|
||||
3:
|
||||
umull r0,r1,r6,r6
|
||||
mov r2,r4
|
||||
bl division32R
|
||||
mov r6,r2 @ base <- remainder
|
||||
|
||||
lsr r5,#1 @ left shift 1 bit
|
||||
cmp r5,#0 @ end ?
|
||||
bne 2b
|
||||
mov r0,r3
|
||||
100: @ fin standard de la fonction
|
||||
pop {r1-r7,lr} @ restaur des registres
|
||||
bx lr @ retour de la fonction en utilisant lr
|
||||
|
||||
/***************************************************/
|
||||
/* division number 64 bits in 2 registers by number 32 bits */
|
||||
/***************************************************/
|
||||
/* r0 contains lower part dividende */
|
||||
/* r1 contains upper part dividende */
|
||||
/* r2 contains divisor */
|
||||
/* r0 return lower part quotient */
|
||||
/* r1 return upper part quotient */
|
||||
/* r2 return remainder */
|
||||
division32R:
|
||||
push {r3-r9,lr} @ save registers
|
||||
mov r6,#0 @ init upper upper part remainder !!
|
||||
mov r7,r1 @ init upper part remainder with upper part dividende
|
||||
mov r8,r0 @ init lower part remainder with lower part dividende
|
||||
mov r9,#0 @ upper part quotient
|
||||
mov r4,#0 @ lower part quotient
|
||||
mov r5,#32 @ bits number
|
||||
1: @ begin loop
|
||||
lsl r6,#1 @ shift upper upper part remainder
|
||||
lsls r7,#1 @ shift upper part remainder
|
||||
orrcs r6,#1
|
||||
lsls r8,#1 @ shift lower part remainder
|
||||
orrcs r7,#1
|
||||
lsls r4,#1 @ shift lower part quotient
|
||||
lsl r9,#1 @ shift upper part quotient
|
||||
orrcs r9,#1
|
||||
@ divisor sustract upper part remainder
|
||||
subs r7,r2
|
||||
sbcs r6,#0 @ and substract carry
|
||||
bmi 2f @ négative ?
|
||||
|
||||
@ positive or equal
|
||||
orr r4,#1 @ 1 -> right bit quotient
|
||||
b 3f
|
||||
2: @ negative
|
||||
orr r4,#0 @ 0 -> right bit quotient
|
||||
adds r7,r2 @ and restaur remainder
|
||||
adc r6,#0
|
||||
3:
|
||||
subs r5,#1 @ decrement bit size
|
||||
bgt 1b @ end ?
|
||||
mov r0,r4 @ lower part quotient
|
||||
mov r1,r9 @ upper part quotient
|
||||
mov r2,r7 @ remainder
|
||||
100: @ function end
|
||||
pop {r3-r9,lr} @ restaur registers
|
||||
bx lr
|
||||
|
||||
/***************************************************/
|
||||
/* ROUTINES INCLUDE */
|
||||
/***************************************************/
|
||||
.include "../affichage.inc"
|
||||
34
Task/Abundant-odd-numbers/AWK/abundant-odd-numbers.awk
Normal file
34
Task/Abundant-odd-numbers/AWK/abundant-odd-numbers.awk
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
# syntax: GAWK -f ABUNDANT_ODD_NUMBERS.AWK
|
||||
# converted from C
|
||||
BEGIN {
|
||||
print(" index number sum")
|
||||
fmt = "%8s %10d %10d\n"
|
||||
n = 1
|
||||
for (c=0; c<25; n+=2) {
|
||||
if (n < sum_proper_divisors(n)) {
|
||||
printf(fmt,++c,n,sum)
|
||||
}
|
||||
}
|
||||
for (; c<1000; n+=2) {
|
||||
if (n < sum_proper_divisors(n)) {
|
||||
c++
|
||||
}
|
||||
}
|
||||
printf(fmt,1000,n-2,sum)
|
||||
for (n=1000000001; ; n+=2) {
|
||||
if (n < sum_proper_divisors(n)) {
|
||||
break
|
||||
}
|
||||
}
|
||||
printf(fmt,"1st > 1B",n,sum)
|
||||
exit(0)
|
||||
}
|
||||
function sum_proper_divisors(n, j) {
|
||||
sum = 1
|
||||
for (i=3; i<sqrt(n)+1; i+=2) {
|
||||
if (n % i == 0) {
|
||||
sum += i + (i == (j = n / i) ? 0 : j)
|
||||
}
|
||||
}
|
||||
return(sum)
|
||||
}
|
||||
57
Task/Abundant-odd-numbers/Ada/abundant-odd-numbers.ada
Normal file
57
Task/Abundant-odd-numbers/Ada/abundant-odd-numbers.ada
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
with Ada.Text_IO, Generic_Divisors;
|
||||
|
||||
procedure Odd_Abundant is
|
||||
function Same(P: Positive) return Positive is (P);
|
||||
|
||||
package Divisor_Sum is new Generic_Divisors
|
||||
(Result_Type => Natural, None => 0, One => Same, Add => "+");
|
||||
|
||||
function Abundant(N: Positive) return Boolean is
|
||||
(Divisor_Sum.Process(N) > N);
|
||||
|
||||
package NIO is new Ada.Text_IO.Integer_IO(Natural);
|
||||
|
||||
Current: Positive := 1;
|
||||
|
||||
procedure Print_Abundant_Line
|
||||
(Idx: Positive; N: Positive; With_Idx: Boolean:= True) is
|
||||
begin
|
||||
if With_Idx then
|
||||
NIO.Put(Idx, 6); Ada.Text_IO.Put(" |");
|
||||
else
|
||||
Ada.Text_IO.Put(" *** |");
|
||||
end if;
|
||||
NIO.Put(N, 12); Ada.Text_IO.Put(" | ");
|
||||
NIO.Put(Divisor_Sum.Process(N), 12); Ada.Text_IO.New_Line;
|
||||
end Print_Abundant_Line;
|
||||
|
||||
begin
|
||||
-- the first 25 abundant odd numbers
|
||||
Ada.Text_IO.Put_Line(" index | number | proper divisor sum ");
|
||||
Ada.Text_IO.Put_Line("-------+-------------+--------------------");
|
||||
for I in 1 .. 25 loop
|
||||
while not Abundant(Current) loop
|
||||
Current := Current + 2;
|
||||
end loop;
|
||||
Print_Abundant_Line(I, Current);
|
||||
Current := Current + 2;
|
||||
end loop;
|
||||
|
||||
-- the one thousandth abundant odd number
|
||||
Ada.Text_IO.Put_Line("-------+-------------+--------------------");
|
||||
for I in 26 .. 1_000 loop
|
||||
Current := Current + 2;
|
||||
while not Abundant(Current) loop
|
||||
Current := Current + 2;
|
||||
end loop;
|
||||
end loop;
|
||||
Print_Abundant_Line(1000, Current);
|
||||
|
||||
-- the first abundant odd number greater than 10**9
|
||||
Ada.Text_IO.Put_Line("-------+-------------+--------------------");
|
||||
Current := 10**9+1;
|
||||
while not Abundant(Current) loop
|
||||
Current := Current + 2;
|
||||
end loop;
|
||||
Print_Abundant_Line(1, Current, False);
|
||||
end Odd_Abundant;
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
on aliquotSum(n)
|
||||
if (n < 2) then return 0
|
||||
set sum to 1
|
||||
set sqrt to n ^ 0.5
|
||||
set limit to sqrt div 1
|
||||
if (limit = sqrt) then
|
||||
set sum to sum + limit
|
||||
set limit to limit - 1
|
||||
end if
|
||||
repeat with i from 2 to limit
|
||||
if (n mod i is 0) then set sum to sum + i + n div i
|
||||
end repeat
|
||||
|
||||
return sum
|
||||
end aliquotSum
|
||||
|
||||
-- Task code:
|
||||
local output, counter, n, sum, astid
|
||||
set output to {"The first 25 abundant odd numbers:"}
|
||||
set counter to 0
|
||||
set n to 1
|
||||
repeat until (counter = 25)
|
||||
set sum to aliquotSum(n)
|
||||
if (sum > n) then
|
||||
set end of output to " " & n & " (proper divisor sum: " & sum & ")"
|
||||
set counter to counter + 1
|
||||
end if
|
||||
set n to n + 2
|
||||
end repeat
|
||||
|
||||
set end of output to "The one thousandth:"
|
||||
repeat until (counter = 1000)
|
||||
set sum to aliquotSum(n)
|
||||
if (sum > n) then set counter to counter + 1
|
||||
set n to n + 2
|
||||
end repeat
|
||||
set end of output to " " & (n - 2) & " (proper divisor sum: " & sum & ")"
|
||||
|
||||
set end of output to "The first > 1,000,000,000:"
|
||||
set n to 1.000000001E+9
|
||||
set sum to aliquotSum(n)
|
||||
repeat until (sum > n)
|
||||
set n to n + 2
|
||||
set sum to aliquotSum(n)
|
||||
end repeat
|
||||
set end of output to " " & (n div 1000000) & text 2 thru -1 of ((1000000 + ((n mod 1000000) as integer)) as text) & ¬
|
||||
" (proper divisor sum: " & (sum div 1000000) & text 2 thru -1 of ((1000000 + ((sum mod 1000000) as integer)) as text) & ")"
|
||||
|
||||
set astid to AppleScript's text item delimiters
|
||||
set AppleScript's text item delimiters to linefeed
|
||||
set output to output as text
|
||||
set AppleScript's text item delimiters to astid
|
||||
return output
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
"The first 25 abundant odd numbers:
|
||||
945 (proper divisor sum: 975)
|
||||
1575 (proper divisor sum: 1649)
|
||||
2205 (proper divisor sum: 2241)
|
||||
2835 (proper divisor sum: 2973)
|
||||
3465 (proper divisor sum: 4023)
|
||||
4095 (proper divisor sum: 4641)
|
||||
4725 (proper divisor sum: 5195)
|
||||
5355 (proper divisor sum: 5877)
|
||||
5775 (proper divisor sum: 6129)
|
||||
5985 (proper divisor sum: 6495)
|
||||
6435 (proper divisor sum: 6669)
|
||||
6615 (proper divisor sum: 7065)
|
||||
6825 (proper divisor sum: 7063)
|
||||
7245 (proper divisor sum: 7731)
|
||||
7425 (proper divisor sum: 7455)
|
||||
7875 (proper divisor sum: 8349)
|
||||
8085 (proper divisor sum: 8331)
|
||||
8415 (proper divisor sum: 8433)
|
||||
8505 (proper divisor sum: 8967)
|
||||
8925 (proper divisor sum: 8931)
|
||||
9135 (proper divisor sum: 9585)
|
||||
9555 (proper divisor sum: 9597)
|
||||
9765 (proper divisor sum: 10203)
|
||||
10395 (proper divisor sum: 12645)
|
||||
11025 (proper divisor sum: 11946)
|
||||
The one thousandth:
|
||||
492975 (proper divisor sum: 519361)
|
||||
The first > 1,000,000,000:
|
||||
1000000575 (proper divisor sum: 1083561009)"
|
||||
29
Task/Abundant-odd-numbers/Arturo/abundant-odd-numbers.arturo
Normal file
29
Task/Abundant-odd-numbers/Arturo/abundant-odd-numbers.arturo
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
abundant?: function [n]-> (2*n) < sum factors n
|
||||
|
||||
print "the first 25 abundant odd numbers:"
|
||||
[i, found]: @[new 1, new 0]
|
||||
while [found<25][
|
||||
if abundant? i [
|
||||
inc 'found
|
||||
print [i "=> sum:" sum factors i]
|
||||
]
|
||||
'i + 2
|
||||
]
|
||||
|
||||
[i, found]: @[new 1, new 0]
|
||||
while [found<1000][
|
||||
if abundant? i [
|
||||
inc 'found
|
||||
]
|
||||
'i + 2
|
||||
]
|
||||
print ["the 1000th abundant odd number:" i-2 "=> sum:" sum factors i-2]
|
||||
|
||||
i: new 1 + 10^9
|
||||
while ø [
|
||||
if abundant? i [
|
||||
print ["the first abundant odd number greater than one billion (10^9):" i "=> sum:" sum factors i]
|
||||
break
|
||||
]
|
||||
'i + 2
|
||||
]
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
Abundant(num){
|
||||
sum := 0, str := ""
|
||||
for n, bool in proper_divisors(num)
|
||||
sum += n, str .= (str?"+":"") n
|
||||
return sum > num ? str " = " sum : 0
|
||||
}
|
||||
proper_divisors(n) {
|
||||
Array := []
|
||||
if n = 1
|
||||
return Array
|
||||
Array[1] := true
|
||||
x := Floor(Sqrt(n))
|
||||
loop, % x+1
|
||||
if !Mod(n, i:=A_Index+1) && (floor(n/i) < n)
|
||||
Array[floor(n/i)] := true
|
||||
Loop % n/x
|
||||
if !Mod(n, i:=A_Index+1) && (i < n)
|
||||
Array[i] := true
|
||||
return Array
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
output := "First 25 abundant odd numbers:`n"
|
||||
while (count<1000)
|
||||
{
|
||||
oddNum := 2*A_Index-1
|
||||
if (str := Abundant(oddNum))
|
||||
{
|
||||
count++
|
||||
if (count<=25)
|
||||
output .= oddNum " " str "`n"
|
||||
if (count = 1000)
|
||||
output .= "`nOne thousandth abundant odd number:`n" oddNum " " str "`n"
|
||||
}
|
||||
}
|
||||
count := 0
|
||||
while !count
|
||||
{
|
||||
num := 2*A_Index -1 + 1000000000
|
||||
if (str := Abundant(num))
|
||||
{
|
||||
count++
|
||||
output .= "`nFirst abundant odd number greater than one billion:`n" num " " str "`n"
|
||||
}
|
||||
}
|
||||
MsgBox % output
|
||||
return
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
numimpar = 1
|
||||
contar = 0
|
||||
sumaDiv = 0
|
||||
|
||||
function SumaDivisores(n)
|
||||
# Devuelve la suma de los divisores propios de n
|
||||
suma = 1
|
||||
i = int(sqr(n))
|
||||
|
||||
for d = 2 to i
|
||||
if n % d = 0 then
|
||||
suma += d
|
||||
otroD = n \ d
|
||||
if otroD <> d Then suma += otroD
|
||||
end if
|
||||
Next d
|
||||
Return suma
|
||||
End Function
|
||||
|
||||
# Encontrar los números requeridos por la tarea:
|
||||
# primeros 25 números abundantes impares
|
||||
Print "Los primeros 25 números impares abundantes:"
|
||||
While contar < 25
|
||||
sumaDiv = SumaDivisores(numimpar)
|
||||
If sumaDiv > numimpar Then
|
||||
contar += 1
|
||||
Print numimpar & " suma divisoria adecuada: " & sumaDiv
|
||||
End If
|
||||
numimpar += 2
|
||||
End While
|
||||
|
||||
# 1000er número impar abundante
|
||||
While contar < 1000
|
||||
sumaDiv = SumaDivisores(numimpar)
|
||||
If sumaDiv > numimpar Then contar += 1
|
||||
numimpar += 2
|
||||
End While
|
||||
Print Chr(10) & "1000º número impar abundante:"
|
||||
Print " " & (numimpar - 2) & " suma divisoria adecuada: " & sumaDiv
|
||||
|
||||
# primer número impar abundante > mil millones (millardo)
|
||||
numimpar = 1000000001
|
||||
encontrado = False
|
||||
While Not encontrado
|
||||
sumaDiv = SumaDivisores(numimpar)
|
||||
If sumaDiv > numimpar Then
|
||||
encontrado = True
|
||||
Print Chr(10) & "Primer número impar abundante > 1 000 000 000:"
|
||||
Print " " & numimpar & " suma divisoria adecuada: " & sumaDiv
|
||||
End If
|
||||
numimpar += 2
|
||||
End While
|
||||
End
|
||||
82
Task/Abundant-odd-numbers/C++/abundant-odd-numbers.cpp
Normal file
82
Task/Abundant-odd-numbers/C++/abundant-odd-numbers.cpp
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
std::vector<int> divisors(int n) {
|
||||
std::vector<int> divs{ 1 };
|
||||
std::vector<int> divs2;
|
||||
|
||||
for (int i = 2; i*i <= n; i++) {
|
||||
if (n%i == 0) {
|
||||
int j = n / i;
|
||||
divs.push_back(i);
|
||||
if (i != j) {
|
||||
divs2.push_back(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
std::copy(divs2.crbegin(), divs2.crend(), std::back_inserter(divs));
|
||||
|
||||
return divs;
|
||||
}
|
||||
|
||||
int sum(const std::vector<int>& divs) {
|
||||
return std::accumulate(divs.cbegin(), divs.cend(), 0);
|
||||
}
|
||||
|
||||
std::string sumStr(const std::vector<int>& divs) {
|
||||
auto it = divs.cbegin();
|
||||
auto end = divs.cend();
|
||||
std::stringstream ss;
|
||||
|
||||
if (it != end) {
|
||||
ss << *it;
|
||||
it = std::next(it);
|
||||
}
|
||||
while (it != end) {
|
||||
ss << " + " << *it;
|
||||
it = std::next(it);
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
int abundantOdd(int searchFrom, int countFrom, int countTo, bool printOne) {
|
||||
int count = countFrom;
|
||||
int n = searchFrom;
|
||||
for (; count < countTo; n += 2) {
|
||||
auto divs = divisors(n);
|
||||
int tot = sum(divs);
|
||||
if (tot > n) {
|
||||
count++;
|
||||
if (printOne && count < countTo) {
|
||||
continue;
|
||||
}
|
||||
auto s = sumStr(divs);
|
||||
if (printOne) {
|
||||
printf("%d < %s = %d\n", n, s.c_str(), tot);
|
||||
} else {
|
||||
printf("%2d. %5d < %s = %d\n", count, n, s.c_str(), tot);
|
||||
}
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int main() {
|
||||
using namespace std;
|
||||
|
||||
const int max = 25;
|
||||
cout << "The first " << max << " abundant odd numbers are:\n";
|
||||
int n = abundantOdd(1, 0, 25, false);
|
||||
|
||||
cout << "\nThe one thousandth abundant odd number is:\n";
|
||||
abundantOdd(n, 25, 1000, true);
|
||||
|
||||
cout << "\nThe first abundant odd number above one billion is:\n";
|
||||
abundantOdd(1e9 + 1, 0, 1, true);
|
||||
|
||||
return 0;
|
||||
}
|
||||
27
Task/Abundant-odd-numbers/C-sharp/abundant-odd-numbers.cs
Normal file
27
Task/Abundant-odd-numbers/C-sharp/abundant-odd-numbers.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
using static System.Console;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public static class AbundantOddNumbers
|
||||
{
|
||||
public static void Main() {
|
||||
WriteLine("First 25 abundant odd numbers:");
|
||||
foreach (var x in AbundantNumbers().Take(25)) WriteLine(x.Format());
|
||||
WriteLine();
|
||||
WriteLine($"The 1000th abundant odd number: {AbundantNumbers().ElementAt(999).Format()}");
|
||||
WriteLine();
|
||||
WriteLine($"First abundant odd number > 1b: {AbundantNumbers(1_000_000_001).First().Format()}");
|
||||
}
|
||||
|
||||
static IEnumerable<(int n, int sum)> AbundantNumbers(int start = 3) =>
|
||||
start.UpBy(2).Select(n => (n, sum: n.DivisorSum())).Where(x => x.sum > x.n);
|
||||
|
||||
static int DivisorSum(this int n) => 3.UpBy(2).TakeWhile(i => i * i <= n).Where(i => n % i == 0)
|
||||
.Select(i => (a:i, b:n/i)).Sum(p => p.a == p.b ? p.a : p.a + p.b) + 1;
|
||||
|
||||
static IEnumerable<int> UpBy(this int n, int step) {
|
||||
for (int i = n; ; i+=step) yield return i;
|
||||
}
|
||||
|
||||
static string Format(this (int n, int sum) pair) => $"{pair.n:N0} with sum {pair.sum:N0}";
|
||||
}
|
||||
23
Task/Abundant-odd-numbers/C/abundant-odd-numbers.c
Normal file
23
Task/Abundant-odd-numbers/C/abundant-odd-numbers.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
// The following function is for odd numbers ONLY
|
||||
// Please use "for (unsigned i = 2, j; i*i <= n; i ++)" for even and odd numbers
|
||||
unsigned sum_proper_divisors(const unsigned n) {
|
||||
unsigned sum = 1;
|
||||
for (unsigned i = 3, j; i < sqrt(n)+1; i += 2) if (n % i == 0) sum += i + (i == (j = n / i) ? 0 : j);
|
||||
return sum;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
unsigned n, c;
|
||||
for (n = 1, c = 0; c < 25; n += 2) if (n < sum_proper_divisors(n)) printf("%u: %u\n", ++c, n);
|
||||
|
||||
for ( ; c < 1000; n += 2) if (n < sum_proper_divisors(n)) c ++;
|
||||
printf("\nThe one thousandth abundant odd number is: %u\n", n);
|
||||
|
||||
for (n = 1000000001 ;; n += 2) if (n < sum_proper_divisors(n)) break;
|
||||
printf("The first abundant odd number above one billion is: %u\n", n);
|
||||
|
||||
return 0;
|
||||
}
|
||||
62
Task/Abundant-odd-numbers/CLU/abundant-odd-numbers.clu
Normal file
62
Task/Abundant-odd-numbers/CLU/abundant-odd-numbers.clu
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
% Integer square root
|
||||
isqrt = proc (s: int) returns (int)
|
||||
x0: int := s / 2
|
||||
if x0 = 0 then
|
||||
return(s)
|
||||
else
|
||||
x1: int := (x0 + s/x0) / 2
|
||||
while x1 < x0 do
|
||||
x0 := x1
|
||||
x1 := (x0 + s/x0) / 2
|
||||
end
|
||||
return(x0)
|
||||
end
|
||||
end isqrt
|
||||
|
||||
% Calculate aliquot sum (for odd numbers only)
|
||||
aliquot = proc (n: int) returns (int)
|
||||
sum: int := 1
|
||||
for i: int in int$from_to_by(3, isqrt(n)+1, 2) do
|
||||
if n//i = 0 then
|
||||
j: int := n / i
|
||||
sum := sum + i
|
||||
if i ~= j then
|
||||
sum := sum + j
|
||||
end
|
||||
end
|
||||
end
|
||||
return(sum)
|
||||
end aliquot
|
||||
|
||||
% Generate abundant odd numbers
|
||||
abundant_odd = iter (n: int) yields (int)
|
||||
while true do
|
||||
if n < aliquot(n) then yield(n) end
|
||||
n := n + 2
|
||||
end
|
||||
end abundant_odd
|
||||
|
||||
start_up = proc ()
|
||||
po: stream := stream$primary_output()
|
||||
|
||||
count: int := 0
|
||||
for n: int in abundant_odd(1) do
|
||||
count := count + 1
|
||||
if count <= 25 cor count = 1000 then
|
||||
stream$putl(po, int$unparse(count)
|
||||
|| ":\t"
|
||||
|| int$unparse(n)
|
||||
|| "\taliquot: "
|
||||
|| int$unparse(aliquot(n)))
|
||||
if count = 1000 then break end
|
||||
end
|
||||
end
|
||||
|
||||
for n: int in abundant_odd(1000000001) do
|
||||
stream$putl(po, "First above 1 billion: "
|
||||
|| int$unparse(n)
|
||||
|| " aliquot: "
|
||||
|| int$unparse(aliquot(n)))
|
||||
break
|
||||
end
|
||||
end start_up
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
;; * Loading the external libraries
|
||||
(eval-when (:compile-toplevel :load-toplevel)
|
||||
(ql:quickload '("cl-annot" "iterate" "alexandria")))
|
||||
|
||||
;; * The package definition
|
||||
(defpackage :abundant-numbers
|
||||
(:use :common-lisp :cl-annot :iterate)
|
||||
(:import-from :alexandria :butlast))
|
||||
(in-package :abundant-numbers)
|
||||
|
||||
(annot:enable-annot-syntax)
|
||||
|
||||
;; * Calculating the divisors
|
||||
@inline
|
||||
(defun divisors (n)
|
||||
"Returns the divisors of N without sorting them."
|
||||
@type fixnum n
|
||||
(iter
|
||||
(for divisor from (isqrt n) downto 1)
|
||||
(for (values m rem) = (floor n divisor))
|
||||
@type fixnum divisor
|
||||
(when (zerop rem)
|
||||
(collecting divisor into result)
|
||||
(adjoining m into result))
|
||||
(finally (return result))))
|
||||
|
||||
;; * Calculating the sum of divisors
|
||||
(defun sum-of-divisors (n)
|
||||
"Returns the sum of the proper divisors of N."
|
||||
@type fixnum n
|
||||
(reduce #'+ (butlast (divisors n))))
|
||||
|
||||
;; * Task 1
|
||||
(time
|
||||
(progn
|
||||
(format t " Task 1~%")
|
||||
(iter
|
||||
(with i = 0)
|
||||
(for n from 1 by 2)
|
||||
(for sum-of-divisors = (sum-of-divisors n))
|
||||
@type fixnum i n sum-of-divisors
|
||||
(while (< i 25))
|
||||
(when (< n sum-of-divisors)
|
||||
(incf i)
|
||||
(format t "~5D: ~6D ~7D~%" i n sum-of-divisors)))
|
||||
|
||||
;; * Task 2
|
||||
(format t "~% Task 2~%")
|
||||
(iter
|
||||
(with i = 0)
|
||||
(until (= i 1000))
|
||||
(for n from 1 by 2)
|
||||
(for sum-of-divisors = (sum-of-divisors n))
|
||||
@type fixnum i n sum-of-divisors
|
||||
(when (< n sum-of-divisors)
|
||||
(incf i))
|
||||
(finally (format t "~5D: ~6D ~7D~%" i n sum-of-divisors)))
|
||||
|
||||
;; * Task 3
|
||||
(format t "~% Task 3~%")
|
||||
(iter
|
||||
(for n from (1+ (expt 10 9)) by 2)
|
||||
(for sum-of-divisors = (sum-of-divisors n))
|
||||
@type fixnum n sum-of-divisors
|
||||
(until (< n sum-of-divisors))
|
||||
(finally (format t "~D ~D~%~%" n sum-of-divisors)))))
|
||||
59
Task/Abundant-odd-numbers/D/abundant-odd-numbers.d
Normal file
59
Task/Abundant-odd-numbers/D/abundant-odd-numbers.d
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import std.stdio;
|
||||
|
||||
int[] divisors(int n) {
|
||||
import std.range;
|
||||
|
||||
int[] divs = [1];
|
||||
int[] divs2;
|
||||
|
||||
for (int i = 2; i * i <= n; i++) {
|
||||
if (n % i == 0) {
|
||||
int j = n / i;
|
||||
divs ~= i;
|
||||
if (i != j) {
|
||||
divs2 ~= j;
|
||||
}
|
||||
}
|
||||
}
|
||||
divs ~= retro(divs2).array;
|
||||
|
||||
return divs;
|
||||
}
|
||||
|
||||
int abundantOdd(int searchFrom, int countFrom, int countTo, bool printOne) {
|
||||
import std.algorithm.iteration;
|
||||
import std.array;
|
||||
import std.conv;
|
||||
|
||||
int count = countFrom;
|
||||
int n = searchFrom;
|
||||
for (; count < countTo; n += 2) {
|
||||
auto divs = divisors(n);
|
||||
int tot = sum(divs);
|
||||
if (tot > n) {
|
||||
count++;
|
||||
if (printOne && count < countTo) {
|
||||
continue;
|
||||
}
|
||||
auto s = divs.map!(to!string).join(" + ");
|
||||
if (printOne) {
|
||||
writefln("%d < %s = %d", n, s, tot);
|
||||
} else {
|
||||
writefln("%2d. %5d < %s = %d", count, n, s, tot);
|
||||
}
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
void main() {
|
||||
const int max = 25;
|
||||
writefln("The first %d abundant odd numbers are:", max);
|
||||
int n = abundantOdd(1, 0, 25, false);
|
||||
|
||||
writeln("\nThe one thousandth abundant odd number is:");
|
||||
abundantOdd(n, 25, 1000, true);
|
||||
|
||||
writeln("\nThe first abundant odd number above one billion is:");
|
||||
abundantOdd(cast(int)(1e9 + 1), 0, 1, true);
|
||||
}
|
||||
47
Task/Abundant-odd-numbers/Delphi/abundant-odd-numbers.delphi
Normal file
47
Task/Abundant-odd-numbers/Delphi/abundant-odd-numbers.delphi
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
program AbundantOddNumbers;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils;
|
||||
|
||||
function SumProperDivisors(const N: Cardinal): Cardinal;
|
||||
var
|
||||
I, J: Cardinal;
|
||||
begin
|
||||
Result := 1;
|
||||
I := 3;
|
||||
while I < Sqrt(N)+1 do begin
|
||||
if N mod I = 0 then begin
|
||||
J := N div I;
|
||||
Inc(Result, I);
|
||||
if I <> J then Inc(Result, J);
|
||||
end;
|
||||
Inc(I, 2);
|
||||
end;
|
||||
end;
|
||||
|
||||
var
|
||||
C, N: Cardinal;
|
||||
begin
|
||||
N := 1;
|
||||
C := 0;
|
||||
while C < 25 do begin
|
||||
Inc(N, 2);
|
||||
if N < SumProperDivisors(N) then begin
|
||||
Inc(C);
|
||||
WriteLn(Format('%u: %u', [C, N]));
|
||||
end;
|
||||
end;
|
||||
|
||||
while C < 1000 do begin
|
||||
Inc(N, 2);
|
||||
if N < SumProperDivisors(N) then Inc(C);
|
||||
end;
|
||||
WriteLn(Format('The one thousandth abundant odd number is: %u', [N]));
|
||||
|
||||
N := 1000000001;
|
||||
while N >= SumProperDivisors(N) do Inc(N, 2);
|
||||
WriteLn(Format('The first abundant odd number above one billion is: %u', [N]));
|
||||
|
||||
end.
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
// Abundant odd numbers. Nigel Galloway: August 1st., 2021
|
||||
let fN g=Seq.initInfinite(int64>>(+)1L)|>Seq.takeWhile(fun n->n*n<=g)|>Seq.filter(fun n->g%n=0L)|>Seq.sumBy(fun n->let i=g/n in n+(if i=n then 0L else i))
|
||||
let aon n=Seq.initInfinite(int64>>(*)2L>>(+)n)|>Seq.map(fun g->(g,fN g))|>Seq.filter(fun(n,g)->2L*n<g)
|
||||
aon 1L|>Seq.take 25|>Seq.iter(fun(n,g)->printfn "The sum of the divisors of %d is %d" n g)
|
||||
let n,g=aon 1L|>Seq.item 999 in printfn "\nThe 1000th abundant odd number is %d. The sum of it's divisors is %d" n g
|
||||
let n,g=aon 1000000001L|>Seq.head in printfn "\nThe first abundant odd number greater than 1000000000 is %d. The sum of it's divisors is %d" n g
|
||||
25
Task/Abundant-odd-numbers/Factor/abundant-odd-numbers.factor
Normal file
25
Task/Abundant-odd-numbers/Factor/abundant-odd-numbers.factor
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
USING: arrays formatting io kernel lists lists.lazy math
|
||||
math.primes.factors sequences tools.memory.private ;
|
||||
IN: rosetta-code.abundant-odd-numbers
|
||||
|
||||
: σ ( n -- sum ) divisors sum ;
|
||||
: abundant? ( n -- ? ) [ σ ] [ 2 * ] bi > ;
|
||||
: abundant-odds-from ( n -- list )
|
||||
dup even? [ 1 + ] when
|
||||
[ 2 + ] lfrom-by [ abundant? ] lfilter ;
|
||||
|
||||
: first25 ( -- seq ) 25 1 abundant-odds-from ltake list>array ;
|
||||
: 1,000th ( -- n ) 999 1 abundant-odds-from lnth ;
|
||||
: first>10^9 ( -- n ) 1,000,000,001 abundant-odds-from car ;
|
||||
|
||||
GENERIC: show ( obj -- )
|
||||
M: integer show dup σ [ commas ] bi@ "%-6s σ = %s\n" printf ;
|
||||
M: array show [ show ] each ;
|
||||
|
||||
: abundant-odd-numbers-demo ( -- )
|
||||
first25 "First 25 abundant odd numbers:"
|
||||
1,000th "1,000th abundant odd number:"
|
||||
first>10^9 "First abundant odd number > one billion:"
|
||||
[ print show nl ] 2tri@ ;
|
||||
|
||||
MAIN: abundant-odd-numbers-demo
|
||||
47
Task/Abundant-odd-numbers/Fortran/abundant-odd-numbers.f
Normal file
47
Task/Abundant-odd-numbers/Fortran/abundant-odd-numbers.f
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
program main
|
||||
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
|
||||
implicit none
|
||||
integer,parameter :: dp=kind(0.0d0)
|
||||
character(len=*),parameter :: g='(*(g0,1x))'
|
||||
integer :: j, icount
|
||||
integer,allocatable :: list(:)
|
||||
real(kind=dp) :: tally
|
||||
|
||||
write(*,*)'N sum'
|
||||
icount=0 ! number of abundant odd numbers found
|
||||
do j=1,huge(0)-1,2 ! loop through odd numbers for candidates
|
||||
list=divisors(j) ! git list of divisors for current value
|
||||
tally= sum([real(list,kind=dp)]) ! sum divisors
|
||||
if(tally>2*j .and. iand(j,1) /= 0) then ! count an abundant odd number
|
||||
icount=icount+1
|
||||
select case(icount) ! if one of the values targeted print it
|
||||
case(1:25,1000);write(*,g)icount,':',j!, list
|
||||
end select
|
||||
endif
|
||||
if(icount.gt.1000)exit ! quit after last targeted value is found
|
||||
enddo
|
||||
|
||||
do j=1000000001,huge(0),2
|
||||
list=divisors(j)
|
||||
tally= sum([real(list,kind=dp)])
|
||||
if(tally>2*j .and. iand(j,1) /= 0) then
|
||||
write(*,g)'First abundant odd number greater than one billion:',j
|
||||
|
||||
exit
|
||||
endif
|
||||
enddo
|
||||
|
||||
contains
|
||||
|
||||
function divisors(num) result (numbers)
|
||||
!> brute force divisors
|
||||
integer,intent(in) :: num
|
||||
integer :: i
|
||||
integer,allocatable :: numbers(:)
|
||||
numbers=[integer :: ]
|
||||
do i=1 , int(sqrt(real(num)))
|
||||
if (mod(num , i) .eq. 0) numbers=[numbers, i,num/i]
|
||||
enddo
|
||||
end function divisors
|
||||
|
||||
end program main
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
Declare Function SumaDivisores(n As Integer) As Integer
|
||||
|
||||
Dim numimpar As Integer = 1
|
||||
Dim contar As Integer = 0
|
||||
Dim sumaDiv As Integer = 0
|
||||
|
||||
Function SumaDivisores(n As Integer) As Integer
|
||||
' Devuelve la suma de los divisores propios de n
|
||||
Dim suma As Integer = 1
|
||||
Dim As Integer d, otroD
|
||||
|
||||
For d = 2 To Cint(Sqr(n))
|
||||
If n Mod d = 0 Then
|
||||
suma += d
|
||||
otroD = n \ d
|
||||
If otroD <> d Then suma += otroD
|
||||
End If
|
||||
Next d
|
||||
Return suma
|
||||
End Function
|
||||
|
||||
' Encontrar los números requeridos por la tarea:
|
||||
|
||||
' primeros 25 números abundantes impares
|
||||
Print "Los primeros 25 números impares abundantes:"
|
||||
Do While contar < 25
|
||||
sumaDiv = SumaDivisores(numimpar)
|
||||
If sumaDiv > numimpar Then
|
||||
contar += 1
|
||||
Print using "######"; numimpar;
|
||||
Print " suma divisoria adecuada: " & sumaDiv
|
||||
End If
|
||||
numimpar += 2
|
||||
Loop
|
||||
|
||||
' 1000er número impar abundante
|
||||
Do While contar < 1000
|
||||
sumaDiv = SumaDivisores(numimpar)
|
||||
If sumaDiv > numimpar Then contar += 1
|
||||
numimpar += 2
|
||||
Loop
|
||||
Print Chr(10) & "1000º número impar abundante:"
|
||||
Print " " & (numimpar - 2) & " suma divisoria adecuada: " & sumaDiv
|
||||
|
||||
' primer número impar abundante > mil millones (millardo)
|
||||
numimpar = 1000000001
|
||||
Dim encontrado As Boolean = False
|
||||
Do While Not encontrado
|
||||
sumaDiv = SumaDivisores(numimpar)
|
||||
If sumaDiv > numimpar Then
|
||||
encontrado = True
|
||||
Print Chr(10) & "Primer número impar abundante > 1 000 000 000:"
|
||||
Print " " & numimpar & " suma divisoria adecuada: " & sumaDiv
|
||||
End If
|
||||
numimpar += 2
|
||||
Loop
|
||||
End
|
||||
41
Task/Abundant-odd-numbers/Frink/abundant-odd-numbers.frink
Normal file
41
Task/Abundant-odd-numbers/Frink/abundant-odd-numbers.frink
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
isAbundantOdd[n] := sum[allFactors[n, true, false]] > n
|
||||
|
||||
n = 3
|
||||
count = 0
|
||||
|
||||
println["The first 25 abundant odd numbers:"]
|
||||
do
|
||||
{
|
||||
if isAbundantOdd[n]
|
||||
{
|
||||
println["$n: proper divisor sum " + sum[allFactors[n, 1, false]]]
|
||||
count = count + 1
|
||||
}
|
||||
|
||||
n = n + 2
|
||||
} while count < 25
|
||||
|
||||
|
||||
println["\nThe thousandth abundant odd number:"]
|
||||
n = 1
|
||||
count = 0
|
||||
do
|
||||
{
|
||||
n = n + 2
|
||||
|
||||
if isAbundantOdd[n]
|
||||
count = count + 1
|
||||
|
||||
} until count == 1000
|
||||
|
||||
println["$n: proper divisor sum " + sum[allFactors[n, 1, false]]]
|
||||
|
||||
|
||||
println["\nThe first abundant odd number over 1 billion:"]
|
||||
n = 10^9 + 1
|
||||
count = 0
|
||||
do
|
||||
n = n + 2
|
||||
until isAbundantOdd[n]
|
||||
|
||||
println["$n: proper divisor sum " + sum[allFactors[n, 1, false]]]
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
local fn SumOfProperDivisors( n as NSUInteger ) as NSUinteger
|
||||
NSUinteger sum = 1
|
||||
|
||||
cln for (unsigned i = 3, j; i < sqrt(n)+1; i += 2) if (n % i == 0) sum += i + (i == (j = n / i) ? 0 : j);
|
||||
end fn = sum
|
||||
|
||||
NSUinteger n, c
|
||||
cln for (n = 1, c = 0; c < 25; n += 2 ) if ( n < SumOfProperDivisors( n ) ) NSLog( @"%2lu: %lu", ++c, n );
|
||||
|
||||
cln for ( ; c < 1000; n += 2 ) if ( n < SumOfProperDivisors( n ) ) c ++;
|
||||
NSLog( @"\nThe one thousandth abundant odd number is: %lu\n", n )
|
||||
|
||||
cln for ( n = 1000000001 ;; n += 2 ) if ( n < SumOfProperDivisors( n ) ) break;
|
||||
NSLog( @"The first abundant odd number above one billion is: %lu\n", n )
|
||||
|
||||
HandleEvents
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
local fn SumOfProperDivisors( n as NSUInteger ) as NSUinteger
|
||||
NSUinteger i, j, sum = 1
|
||||
|
||||
for i = 3 to sqr(n) step 2
|
||||
if ( n mod i == 0 )
|
||||
sum += i
|
||||
j = n/i
|
||||
if ( i != j )
|
||||
sum += j
|
||||
end if
|
||||
end if
|
||||
next
|
||||
end fn = sum
|
||||
|
||||
NSUinteger n = 1, c
|
||||
|
||||
while ( c < 25 )
|
||||
if ( n < fn SumOfProperDivisors( n ) )
|
||||
NSLog( @"%2lu: %lu", c, n )
|
||||
c++
|
||||
end if
|
||||
n += 2
|
||||
wend
|
||||
|
||||
while ( c < 1000 )
|
||||
if ( n < fn SumOfProperDivisors( n ) ) then c++
|
||||
n += 2
|
||||
wend
|
||||
NSLog( @"\nThe one thousandth abundant odd number is: %lu\n", n )
|
||||
|
||||
n = 1000000001
|
||||
while ( n >= fn SumOfProperDivisors( n ) )
|
||||
n += 2
|
||||
wend
|
||||
NSLog( @"The first abundant odd number above one billion is: %lu\n", n )
|
||||
|
||||
HandleEvents
|
||||
73
Task/Abundant-odd-numbers/Go/abundant-odd-numbers.go
Normal file
73
Task/Abundant-odd-numbers/Go/abundant-odd-numbers.go
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func divisors(n int) []int {
|
||||
divs := []int{1}
|
||||
divs2 := []int{}
|
||||
for i := 2; i*i <= n; i++ {
|
||||
if n%i == 0 {
|
||||
j := n / i
|
||||
divs = append(divs, i)
|
||||
if i != j {
|
||||
divs2 = append(divs2, j)
|
||||
}
|
||||
}
|
||||
}
|
||||
for i := len(divs2) - 1; i >= 0; i-- {
|
||||
divs = append(divs, divs2[i])
|
||||
}
|
||||
return divs
|
||||
}
|
||||
|
||||
func sum(divs []int) int {
|
||||
tot := 0
|
||||
for _, div := range divs {
|
||||
tot += div
|
||||
}
|
||||
return tot
|
||||
}
|
||||
|
||||
func sumStr(divs []int) string {
|
||||
s := ""
|
||||
for _, div := range divs {
|
||||
s += strconv.Itoa(div) + " + "
|
||||
}
|
||||
return s[0 : len(s)-3]
|
||||
}
|
||||
|
||||
func abundantOdd(searchFrom, countFrom, countTo int, printOne bool) int {
|
||||
count := countFrom
|
||||
n := searchFrom
|
||||
for ; count < countTo; n += 2 {
|
||||
divs := divisors(n)
|
||||
if tot := sum(divs); tot > n {
|
||||
count++
|
||||
if printOne && count < countTo {
|
||||
continue
|
||||
}
|
||||
s := sumStr(divs)
|
||||
if !printOne {
|
||||
fmt.Printf("%2d. %5d < %s = %d\n", count, n, s, tot)
|
||||
} else {
|
||||
fmt.Printf("%d < %s = %d\n", n, s, tot)
|
||||
}
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func main() {
|
||||
const max = 25
|
||||
fmt.Println("The first", max, "abundant odd numbers are:")
|
||||
n := abundantOdd(1, 0, 25, false)
|
||||
|
||||
fmt.Println("\nThe one thousandth abundant odd number is:")
|
||||
abundantOdd(n, 25, 1000, true)
|
||||
|
||||
fmt.Println("\nThe first abundant odd number above one billion is:")
|
||||
abundantOdd(1e9+1, 0, 1, true)
|
||||
}
|
||||
65
Task/Abundant-odd-numbers/Groovy/abundant-odd-numbers.groovy
Normal file
65
Task/Abundant-odd-numbers/Groovy/abundant-odd-numbers.groovy
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
class Abundant {
|
||||
static List<Integer> divisors(int n) {
|
||||
List<Integer> divs = new ArrayList<>()
|
||||
divs.add(1)
|
||||
List<Integer> divs2 = new ArrayList<>()
|
||||
|
||||
int i = 2
|
||||
while (i * i < n) {
|
||||
if (n % i == 0) {
|
||||
int j = (int) (n / i)
|
||||
divs.add(i)
|
||||
if (i != j) {
|
||||
divs2.add(j)
|
||||
}
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
Collections.reverse(divs2)
|
||||
divs.addAll(divs2)
|
||||
return divs
|
||||
}
|
||||
|
||||
static int abundantOdd(int searchFrom, int countFrom, int countTo, boolean printOne) {
|
||||
int count = countFrom
|
||||
int n = searchFrom
|
||||
|
||||
while (count < countTo) {
|
||||
List<Integer> divs = divisors(n)
|
||||
int tot = divs.stream().reduce(Integer.&sum).orElse(0)
|
||||
|
||||
if (tot > n) {
|
||||
count++
|
||||
if (!printOne || count >= countTo) {
|
||||
String s = divs.stream()
|
||||
.map(Integer.&toString)
|
||||
.reduce { a, b -> a + " + " + b }
|
||||
.orElse("")
|
||||
if (printOne) {
|
||||
System.out.printf("%d < %s = %d\n", n, s, tot)
|
||||
} else {
|
||||
System.out.printf("%2d. %5d < %s = %d\n", count, n, s, tot)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
n += 2
|
||||
}
|
||||
|
||||
return n
|
||||
}
|
||||
|
||||
static void main(String[] args) {
|
||||
int max = 25
|
||||
|
||||
System.out.printf("The first %d abundant odd numbers are:\n", max)
|
||||
int n = abundantOdd(1, 0, 25, false)
|
||||
|
||||
System.out.println("\nThe one thousandth abundant odd number is:")
|
||||
abundantOdd(n, 25, 1000, true)
|
||||
|
||||
System.out.println("\nThe first abundant odd number above one billion is:")
|
||||
abundantOdd((int) (1e9 + 1), 0, 1, true)
|
||||
}
|
||||
}
|
||||
29
Task/Abundant-odd-numbers/Haskell/abundant-odd-numbers-1.hs
Normal file
29
Task/Abundant-odd-numbers/Haskell/abundant-odd-numbers-1.hs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import Data.List (nub)
|
||||
|
||||
divisorSum :: Integral a => a -> a
|
||||
divisorSum n =
|
||||
sum
|
||||
. map (\i -> sum $ nub [i, n `quot` i])
|
||||
. filter ((== 0) . (n `rem`))
|
||||
$ takeWhile ((<= n) . (^ 2)) [1 ..]
|
||||
|
||||
oddAbundants :: Integral a => a -> [(a, a)]
|
||||
oddAbundants n =
|
||||
[ (i, divisorSum i) | i <- [n ..], odd i, divisorSum i > i * 2 ]
|
||||
|
||||
printAbundant :: (Int, Int) -> IO ()
|
||||
printAbundant (n, s) =
|
||||
putStrLn
|
||||
$ show n
|
||||
++ " with "
|
||||
++ show s
|
||||
++ " as the sum of all proper divisors."
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
putStrLn "The first 25 odd abundant numbers are:"
|
||||
mapM_ printAbundant . take 25 $ oddAbundants 1
|
||||
putStrLn "The 1000th odd abundant number is:"
|
||||
printAbundant $ oddAbundants 1 !! 1000
|
||||
putStrLn "The first odd abundant number above 1000000000 is:"
|
||||
printAbundant . head . oddAbundants $ 10 ^ 9
|
||||
39
Task/Abundant-odd-numbers/Haskell/abundant-odd-numbers-2.hs
Normal file
39
Task/Abundant-odd-numbers/Haskell/abundant-odd-numbers-2.hs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import Data.List (group, sort)
|
||||
import Data.Numbers.Primes
|
||||
|
||||
abundantTuple :: Int -> [(Int, Int)]
|
||||
abundantTuple n =
|
||||
let x = divisorSum n
|
||||
in [(n, x) | n < x]
|
||||
|
||||
divisorSum :: Int -> Int
|
||||
divisorSum = sum . init . divisors
|
||||
|
||||
divisors :: Int -> [Int]
|
||||
divisors =
|
||||
foldr
|
||||
(flip ((<*>) . fmap (*)) . scanl (*) 1)
|
||||
[1]
|
||||
. group
|
||||
. primeFactors
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
putStrLn
|
||||
"First 25 abundant odd numbers with their divisor sums:"
|
||||
mapM_ print $ take 25 ([1, 3 ..] >>= abundantTuple)
|
||||
--
|
||||
putStrLn
|
||||
"\n1000th odd abundant number with its divisor sum:"
|
||||
print $ ([1, 3 ..] >>= abundantTuple) !! 999
|
||||
--
|
||||
putStrLn
|
||||
( "\nFirst odd abundant number over 10^9, "
|
||||
<> "with its divisor sum:"
|
||||
)
|
||||
let billion = 10 ^ 9 :: Int
|
||||
print $
|
||||
head
|
||||
( [1 + billion, 3 + billion ..]
|
||||
>>= abundantTuple
|
||||
)
|
||||
44
Task/Abundant-odd-numbers/Java/abundant-odd-numbers.java
Normal file
44
Task/Abundant-odd-numbers/Java/abundant-odd-numbers.java
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AbundantOddNumbers {
|
||||
private static List<Integer> list = new ArrayList<>();
|
||||
private static List<Integer> result = new ArrayList<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("First 25: ");
|
||||
abundantOdd(1,100000, 25, false);
|
||||
|
||||
System.out.println("\n\nThousandth: ");
|
||||
abundantOdd(1,2500000, 1000, true);
|
||||
|
||||
System.out.println("\n\nFirst over 1bn:");
|
||||
abundantOdd(1000000001, 2147483647, 1, false);
|
||||
}
|
||||
private static void abundantOdd(int start, int finish, int listSize, boolean printOne) {
|
||||
for (int oddNum = start; oddNum < finish; oddNum += 2) {
|
||||
list.clear();
|
||||
for (int toDivide = 1; toDivide < oddNum; toDivide+=2) {
|
||||
if (oddNum % toDivide == 0)
|
||||
list.add(toDivide);
|
||||
}
|
||||
if (sumList(list) > oddNum) {
|
||||
if(!printOne)
|
||||
System.out.printf("%5d <= %5d \n",oddNum, sumList(list) );
|
||||
result.add(oddNum);
|
||||
}
|
||||
if(printOne && result.size() >= listSize)
|
||||
System.out.printf("%5d <= %5d \n",oddNum, sumList(list) );
|
||||
|
||||
if(result.size() >= listSize) break;
|
||||
}
|
||||
}
|
||||
private static int sumList(List list) {
|
||||
int sum = 0;
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
String temp = list.get(i).toString();
|
||||
sum += Integer.parseInt(temp);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
211
Task/Abundant-odd-numbers/JavaScript/abundant-odd-numbers.js
Normal file
211
Task/Abundant-odd-numbers/JavaScript/abundant-odd-numbers.js
Normal file
|
|
@ -0,0 +1,211 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
const main = () => {
|
||||
|
||||
// abundantTuple :: Int -> [(Int, Int)]
|
||||
const abundantTuple = n => {
|
||||
// Either a list containing the tuple of N
|
||||
// and its divisor sum (if n is abundant),
|
||||
// or otherwise an empty list.
|
||||
const x = divisorSum(n);
|
||||
return n < x ? ([
|
||||
Tuple(n)(x)
|
||||
]) : [];
|
||||
};
|
||||
|
||||
// divisorSum :: Int -> Int
|
||||
const divisorSum = n => {
|
||||
// Sum of the divisors of n.
|
||||
const
|
||||
floatRoot = Math.sqrt(n),
|
||||
intRoot = Math.floor(floatRoot),
|
||||
lows = filter(x => 0 === n % x)(
|
||||
enumFromTo(1)(intRoot)
|
||||
);
|
||||
return sum(lows.concat(map(quot(n))(
|
||||
intRoot === floatRoot ? (
|
||||
lows.slice(1, -1)
|
||||
) : lows.slice(1)
|
||||
)));
|
||||
};
|
||||
|
||||
// TEST ---------------------------------------
|
||||
console.log(
|
||||
'First 25 abundant odd numbers, with their divisor sums:'
|
||||
)
|
||||
console.log(unlines(map(showTuple)(
|
||||
take(25)(
|
||||
concatMapGen(abundantTuple)(
|
||||
enumFromThen(1)(3)
|
||||
)
|
||||
)
|
||||
)));
|
||||
console.log(
|
||||
'\n\n1000th abundant odd number, with its divisor sum:'
|
||||
)
|
||||
console.log(showTuple(
|
||||
take(1)(drop(999)(
|
||||
concatMapGen(abundantTuple)(
|
||||
enumFromThen(1)(3)
|
||||
)
|
||||
))[0]
|
||||
))
|
||||
console.log(
|
||||
'\n\nFirst abundant odd number above 10^9, with divisor sum:'
|
||||
)
|
||||
const billion = Math.pow(10, 9);
|
||||
console.log(showTuple(
|
||||
take(1)(
|
||||
concatMapGen(abundantTuple)(
|
||||
enumFromThen(1 + billion)(3 + billion)
|
||||
)
|
||||
)[0]
|
||||
))
|
||||
};
|
||||
|
||||
|
||||
// GENERAL REUSABLE FUNCTIONS -------------------------
|
||||
|
||||
// Tuple (,) :: a -> b -> (a, b)
|
||||
const Tuple = a => b => ({
|
||||
type: 'Tuple',
|
||||
'0': a,
|
||||
'1': b,
|
||||
length: 2
|
||||
});
|
||||
|
||||
// concatMapGen :: (a -> [b]) -> Gen [a] -> Gen [b]
|
||||
const concatMapGen = f =>
|
||||
function*(xs) {
|
||||
let
|
||||
x = xs.next(),
|
||||
v = undefined;
|
||||
while (!x.done) {
|
||||
v = f(x.value);
|
||||
if (0 < v.length) {
|
||||
yield v[0];
|
||||
}
|
||||
x = xs.next();
|
||||
}
|
||||
};
|
||||
|
||||
// drop :: Int -> [a] -> [a]
|
||||
// drop :: Int -> Generator [a] -> Generator [a]
|
||||
// drop :: Int -> String -> String
|
||||
const drop = n => xs =>
|
||||
Infinity > length(xs) ? (
|
||||
xs.slice(n)
|
||||
) : (take(n)(xs), xs);
|
||||
|
||||
// dropAround :: (a -> Bool) -> [a] -> [a]
|
||||
// dropAround :: (Char -> Bool) -> String -> String
|
||||
const dropAround = p => xs => dropWhile(p)(
|
||||
dropWhileEnd(p)(xs)
|
||||
);
|
||||
|
||||
// dropWhile :: (a -> Bool) -> [a] -> [a]
|
||||
// dropWhile :: (Char -> Bool) -> String -> String
|
||||
const dropWhile = p => xs => {
|
||||
const lng = xs.length;
|
||||
return 0 < lng ? xs.slice(
|
||||
until(i => i === lng || !p(xs[i]))(
|
||||
i => 1 + i
|
||||
)(0)
|
||||
) : [];
|
||||
};
|
||||
|
||||
// dropWhileEnd :: (a -> Bool) -> [a] -> [a]
|
||||
// dropWhileEnd :: (Char -> Bool) -> String -> String
|
||||
const dropWhileEnd = p => xs => {
|
||||
let i = xs.length;
|
||||
while (i-- && p(xs[i])) {}
|
||||
return xs.slice(0, i + 1);
|
||||
};
|
||||
|
||||
// enumFromThen :: Int -> Int -> Gen [Int]
|
||||
const enumFromThen = x =>
|
||||
// A non-finite stream of integers,
|
||||
// starting with x and y, and continuing
|
||||
// with the same interval.
|
||||
function*(y) {
|
||||
const d = y - x;
|
||||
let v = y + d;
|
||||
yield x;
|
||||
yield y;
|
||||
while (true) {
|
||||
yield v;
|
||||
v = d + v;
|
||||
}
|
||||
};
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = m => n =>
|
||||
Array.from({
|
||||
length: 1 + n - m
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// filter :: (a -> Bool) -> [a] -> [a]
|
||||
const filter = f => xs => xs.filter(f);
|
||||
|
||||
// Returns Infinity over objects without finite length.
|
||||
// This enables zip and zipWith to choose the shorter
|
||||
// argument when one is non-finite, like cycle, repeat etc
|
||||
|
||||
// length :: [a] -> Int
|
||||
const length = xs =>
|
||||
(Array.isArray(xs) || 'string' === typeof xs) ? (
|
||||
xs.length
|
||||
) : Infinity;
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = f => xs =>
|
||||
(Array.isArray(xs) ? (
|
||||
xs
|
||||
) : xs.split('')).map(f);
|
||||
|
||||
// quot :: Int -> Int -> Int
|
||||
const quot = n => m => Math.floor(n / m);
|
||||
|
||||
// show :: a -> String
|
||||
const show = JSON.stringify;
|
||||
|
||||
// showTuple :: Tuple -> String
|
||||
const showTuple = tpl =>
|
||||
'(' + enumFromTo(0)(tpl.length - 1)
|
||||
.map(x => unQuoted(show(tpl[x])))
|
||||
.join(',') + ')';
|
||||
|
||||
// sum :: [Num] -> Num
|
||||
const sum = xs => xs.reduce((a, x) => a + x, 0);
|
||||
|
||||
// take :: Int -> [a] -> [a]
|
||||
// take :: Int -> String -> String
|
||||
const take = n => xs =>
|
||||
'GeneratorFunction' !== xs.constructor.constructor.name ? (
|
||||
xs.slice(0, n)
|
||||
) : [].concat.apply([], Array.from({
|
||||
length: n
|
||||
}, () => {
|
||||
const x = xs.next();
|
||||
return x.done ? [] : [x.value];
|
||||
}));
|
||||
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs => xs.join('\n');
|
||||
|
||||
// until :: (a -> Bool) -> (a -> a) -> a -> a
|
||||
const until = p => f => x => {
|
||||
let v = x;
|
||||
while (!p(v)) v = f(v);
|
||||
return v;
|
||||
};
|
||||
|
||||
// unQuoted :: String -> String
|
||||
const unQuoted = s =>
|
||||
dropAround(x => 34 === x.codePointAt(0))(
|
||||
s
|
||||
);
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
16
Task/Abundant-odd-numbers/Jq/abundant-odd-numbers-1.jq
Normal file
16
Task/Abundant-odd-numbers/Jq/abundant-odd-numbers-1.jq
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
# The factors, unsorted
|
||||
def factors:
|
||||
. as $num
|
||||
| reduce range(1; 1 + sqrt|floor) as $i
|
||||
([];
|
||||
if ($num % $i) == 0 then
|
||||
($num / $i) as $r
|
||||
| if $i == $r then . + [$i] else . + [$i, $r] end
|
||||
else .
|
||||
end) ;
|
||||
|
||||
def abundant_odd_numbers:
|
||||
range(1; infinite; 2)
|
||||
| (factors | add) as $sum
|
||||
| select($sum > 2*.)
|
||||
| [., $sum] ;
|
||||
6
Task/Abundant-odd-numbers/Jq/abundant-odd-numbers-2.jq
Normal file
6
Task/Abundant-odd-numbers/Jq/abundant-odd-numbers-2.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
( ["n", "sum of divisors"],
|
||||
limit(25; abundant_odd_numbers)),
|
||||
[],
|
||||
(["The 1000th abundant odd number and corresponding sum of divisors:"]
|
||||
+ nth(999; abundant_odd_numbers))
|
||||
| @tsv
|
||||
38
Task/Abundant-odd-numbers/Julia/abundant-odd-numbers.julia
Normal file
38
Task/Abundant-odd-numbers/Julia/abundant-odd-numbers.julia
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
using Primes
|
||||
|
||||
function propfact(n)
|
||||
f = [one(n)]
|
||||
for (p, x) in factor(n)
|
||||
f = reduce(vcat, [f*p^i for i in 1:x], init=f)
|
||||
end
|
||||
pop!(f)
|
||||
sort(f)
|
||||
end
|
||||
|
||||
isabundant(n) = sum(propfact(n)) > n
|
||||
prettyprintfactors(n) = (a = propfact(n); println("$n has proper divisors $a, these sum to $(sum(a))."))
|
||||
|
||||
function oddabundantsfrom(startingint, needed, nprint=0)
|
||||
n = isodd(startingint) ? startingint : startingint + 1
|
||||
count = one(n)
|
||||
while count <= needed
|
||||
if isabundant(n)
|
||||
if nprint == 0
|
||||
prettyprintfactors(n)
|
||||
elseif nprint == count
|
||||
prettyprintfactors(n)
|
||||
end
|
||||
count += 1
|
||||
end
|
||||
n += 2
|
||||
end
|
||||
end
|
||||
|
||||
println("First 25 abundant odd numbers:")
|
||||
oddabundantsfrom(2, 25)
|
||||
|
||||
println("The thousandth abundant odd number:")
|
||||
oddabundantsfrom(2, 1001, 1000)
|
||||
|
||||
println("The first abundant odd number greater than one billion:")
|
||||
oddabundantsfrom(1000000000, 1)
|
||||
58
Task/Abundant-odd-numbers/Kotlin/abundant-odd-numbers.kotlin
Normal file
58
Task/Abundant-odd-numbers/Kotlin/abundant-odd-numbers.kotlin
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
fun divisors(n: Int): List<Int> {
|
||||
val divs = mutableListOf(1)
|
||||
val divs2 = mutableListOf<Int>()
|
||||
|
||||
var i = 2
|
||||
while (i * i <= n) {
|
||||
if (n % i == 0) {
|
||||
val j = n / i
|
||||
divs.add(i)
|
||||
if (i != j) {
|
||||
divs2.add(j)
|
||||
}
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
divs.addAll(divs2.reversed())
|
||||
|
||||
return divs
|
||||
}
|
||||
|
||||
fun abundantOdd(searchFrom: Int, countFrom: Int, countTo: Int, printOne: Boolean): Int {
|
||||
var count = countFrom
|
||||
var n = searchFrom
|
||||
|
||||
while (count < countTo) {
|
||||
val divs = divisors(n)
|
||||
val tot = divs.sum()
|
||||
if (tot > n) {
|
||||
count++
|
||||
if (!printOne || count >= countTo) {
|
||||
val s = divs.joinToString(" + ")
|
||||
if (printOne) {
|
||||
println("$n < $s = $tot")
|
||||
} else {
|
||||
println("%2d. %5d < %s = %d".format(count, n, s, tot))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
n += 2
|
||||
}
|
||||
|
||||
return n
|
||||
}
|
||||
|
||||
|
||||
fun main() {
|
||||
val max = 25
|
||||
println("The first $max abundant odd numbers are:")
|
||||
val n = abundantOdd(1, 0, 25, false)
|
||||
|
||||
println("\nThe one thousandth abundant odd number is:")
|
||||
abundantOdd(n, 25, 1000, true)
|
||||
|
||||
println("\nThe first abundant odd number above one billion is:")
|
||||
abundantOdd((1e9 + 1).toInt(), 0, 1, true)
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
// Note that the following function is for odd numbers only
|
||||
// Use "for (unsigned i = 2; i*i <= n; i++)" for even and odd numbers
|
||||
|
||||
def sum_proper_divisors_of_odd(n: int) -> int:
|
||||
var sum = 1
|
||||
var i = 3
|
||||
let limit = sqrt(n) + 1
|
||||
while i < limit:
|
||||
if n % i == 0:
|
||||
sum += i
|
||||
let j = n / i
|
||||
if i != j:
|
||||
sum += j
|
||||
i += 2
|
||||
return sum
|
||||
|
||||
def abundant_odd_numbers():
|
||||
var n = 1
|
||||
var c = 0
|
||||
print "index: number proper_sum"
|
||||
while c < 25:
|
||||
let s = sum_proper_divisors_of_odd(n)
|
||||
if n < s:
|
||||
c += 1
|
||||
print concat_string([string(c), ": ", string(n), ", ", string(s)], "")
|
||||
n += 2
|
||||
var s = 1
|
||||
while c < 1000:
|
||||
s = sum_proper_divisors_of_odd(n)
|
||||
if n < s:
|
||||
c += 1
|
||||
n += 2
|
||||
print concat_string(["1000: ", string(n), ", ", string(s)], "")
|
||||
n = 999999999
|
||||
while n >= s:
|
||||
n += 2
|
||||
s = sum_proper_divisors_of_odd(n)
|
||||
print concat_string(["The first abundant odd number above one billion is: ", string(n), ", ", string(s)], "")
|
||||
|
||||
|
||||
abundant_odd_numbers()
|
||||
37
Task/Abundant-odd-numbers/Lua/abundant-odd-numbers.lua
Normal file
37
Task/Abundant-odd-numbers/Lua/abundant-odd-numbers.lua
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
-- Return the sum of the proper divisors of x
|
||||
function sumDivs (x)
|
||||
local sum, sqr = 1, math.sqrt(x)
|
||||
for d = 2, sqr do
|
||||
if x % d == 0 then
|
||||
sum = sum + d
|
||||
if d ~= sqr then sum = sum + (x/d) end
|
||||
end
|
||||
end
|
||||
return sum
|
||||
end
|
||||
|
||||
-- Return a table of odd abundant numbers
|
||||
function oddAbundants (mode, limit)
|
||||
local n, count, divlist, divsum = 1, 0, {}
|
||||
repeat
|
||||
n = n + 2
|
||||
divsum = sumDivs(n)
|
||||
if divsum > n then
|
||||
table.insert(divlist, {n, divsum})
|
||||
count = count + 1
|
||||
if mode == "Above" and n > limit then return divlist[#divlist] end
|
||||
end
|
||||
until count == limit
|
||||
if mode == "First" then return divlist end
|
||||
if mode == "Nth" then return divlist[#divlist] end
|
||||
end
|
||||
|
||||
-- Write a result to stdout
|
||||
function showResult (msg, t)
|
||||
print(msg .. ": the proper divisors of " .. t[1] .. " sum to " .. t[2])
|
||||
end
|
||||
|
||||
-- Main procedure
|
||||
for k, v in pairs(oddAbundants("First", 25)) do showResult(k, v) end
|
||||
showResult("1000", oddAbundants("Nth", 1000))
|
||||
showResult("Above 1e6", oddAbundants("Above", 1e6))
|
||||
35
Task/Abundant-odd-numbers/MAD/abundant-odd-numbers.mad
Normal file
35
Task/Abundant-odd-numbers/MAD/abundant-odd-numbers.mad
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
NORMAL MODE IS INTEGER
|
||||
|
||||
INTERNAL FUNCTION(ND)
|
||||
ENTRY TO ODDSUM.
|
||||
SUM = 1
|
||||
SQN = SQRT.(ND)
|
||||
THROUGH CHECK, FOR CN=3, 2, CN.G.SQN
|
||||
TM = ND/CN
|
||||
WHENEVER TM*CN.E.ND
|
||||
SUM = SUM + CN
|
||||
WHENEVER TM.NE.CN, SUM = SUM + TM
|
||||
CHECK END OF CONDITIONAL
|
||||
FUNCTION RETURN SUM
|
||||
END OF FUNCTION
|
||||
|
||||
SEEN = 0
|
||||
NUM = 1
|
||||
|
||||
THROUGH SHOW, FOR NUM=1, 2, SEEN.G.1000
|
||||
WHENEVER NUM.L.ODDSUM.(NUM)
|
||||
SEEN = SEEN + 1
|
||||
WHENEVER SEEN.LE.25 .OR. SEEN.E.1000,
|
||||
0 PRINT FORMAT OUTFMT,SEEN,NUM,ODDSUM.(NUM)
|
||||
SHOW END OF CONDITIONAL
|
||||
|
||||
BILION THROUGH BILION, FOR NUM=NUM, 2,
|
||||
0 NUM.G.1000000000 .AND. NUM.L.ODDSUM.(NUM)
|
||||
|
||||
PRINT FORMAT HUGENO,NUM,ODDSUM.(NUM)
|
||||
|
||||
VECTOR VALUES OUTFMT =
|
||||
0 $4HNO. ,I4,S1,3HIS ,I6,S1,7HDIVSUM ,I6*$
|
||||
VECTOR VALUES HUGENO =
|
||||
0 $25HFIRST ABOVE 1 BILLION IS ,I10,S1,7HDIVSUM ,I10*$
|
||||
END OF PROGRAM
|
||||
39
Task/Abundant-odd-numbers/Maple/abundant-odd-numbers.maple
Normal file
39
Task/Abundant-odd-numbers/Maple/abundant-odd-numbers.maple
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
with(NumberTheory):
|
||||
|
||||
# divisorSum returns the sum of the divisors of x not including x
|
||||
divisorSum := proc(x::integer)
|
||||
return SumOfDivisors(x) - x;
|
||||
end proc:
|
||||
|
||||
|
||||
# abundantNumber returns true if x is an abundant number and false otherwise
|
||||
abundantNumber := proc(x::integer)
|
||||
if (SumOfDivisors(x) > 2*x) then return true
|
||||
else return false end if;
|
||||
end proc:
|
||||
|
||||
count := 0:
|
||||
number := 1:
|
||||
|
||||
cat("First 25 abundant odd numbers");
|
||||
|
||||
while count < 25 do
|
||||
if (abundantNumber(number)) then
|
||||
count += 1:
|
||||
print(cat(count, ": ", number, " sum of divisors ", SumOfDivisors(number), " sum of proper divisors ", divisorSum(number)));
|
||||
else end if;
|
||||
number += 2:
|
||||
end:
|
||||
|
||||
while (count < 1000) do
|
||||
if (abundantNumber(number)) then
|
||||
count += 1:
|
||||
else end if:
|
||||
number += 2:
|
||||
end:
|
||||
|
||||
cat("The 1000th odd abundant number is ", number - 2, ", its sum of divisors is ", SumOfDivisors(number - 2), ", and its sum of proper divisors is ", divisorSum(number - 2));
|
||||
|
||||
for number from 10^9 + 1 by 2 to infinity while not abundantNumber(number) do end:
|
||||
|
||||
cat("First abundant odd number > 10^9 is ", number, ", its sum of divisors is ", SumOfDivisors(number), ", and its sum of proper divisors is ",divisorSum(number));
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
ClearAll[AbundantQ]
|
||||
AbundantQ[n_] := TrueQ[Greater[Total @ Most @ Divisors @ n, n]]
|
||||
res = {};
|
||||
i = 1;
|
||||
While[Length[res] < 25,
|
||||
If[AbundantQ[i],
|
||||
AppendTo[res, {i, Total @ Most @ Divisors @ i}];
|
||||
];
|
||||
i += 2;
|
||||
];
|
||||
res
|
||||
|
||||
res = {};
|
||||
i = 1;
|
||||
While[Length[res] < 1000,
|
||||
If[AbundantQ[i],
|
||||
AppendTo[res, {i, Total @ Most @ Divisors @ i}];
|
||||
];
|
||||
i += 2;
|
||||
];
|
||||
res[[-1]]
|
||||
|
||||
res = {};
|
||||
i = 1000000001;
|
||||
While[Length[res] < 1,
|
||||
If[AbundantQ[i],
|
||||
AppendTo[res, {i, Total @ Most @ Divisors @ i}];
|
||||
];
|
||||
i += 2;
|
||||
];
|
||||
res
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
block([k: 0, n: 1, l: []],
|
||||
while k < 25 do (
|
||||
n: n+2,
|
||||
if divsum(n,-1) > 2 then (
|
||||
k: k+1,
|
||||
l: append(l, [[n,divsum(n)]])
|
||||
)
|
||||
),
|
||||
return(l)
|
||||
);
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
block([k: 0, n: 1],
|
||||
while k < 1000 do (
|
||||
n: n+2,
|
||||
if divsum(n,1) > 2*n then k: k+1
|
||||
),
|
||||
return([n,divsum(n)])
|
||||
);
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
block([n: 5, l: [5], r: divsum(n,-1)],
|
||||
while n < 10^8 do (
|
||||
if not mod(n,3)=0 then (
|
||||
s: divsum(n,-1),
|
||||
if s > r then (r: s, l: append(l, [n]))
|
||||
),
|
||||
n: n+10
|
||||
),
|
||||
return(l)
|
||||
);
|
||||
54
Task/Abundant-odd-numbers/Nim/abundant-odd-numbers.nim
Normal file
54
Task/Abundant-odd-numbers/Nim/abundant-odd-numbers.nim
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
from math import sqrt
|
||||
import strformat
|
||||
|
||||
#---------------------------------------------------------------------------------------------------
|
||||
|
||||
proc sumProperDivisors(n: int): int =
|
||||
## Compute the sum of proper divisors.
|
||||
## "n" is supposed to be odd.
|
||||
result = 1
|
||||
for d in countup(3, sqrt(n.toFloat).int, 2):
|
||||
if n mod d == 0:
|
||||
inc result, d
|
||||
if n div d != d:
|
||||
inc result, n div d
|
||||
|
||||
#---------------------------------------------------------------------------------------------------
|
||||
|
||||
iterator oddAbundant(start: int): tuple[n, s: int] =
|
||||
## Yield the odd abundant numbers and the sum of their proper
|
||||
## divisors greater or equal to "start".
|
||||
var n = start + (start and 1 xor 1) # Start with an odd number.
|
||||
while true:
|
||||
let s = n.sumProperDivisors()
|
||||
if s > n:
|
||||
yield (n, s)
|
||||
inc n, 2
|
||||
|
||||
#---------------------------------------------------------------------------------------------------
|
||||
|
||||
echo "List of 25 first odd abundant numbers."
|
||||
echo "Rank Number Proper divisors sum"
|
||||
echo "---- ----- -------------------"
|
||||
var rank = 0
|
||||
for (n, s) in oddAbundant(1):
|
||||
inc rank
|
||||
echo fmt"{rank:2}: {n:5} {s:5}"
|
||||
if rank == 25:
|
||||
break
|
||||
|
||||
echo ""
|
||||
rank = 0
|
||||
for (n, s) in oddAbundant(1):
|
||||
inc rank
|
||||
if rank == 1000:
|
||||
echo fmt"The 1000th odd abundant number is {n}."
|
||||
echo fmt"The sum of its proper divisors is {s}."
|
||||
break
|
||||
|
||||
echo ""
|
||||
for (n, s) in oddAbundant(1_000_000_000):
|
||||
if n > 1_000_000_000:
|
||||
echo fmt"The first odd abundant number greater than 1000000000 is {n}."
|
||||
echo fmt"The sum of its proper divisors is {s}."
|
||||
break
|
||||
180
Task/Abundant-odd-numbers/Pascal/abundant-odd-numbers.pas
Normal file
180
Task/Abundant-odd-numbers/Pascal/abundant-odd-numbers.pas
Normal file
|
|
@ -0,0 +1,180 @@
|
|||
program AbundantOddNumbers;
|
||||
{$IFDEF FPC}
|
||||
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$CODEALIGN proc=16}{$ALIGN 16}
|
||||
{$ELSE}
|
||||
{$APPTYPE CONSOLE}
|
||||
{$ENDIF}
|
||||
{geeksforgeeks
|
||||
* 1100 = 2^2*5^2*11^1
|
||||
(2^0 + 2^1 + 2^2) * (5^0 + 5^1 + 5^2) * (11^0 + 11^1)
|
||||
(upto the power of factor in factorization i.e. power of 2 and 5 is 2 and 11 is 1.)
|
||||
= (1 + 2 + 2^2) * (1 + 5 + 5^2) * (1 + 11)
|
||||
= 7 * 31 * 12
|
||||
= 2604
|
||||
So, sum of all factors of 1100 = 2604 }
|
||||
uses
|
||||
SysUtils;
|
||||
var
|
||||
//all primes < 2^16=65536
|
||||
primes : array[0..6541] of Word;
|
||||
|
||||
procedure InitPrimes;
|
||||
//sieve of erathotenes
|
||||
var
|
||||
p : array[word] of byte;
|
||||
i,j : NativeInt;
|
||||
Begin
|
||||
fillchar(p,SizeOf(p),#0);
|
||||
p[0] := 1;
|
||||
p[1] := 1;
|
||||
For i := 2 to high(p) do
|
||||
if p[i] = 0 then
|
||||
begin
|
||||
j := i*i;
|
||||
IF j>high(p) then
|
||||
BREAK;
|
||||
while j <= High(p) do
|
||||
begin
|
||||
p[j] := 1;
|
||||
inc(j,i);
|
||||
end;
|
||||
end;
|
||||
j := 0;
|
||||
For i := 2 to high(p) do
|
||||
IF p[i] = 0 then
|
||||
Begin
|
||||
primes[j] := i;
|
||||
inc(j);
|
||||
end;
|
||||
end;
|
||||
|
||||
function PotToString(N: NativeUint):String;
|
||||
var
|
||||
pN,pr,PowerPr,rest : NativeUint;
|
||||
begin
|
||||
pN := 0; //starting at 2;
|
||||
Result := '';
|
||||
repeat
|
||||
pr := primes[pN];
|
||||
rest := N div pr;
|
||||
if rest < pr then
|
||||
BREAK;
|
||||
//same as N MOD PR = 0
|
||||
if rest*pr = N then
|
||||
begin
|
||||
result := result+IntToStr(pr);
|
||||
N := rest;
|
||||
rest := N div pr;
|
||||
PowerPr := 1;
|
||||
while rest*pr = N do
|
||||
begin
|
||||
inc(PowerPr);
|
||||
N := rest;
|
||||
rest := N div pr;
|
||||
end;
|
||||
if PowerPr > 1 then
|
||||
result := result+'^'+IntToStr(PowerPr);
|
||||
if N > 1 then
|
||||
result := result +'*';
|
||||
end;
|
||||
inc(pN);
|
||||
until pN > High(Primes);
|
||||
//is there a last prime factor of N
|
||||
if N <> 1 then
|
||||
result := result+IntToStr(N);
|
||||
end;
|
||||
|
||||
function OutNum(N: NativeUint):string;
|
||||
Begin
|
||||
result := Format('%10u= %s', [N,PotToString(N)]);
|
||||
end;
|
||||
|
||||
function SumProperDivisors(N: NativeUint): NativeUint;
|
||||
var
|
||||
pN,pr,PowerPr,SumOfPower,rest,N0 : NativeUint;
|
||||
begin
|
||||
N0 := N;
|
||||
pN := 0; //starting at 2;
|
||||
Result := 1;
|
||||
repeat
|
||||
pr := primes[pN];
|
||||
rest := N div pr;
|
||||
if rest < pr then
|
||||
BREAK;
|
||||
//same as N MOD PR = 0
|
||||
if rest*pr = N then
|
||||
begin
|
||||
// IF pr=5 then break;
|
||||
// IF pr=7 then break;
|
||||
PowerPr := 1;
|
||||
SumOfPower:= 1;
|
||||
repeat
|
||||
PowerPr := PowerPr*pr;
|
||||
inc(SumOfPower,PowerPr);
|
||||
N := rest;
|
||||
rest := N div pr;
|
||||
until N <> rest*pr;
|
||||
result := result*SumOfPower;
|
||||
end;
|
||||
inc(pN);
|
||||
until pN > High(Primes);
|
||||
//is there a last prime factor of N
|
||||
if N <> 1 then
|
||||
result := result*(N+1);
|
||||
result := result-N0;
|
||||
end;
|
||||
|
||||
var
|
||||
C, N,N0,k: Cardinal;
|
||||
begin
|
||||
InitPrimes;
|
||||
|
||||
k := High(k);
|
||||
N := 1;
|
||||
N0 := N;
|
||||
C := 0;
|
||||
while C < 25 do begin
|
||||
inc(N, 2);
|
||||
if N < SumProperDivisors(N) then begin
|
||||
Inc(C);
|
||||
WriteLn(Format('%5u: %s', [C,OutNum(N)]));
|
||||
IF k > N-N0 then
|
||||
k := N-N0;
|
||||
N0 := N;
|
||||
end;
|
||||
end;
|
||||
Writeln(' Min Delta ',k);
|
||||
writeln;
|
||||
|
||||
while C < 1000 do begin
|
||||
Inc(N, 2);
|
||||
if N < SumProperDivisors(N) then
|
||||
Begin
|
||||
Inc(C);
|
||||
IF k > N-N0 then
|
||||
k := N-N0;
|
||||
N0 := N;
|
||||
end;
|
||||
end;
|
||||
WriteLn(' 1000: ',OutNum(N));
|
||||
Writeln(' Min Delta ',k);
|
||||
writeln;
|
||||
|
||||
while C < 10000 do begin
|
||||
Inc(N, 2);
|
||||
if N < SumProperDivisors(N) then
|
||||
Begin
|
||||
Inc(C);
|
||||
IF k > N-N0 then
|
||||
k := N-N0;
|
||||
N0 := N;
|
||||
end;
|
||||
end;
|
||||
WriteLn('10000: ',OutNum(N));
|
||||
Writeln(' Min Delta ',k);
|
||||
|
||||
N := 1000000001;
|
||||
while N >= SumProperDivisors(N) do
|
||||
Inc(N, 2);
|
||||
WriteLn('The first abundant odd number above one billion is: ',OutNum(N));
|
||||
end.
|
||||
24
Task/Abundant-odd-numbers/Perl/abundant-odd-numbers.pl
Normal file
24
Task/Abundant-odd-numbers/Perl/abundant-odd-numbers.pl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use feature 'say';
|
||||
use ntheory qw/divisor_sum divisors/;
|
||||
|
||||
sub odd_abundants {
|
||||
my($start,$count) = @_;
|
||||
my $n = int(( $start + 2 ) / 3);
|
||||
$n += 1 if 0 == $n % 2;
|
||||
$n *= 3;
|
||||
my @out;
|
||||
while (@out < $count) {
|
||||
$n += 6;
|
||||
next unless (my $ds = divisor_sum($n)) > 2*$n;
|
||||
my @d = divisors($n);
|
||||
push @out, sprintf "%6d: divisor sum: %s = %d", $n, join(' + ', @d[0..@d-2]), $ds-$n;
|
||||
}
|
||||
@out;
|
||||
}
|
||||
|
||||
say 'First 25 abundant odd numbers:';
|
||||
say for odd_abundants(1, 25);
|
||||
say "\nOne thousandth abundant odd number:\n", (odd_abundants(1, 1000))[999];
|
||||
say "\nFirst abundant odd number above one billion:\n", odd_abundants(999_999_999, 1);
|
||||
23
Task/Abundant-odd-numbers/Phix/abundant-odd-numbers.phix
Normal file
23
Task/Abundant-odd-numbers/Phix/abundant-odd-numbers.phix
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">abundantOdd</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">done</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">printAll</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">while</span> <span style="color: #000000;">done</span><span style="color: #0000FF;"><</span><span style="color: #000000;">lim</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">tot</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">tot</span><span style="color: #0000FF;">></span><span style="color: #000000;">n</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">done</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">printAll</span> <span style="color: #008080;">or</span> <span style="color: #000000;">done</span><span style="color: #0000FF;">=</span><span style="color: #000000;">lim</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">ln</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">printAll</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%2d. "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">done</span><span style="color: #0000FF;">):</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
|
||||
<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;">"%s%,6d (proper sum:%,d)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ln</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tot</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">2</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<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;">"\n"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">n</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<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;">"The first 25 abundant odd numbers are:\n"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">abundantOdd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
|
||||
<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;">"The one thousandth abundant odd number is:"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">abundantOdd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
|
||||
<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;">"The first abundant odd number above one billion is:"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">abundantOdd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1e9</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
46
Task/Abundant-odd-numbers/PicoLisp/abundant-odd-numbers.l
Normal file
46
Task/Abundant-odd-numbers/PicoLisp/abundant-odd-numbers.l
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
(de accud (Var Key)
|
||||
(if (assoc Key (val Var))
|
||||
(con @ (inc (cdr @)))
|
||||
(push Var (cons Key 1)) )
|
||||
Key )
|
||||
(de **sum (L)
|
||||
(let S 1
|
||||
(for I (cdr L)
|
||||
(inc 'S (** (car L) I)) )
|
||||
S ) )
|
||||
(de factor-sum (N)
|
||||
(if (=1 N)
|
||||
0
|
||||
(let
|
||||
(R NIL
|
||||
D 2
|
||||
L (1 2 2 . (4 2 4 2 4 6 2 6 .))
|
||||
M (sqrt N)
|
||||
N1 N
|
||||
S 1 )
|
||||
(while (>= M D)
|
||||
(if (=0 (% N1 D))
|
||||
(setq M
|
||||
(sqrt (setq N1 (/ N1 (accud 'R D)))) )
|
||||
(inc 'D (pop 'L)) ) )
|
||||
(accud 'R N1)
|
||||
(for I R
|
||||
(setq S (* S (**sum I))) )
|
||||
(- S N) ) ) )
|
||||
(de factor-list NIL
|
||||
(let (N 1 C 0)
|
||||
(make
|
||||
(loop
|
||||
(when (> (setq @@ (factor-sum N)) N)
|
||||
(link (cons N @@))
|
||||
(inc 'C) )
|
||||
(inc 'N 2)
|
||||
(T (= C 1000)) ) ) ) )
|
||||
(let L (factor-list)
|
||||
(for N 25
|
||||
(println N (++ L)) )
|
||||
(println 1000 (last L))
|
||||
(println
|
||||
'****
|
||||
1000000575
|
||||
(factor-sum 1000000575) ) )
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
void setup() {
|
||||
println("First 25 abundant odd numbers: ");
|
||||
int abundant = 0;
|
||||
int i = 1;
|
||||
while (abundant < 25) {
|
||||
int sigma_sum = sigma(i);
|
||||
if (sigma_sum > 2 * i) {
|
||||
abundant++;
|
||||
println(i + " Sigma sum: " + sigma_sum);
|
||||
}
|
||||
i += 2;
|
||||
}
|
||||
println("Thousandth abundant odd number: ");
|
||||
while (abundant < 1000) {
|
||||
int sigma_sum = sigma(i);
|
||||
if (sigma_sum > 2 * i) {
|
||||
abundant++;
|
||||
if (abundant == 1000) {
|
||||
println(i + " Sigma sum: " + sigma_sum);
|
||||
}
|
||||
}
|
||||
i += 2;
|
||||
}
|
||||
println("First abundant odd number greater than 10^9: ");
|
||||
i = int(pow(10, 9)) + 1;
|
||||
while (!(sigma(i) > 2 * i)) {
|
||||
i += 2;
|
||||
}
|
||||
println(i + " Sigma sum: " + sigma(i));
|
||||
}
|
||||
|
||||
int sigma(int n) {
|
||||
int sum = 0;
|
||||
for (int i = 1; i < sqrt(n); i++) {
|
||||
if (n % i == 0) {
|
||||
sum += i + n / i;
|
||||
}
|
||||
}
|
||||
if (sqrt(n) % 1 == 0) {
|
||||
sum += sqrt(n);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
NewList l_sum.i()
|
||||
|
||||
|
||||
Procedure.i sum_proper_divisors(n.i)
|
||||
Define.i sum, i=3, j
|
||||
Shared l_sum()
|
||||
AddElement(l_sum())
|
||||
l_sum()=1
|
||||
While i<Sqr(n)+1
|
||||
If n%i=0
|
||||
sum+i
|
||||
AddElement(l_sum())
|
||||
l_sum()=i
|
||||
j=n/i
|
||||
If i<>j
|
||||
sum+j
|
||||
AddElement(l_sum())
|
||||
l_sum()=j
|
||||
EndIf
|
||||
EndIf
|
||||
i+2
|
||||
Wend
|
||||
ProcedureReturn sum+1
|
||||
EndProcedure
|
||||
|
||||
|
||||
If OpenConsole("Abundant_odd_numbers")
|
||||
Define.i n, c, s
|
||||
|
||||
n=1
|
||||
c=0
|
||||
While c<25
|
||||
ClearList(l_sum())
|
||||
s=sum_proper_divisors(n)
|
||||
If n<s
|
||||
SortList(l_sum(),#PB_Sort_Ascending)
|
||||
c+1
|
||||
Print(RSet(Str(c),3)+": "+RSet(Str(n),6)+" -> "+RSet(Str(s),6))
|
||||
ForEach l_sum()
|
||||
If ListIndex(l_sum())=0
|
||||
Print(" = ")
|
||||
Else
|
||||
Print("+")
|
||||
EndIf
|
||||
Print(Str(l_sum()))
|
||||
Next
|
||||
PrintN("")
|
||||
EndIf
|
||||
n+2
|
||||
Wend
|
||||
|
||||
n-2
|
||||
While c<1000
|
||||
s=sum_proper_divisors(n+2)
|
||||
c+Bool(n<s)
|
||||
n+2
|
||||
Wend
|
||||
PrintN(~"\nThe one thousandth abundant odd number is: "+Str(n)+
|
||||
~"\n\tand the proper divisor sum is: "+Str(s))
|
||||
|
||||
n=1000000001-2
|
||||
Repeat
|
||||
n+2
|
||||
s=sum_proper_divisors(n)
|
||||
Until n<s
|
||||
PrintN("The first abundant odd number above one billion is: "+Str(n)+
|
||||
~"\n\tand the proper divisor sum is: "+Str(s))
|
||||
|
||||
Input()
|
||||
EndIf
|
||||
46
Task/Abundant-odd-numbers/Python/abundant-odd-numbers-1.py
Normal file
46
Task/Abundant-odd-numbers/Python/abundant-odd-numbers-1.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#!/usr/bin/python
|
||||
# Abundant odd numbers - Python
|
||||
|
||||
oddNumber = 1
|
||||
aCount = 0
|
||||
dSum = 0
|
||||
|
||||
from math import sqrt
|
||||
|
||||
def divisorSum(n):
|
||||
sum = 1
|
||||
i = int(sqrt(n)+1)
|
||||
|
||||
for d in range (2, i):
|
||||
if n % d == 0:
|
||||
sum += d
|
||||
otherD = n // d
|
||||
if otherD != d:
|
||||
sum += otherD
|
||||
return sum
|
||||
|
||||
print ("The first 25 abundant odd numbers:")
|
||||
while aCount < 25:
|
||||
dSum = divisorSum(oddNumber )
|
||||
if dSum > oddNumber :
|
||||
aCount += 1
|
||||
print("{0:5} proper divisor sum: {1}". format(oddNumber ,dSum ))
|
||||
oddNumber += 2
|
||||
|
||||
while aCount < 1000:
|
||||
dSum = divisorSum(oddNumber )
|
||||
if dSum > oddNumber :
|
||||
aCount += 1
|
||||
oddNumber += 2
|
||||
print ("\n1000th abundant odd number:")
|
||||
print (" ",(oddNumber - 2)," proper divisor sum: ",dSum)
|
||||
|
||||
oddNumber = 1000000001
|
||||
found = False
|
||||
while not found :
|
||||
dSum = divisorSum(oddNumber )
|
||||
if dSum > oddNumber :
|
||||
found = True
|
||||
print ("\nFirst abundant odd number > 1 000 000 000:")
|
||||
print (" ",oddNumber," proper divisor sum: ",dSum)
|
||||
oddNumber += 2
|
||||
101
Task/Abundant-odd-numbers/Python/abundant-odd-numbers-2.py
Normal file
101
Task/Abundant-odd-numbers/Python/abundant-odd-numbers-2.py
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
'''Odd abundant numbers'''
|
||||
|
||||
from math import sqrt
|
||||
from itertools import chain, count, islice
|
||||
|
||||
|
||||
# abundantTuple :: Int -> [(Int, Int)]
|
||||
def abundantTuple(n):
|
||||
'''A list containing the tuple of N and its divisor
|
||||
sum, if n is abundant, or an empty list.
|
||||
'''
|
||||
x = divisorSum(n)
|
||||
return [(n, x)] if n < x else []
|
||||
|
||||
|
||||
# divisorSum :: Int -> Int
|
||||
def divisorSum(n):
|
||||
'''Sum of the divisors of n.'''
|
||||
floatRoot = sqrt(n)
|
||||
intRoot = int(floatRoot)
|
||||
blnSquare = intRoot == floatRoot
|
||||
lows = [x for x in range(1, 1 + intRoot) if 0 == n % x]
|
||||
return sum(lows + [
|
||||
n // x for x in (
|
||||
lows[1:-1] if blnSquare else lows[1:]
|
||||
)
|
||||
])
|
||||
|
||||
|
||||
# TEST ----------------------------------------------------
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''Subsets of abundant odd numbers.'''
|
||||
|
||||
# First 25.
|
||||
print('First 25 abundant odd numbers with their divisor sums:')
|
||||
for x in take(25)(
|
||||
concatMap(abundantTuple)(
|
||||
enumFromThen(1)(3)
|
||||
)
|
||||
):
|
||||
print(x)
|
||||
|
||||
# The 1000th.
|
||||
print('\n1000th odd abundant number with its divisor sum:')
|
||||
print(
|
||||
take(1000)(
|
||||
concatMap(abundantTuple)(
|
||||
enumFromThen(1)(3)
|
||||
)
|
||||
)[-1]
|
||||
)
|
||||
|
||||
# First over 10^9.
|
||||
print('\nFirst odd abundant number over 10^9, with its divisor sum:')
|
||||
billion = (10 ** 9)
|
||||
print(
|
||||
take(1)(
|
||||
concatMap(abundantTuple)(
|
||||
enumFromThen(1 + billion)(3 + billion)
|
||||
)
|
||||
)[0]
|
||||
)
|
||||
|
||||
|
||||
# GENERAL FUNCTIONS ---------------------------------------
|
||||
|
||||
# enumFromThen :: Int -> Int -> [Int]
|
||||
def enumFromThen(m):
|
||||
'''A non-finite stream of integers
|
||||
starting at m, and continuing
|
||||
at the interval between m and n.
|
||||
'''
|
||||
return lambda n: count(m, n - m)
|
||||
|
||||
|
||||
# concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
def concatMap(f):
|
||||
'''A concatenated list over which a function f
|
||||
has been mapped.
|
||||
The list monad can be derived by using an (a -> [b])
|
||||
function which wraps its output in a list (using an
|
||||
empty list to represent computational failure).
|
||||
'''
|
||||
return lambda xs: (
|
||||
chain.from_iterable(map(f, xs))
|
||||
)
|
||||
|
||||
|
||||
# take :: Int -> [a] -> [a]
|
||||
def take(n):
|
||||
'''The prefix of xs of length n,
|
||||
or xs itself if n > length xs.
|
||||
'''
|
||||
return lambda xs: (
|
||||
list(islice(xs, n))
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
4
Task/Abundant-odd-numbers/Q/abundant-odd-numbers-1.q
Normal file
4
Task/Abundant-odd-numbers/Q/abundant-odd-numbers-1.q
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
s:{c where 0=x mod c:1+til x div 2} / proper divisors
|
||||
sd:sum s@ / sum of proper divisors
|
||||
abundant:{x<sd x}
|
||||
Filter:{y where x each y}
|
||||
12
Task/Abundant-odd-numbers/Q/abundant-odd-numbers-2.q
Normal file
12
Task/Abundant-odd-numbers/Q/abundant-odd-numbers-2.q
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
q)count A:Filter[abundant] 1+2*til 260000 / a batch of abundant odd numbers; 1000+ is enough
|
||||
1054
|
||||
|
||||
q)1 sd'\25#A / first 25 abundant odd numbers, and the sum of their divisors
|
||||
945 1575 2205 2835 3465 4095 4725 5355 5775 5985 6435 6615 6825 7245 7425 7875 8085 8415 8505 8925 9135 9555 9765 10395 11025
|
||||
975 1649 2241 2973 4023 4641 5195 5877 6129 6495 6669 7065 7063 7731 7455 8349 8331 8433 8967 8931 9585 9597 10203 12645 11946
|
||||
|
||||
q)1 sd\A 999 / 1000th abundant odd number and the sum of its divisors
|
||||
492975 519361
|
||||
|
||||
q)1 sd\(not abundant@)(2+)/1000000000-1 / first abundant odd number above 1,000,000,000 and its divisors
|
||||
1000000575 1083561009
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
[ 0 swap factors witheach + ] is sigmasum ( n --> n )
|
||||
|
||||
0 -1 [ 2 +
|
||||
dup sigmasum
|
||||
over 2 * over < iff
|
||||
[ over echo sp
|
||||
echo cr
|
||||
dip 1+ ]
|
||||
else drop
|
||||
over 25 = until ]
|
||||
2drop
|
||||
cr
|
||||
0 -1
|
||||
[ 2 + dup sigmasum
|
||||
over 2 * > if [ dip 1+ ]
|
||||
over 1000 = until ]
|
||||
dup echo sp sigmasum echo cr
|
||||
drop
|
||||
cr
|
||||
999999999
|
||||
[ 2 + dup sigmasum
|
||||
over 2 * > until ]
|
||||
dup echo sp sigmasum echo cr
|
||||
41
Task/Abundant-odd-numbers/R/abundant-odd-numbers.r
Normal file
41
Task/Abundant-odd-numbers/R/abundant-odd-numbers.r
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
# Abundant Odd Numbers
|
||||
|
||||
find_div_sum <- function(x){
|
||||
# Finds sigma: the sum of the divisors (not including the number itself) of an odd number
|
||||
if (x < 16) return(0)
|
||||
root <- sqrt(x)
|
||||
vec <- as.vector(1)
|
||||
for (i in seq.int(3, root - 1, by = 2)){
|
||||
if(x %% i == 0){
|
||||
vec <- c(vec, i, x/i)
|
||||
}
|
||||
}
|
||||
if (root == trunc(root)) vec = c(vec, root)
|
||||
return(sum(vec))
|
||||
}
|
||||
|
||||
get_n_abun <- function(index = 1, total = 25, print_all = TRUE){
|
||||
# Finds a total of 'total' abundant odds starting with 'index', with print option
|
||||
n <- 1
|
||||
while(n <= total){
|
||||
my_sum <- find_div_sum(index)
|
||||
if (my_sum > index){
|
||||
if(print_all) cat(index, "..... sigma is", my_sum, "\n")
|
||||
n <- n + 1
|
||||
}
|
||||
index <- index + 2
|
||||
}
|
||||
if(!print_all) cat(index - 2, "..... sigma is", my_sum, "\n")
|
||||
}
|
||||
|
||||
# Get first 25
|
||||
cat("The first 25 abundants are")
|
||||
get_n_abun()
|
||||
|
||||
# Get the 1000th
|
||||
cat("The 1000th odd abundant is")
|
||||
get_n_abun(total = 1000, print_all = F)
|
||||
|
||||
# Get the first after 1e9
|
||||
cat("First odd abundant after 1e9 is")
|
||||
get_n_abun(index = 1e9 + 1, total = 1, print_all = F)
|
||||
40
Task/Abundant-odd-numbers/REXX/abundant-odd-numbers.rexx
Normal file
40
Task/Abundant-odd-numbers/REXX/abundant-odd-numbers.rexx
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/*REXX pgm displays abundant odd numbers: 1st 25, one─thousandth, first > 1 billion. */
|
||||
parse arg Nlow Nuno Novr . /*obtain optional arguments from the CL*/
|
||||
if Nlow=='' | Nlow=="," then Nlow= 25 /*Not specified? Then use the default.*/
|
||||
if Nuno=='' | Nuno=="," then Nuno= 1000 /* " " " " " " */
|
||||
if Novr=='' | Novr=="," then Novr= 1000000000 /* " " " " " " */
|
||||
numeric digits max(9, length(Novr) ) /*ensure enough decimal digits for // */
|
||||
@= 'odd abundant number' /*variable for annotating the output. */
|
||||
#= 0 /*count of odd abundant numbers so far.*/
|
||||
do j=3 by 2 until #>=Nlow; $= sigO(j) /*get the sigma for an odd integer. */
|
||||
if $<=j then iterate /*sigma ≤ J ? Then ignore it. */
|
||||
#= # + 1 /*bump the counter for abundant odd #'s*/
|
||||
say rt(th(#)) @ 'is:'rt(commas(j), 8) rt("sigma=") rt(commas($), 9)
|
||||
end /*j*/
|
||||
say
|
||||
#= 0 /*count of odd abundant numbers so far.*/
|
||||
do j=3 by 2; $= sigO(j) /*get the sigma for an odd integer. */
|
||||
if $<=j then iterate /*sigma ≤ J ? Then ignore it. */
|
||||
#= # + 1 /*bump the counter for abundant odd #'s*/
|
||||
if #<Nuno then iterate /*Odd abundant# count<Nuno? Then skip.*/
|
||||
say rt(th(#)) @ 'is:'rt(commas(j), 8) rt("sigma=") rt(commas($), 9)
|
||||
leave /*we're finished displaying NUNOth num.*/
|
||||
end /*j*/
|
||||
say
|
||||
do j=1+Novr%2*2 by 2; $= sigO(j) /*get sigma for an odd integer > Novr. */
|
||||
if $<=j then iterate /*sigma ≤ J ? Then ignore it. */
|
||||
say rt(th(1)) @ 'over' commas(Novr) "is: " commas(j) rt('sigma=') commas($)
|
||||
leave /*we're finished displaying NOVRth num.*/
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
commas:parse arg _; do c_=length(_)-3 to 1 by -3; _=insert(',', _, c_); end; return _
|
||||
rt: procedure; parse arg #,len; if len=='' then len= 20; return right(#, len)
|
||||
th: parse arg th; return th||word('th st nd rd',1+(th//10)*(th//100%10\==1)*(th//10<4))
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
sigO: parse arg x; s= 1 /*sigma for odd integers. ___*/
|
||||
do k=3 by 2 while k*k<x /*divide by all odd integers up to √ x */
|
||||
if x//k==0 then s= s + k + x%k /*add the two divisors to (sigma) sum. */
|
||||
end /*k*/ /* ___*/
|
||||
if k*k==x then return s + k /*Was X a square? If so, add √ x */
|
||||
return s /*return (sigma) sum of the divisors. */
|
||||
14
Task/Abundant-odd-numbers/Racket/abundant-odd-numbers.rkt
Normal file
14
Task/Abundant-odd-numbers/Racket/abundant-odd-numbers.rkt
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#lang racket
|
||||
|
||||
(require math/number-theory
|
||||
racket/generator)
|
||||
|
||||
(define (make-generator start)
|
||||
(in-generator
|
||||
(for ([n (in-naturals start)] #:when (odd? n))
|
||||
(define divisor-sum (- (apply + (divisors n)) n))
|
||||
(when (> divisor-sum n) (yield (list n divisor-sum))))))
|
||||
|
||||
(for/list ([i (in-range 25)] [x (make-generator 0)]) x) ; Task 1
|
||||
(for/last ([i (in-range 1000)] [x (make-generator 0)]) x) ; Task 2
|
||||
(for/first ([x (make-generator (add1 (inexact->exact 1e9)))]) x) ; Task 3
|
||||
27
Task/Abundant-odd-numbers/Raku/abundant-odd-numbers.raku
Normal file
27
Task/Abundant-odd-numbers/Raku/abundant-odd-numbers.raku
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
sub odd-abundant (\x) {
|
||||
my @l = x.is-prime ?? 1 !! flat
|
||||
1, (3 .. x.sqrt.floor).map: -> \d {
|
||||
next unless d +& 1;
|
||||
my \y = x div d;
|
||||
next if y * d !== x;
|
||||
d !== y ?? (d, y) !! d
|
||||
};
|
||||
@l.sum > x ?? @l.sort !! Empty;
|
||||
}
|
||||
|
||||
sub odd-abundants (Int :$start-at is copy) {
|
||||
$start-at = ( $start-at + 2 ) div 3;
|
||||
$start-at += $start-at %% 2;
|
||||
$start-at *= 3;
|
||||
($start-at, *+6 ... *).hyper.map: {
|
||||
next unless my $oa = cache .&odd-abundant;
|
||||
sprintf "%6d: divisor sum: {$oa.join: ' + '} = {$oa.sum}", $_
|
||||
}
|
||||
}
|
||||
|
||||
put 'First 25 abundant odd numbers:';
|
||||
.put for odd-abundants( :start-at(1) )[^25];
|
||||
|
||||
put "\nOne thousandth abundant odd number:\n" ~ odd-abundants( :start-at(1) )[999] ~
|
||||
|
||||
"\n\nFirst abundant odd number above one billion:\n" ~ odd-abundants( :start-at(1_000_000_000) ).head;
|
||||
66
Task/Abundant-odd-numbers/Ring/abundant-odd-numbers.ring
Normal file
66
Task/Abundant-odd-numbers/Ring/abundant-odd-numbers.ring
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
#Project: Anbundant odd numbers
|
||||
|
||||
max = 100000000
|
||||
limit = 25
|
||||
nr = 0
|
||||
m = 1
|
||||
check = 0
|
||||
index = 0
|
||||
see "working..." + nl
|
||||
see "wait for done..." + nl
|
||||
while true
|
||||
check = 0
|
||||
if m%2 = 1
|
||||
nice(m)
|
||||
ok
|
||||
if check = 1
|
||||
nr = nr + 1
|
||||
ok
|
||||
if nr = max
|
||||
exit
|
||||
ok
|
||||
m = m + 1
|
||||
end
|
||||
see "done..." + nl
|
||||
|
||||
func nice(n)
|
||||
check = 0
|
||||
nArray = []
|
||||
for i = 1 to n - 1
|
||||
if n % i = 0
|
||||
add(nArray,i)
|
||||
ok
|
||||
next
|
||||
sum = 0
|
||||
for p = 1 to len(nArray)
|
||||
sum = sum + nArray[p]
|
||||
next
|
||||
if sum > n
|
||||
check = 1
|
||||
index = index + 1
|
||||
if index < limit + 1
|
||||
showArray(n,nArray,sum,index)
|
||||
ok
|
||||
if index = 100
|
||||
see "One thousandth abundant odd number:" + nl
|
||||
showArray2(n,nArray,sum,index)
|
||||
ok
|
||||
if index = 100000000
|
||||
see "First abundant odd number above one billion:" + nl
|
||||
showArray2(n,nArray,sum,index)
|
||||
ok
|
||||
ok
|
||||
|
||||
func showArray(n,nArray,sum,index)
|
||||
see "" + index + ". " + string(n) + ": divisor sum: "
|
||||
for m = 1 to len(nArray)
|
||||
if m < len(nArray)
|
||||
see string(nArray[m]) + " + "
|
||||
else
|
||||
see string(nArray[m]) + " = " + string(sum) + nl + nl
|
||||
ok
|
||||
next
|
||||
|
||||
func showArray2(n,nArray,sum,index)
|
||||
see "" + index + ". " + string(n) + ": divisor sum: " +
|
||||
see string(nArray[m]) + " = " + string(sum) + nl + nl
|
||||
25
Task/Abundant-odd-numbers/Ruby/abundant-odd-numbers.rb
Normal file
25
Task/Abundant-odd-numbers/Ruby/abundant-odd-numbers.rb
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
require "prime"
|
||||
|
||||
class Integer
|
||||
def proper_divisors
|
||||
return [] if self == 1
|
||||
primes = prime_division.flat_map{|prime, freq| [prime] * freq}
|
||||
(1...primes.size).each_with_object([1]) do |n, res|
|
||||
primes.combination(n).map{|combi| res << combi.inject(:*)}
|
||||
end.flatten.uniq
|
||||
end
|
||||
end
|
||||
|
||||
def generator_odd_abundants(from=1)
|
||||
from += 1 if from.even?
|
||||
Enumerator.new do |y|
|
||||
from.step(nil, 2) do |n|
|
||||
sum = n.proper_divisors.sum
|
||||
y << [n, sum] if sum > n
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
generator_odd_abundants.take(25).each{|n, sum| puts "#{n} with sum #{sum}" }
|
||||
puts "\n%d with sum %#d" % generator_odd_abundants.take(1000).last
|
||||
puts "\n%d with sum %#d" % generator_odd_abundants(1_000_000_000).next
|
||||
54
Task/Abundant-odd-numbers/Rust/abundant-odd-numbers.rust
Normal file
54
Task/Abundant-odd-numbers/Rust/abundant-odd-numbers.rust
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
fn divisors(n: u64) -> Vec<u64> {
|
||||
let mut divs = vec![1];
|
||||
let mut divs2 = Vec::new();
|
||||
|
||||
for i in (2..).take_while(|x| x * x <= n).filter(|x| n % x == 0) {
|
||||
divs.push(i);
|
||||
let j = n / i;
|
||||
if i != j {
|
||||
divs2.push(j);
|
||||
}
|
||||
}
|
||||
divs.extend(divs2.iter().rev());
|
||||
|
||||
divs
|
||||
}
|
||||
|
||||
fn sum_string(v: Vec<u64>) -> String {
|
||||
v[1..]
|
||||
.iter()
|
||||
.fold(format!("{}", v[0]), |s, i| format!("{} + {}", s, i))
|
||||
}
|
||||
|
||||
fn abundant_odd(search_from: u64, count_from: u64, count_to: u64, print_one: bool) -> u64 {
|
||||
let mut count = count_from;
|
||||
for n in (search_from..).step_by(2) {
|
||||
let divs = divisors(n);
|
||||
let total: u64 = divs.iter().sum();
|
||||
if total > n {
|
||||
count += 1;
|
||||
let s = sum_string(divs);
|
||||
if !print_one {
|
||||
println!("{}. {} < {} = {}", count, n, s, total);
|
||||
} else if count == count_to {
|
||||
println!("{} < {} = {}", n, s, total);
|
||||
}
|
||||
}
|
||||
if count == count_to {
|
||||
break;
|
||||
}
|
||||
}
|
||||
count_to
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let max = 25;
|
||||
println!("The first {} abundant odd numbers are:", max);
|
||||
let n = abundant_odd(1, 0, max, false);
|
||||
|
||||
println!("The one thousandth abundant odd number is:");
|
||||
abundant_odd(n, 25, 1000, true);
|
||||
|
||||
println!("The first abundant odd number above one billion is:");
|
||||
abundant_odd(1e9 as u64 + 1, 0, 1, true);
|
||||
}
|
||||
60
Task/Abundant-odd-numbers/Scala/abundant-odd-numbers.scala
Normal file
60
Task/Abundant-odd-numbers/Scala/abundant-odd-numbers.scala
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import scala.collection.mutable.ListBuffer
|
||||
|
||||
object Abundant {
|
||||
def divisors(n: Int): ListBuffer[Int] = {
|
||||
val divs = new ListBuffer[Int]
|
||||
divs.append(1)
|
||||
|
||||
val divs2 = new ListBuffer[Int]
|
||||
var i = 2
|
||||
|
||||
while (i * i <= n) {
|
||||
if (n % i == 0) {
|
||||
val j = n / i
|
||||
divs.append(i)
|
||||
if (i != j) {
|
||||
divs2.append(j)
|
||||
}
|
||||
}
|
||||
i += 1
|
||||
}
|
||||
|
||||
divs.appendAll(divs2.reverse)
|
||||
divs
|
||||
}
|
||||
|
||||
def abundantOdd(searchFrom: Int, countFrom: Int, countTo: Int, printOne: Boolean): Int = {
|
||||
var count = countFrom
|
||||
var n = searchFrom
|
||||
while (count < countTo) {
|
||||
val divs = divisors(n)
|
||||
val tot = divs.sum
|
||||
if (tot > n) {
|
||||
count += 1
|
||||
if (!printOne || !(count < countTo)) {
|
||||
val s = divs.map(a => a.toString).mkString(" + ")
|
||||
if (printOne) {
|
||||
printf("%d < %s = %d\n", n, s, tot)
|
||||
} else {
|
||||
printf("%2d. %5d < %s = %d\n", count, n, s, tot)
|
||||
}
|
||||
}
|
||||
}
|
||||
n += 2
|
||||
}
|
||||
|
||||
n
|
||||
}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
val max = 25
|
||||
printf("The first %d abundant odd numbers are:\n", max)
|
||||
val n = abundantOdd(1, 0, max, printOne = false)
|
||||
|
||||
printf("\nThe one thousandth abundant odd number is:\n")
|
||||
abundantOdd(n, 25, 1000, printOne = true)
|
||||
|
||||
printf("\nThe first abundant odd number above one billion is:\n")
|
||||
abundantOdd((1e9 + 1).intValue(), 0, 1, printOne = true)
|
||||
}
|
||||
}
|
||||
27
Task/Abundant-odd-numbers/Sidef/abundant-odd-numbers.sidef
Normal file
27
Task/Abundant-odd-numbers/Sidef/abundant-odd-numbers.sidef
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
func is_abundant(n) {
|
||||
n.sigma > 2*n
|
||||
}
|
||||
|
||||
func odd_abundants (from = 1) {
|
||||
from = (from + 2)//3
|
||||
from += (from%2 - 1)
|
||||
3*from .. Inf `by` 6 -> lazy.grep(is_abundant)
|
||||
}
|
||||
|
||||
say " Index | Number | proper divisor sum"
|
||||
const sep = "-------+-------------+-------------------\n"
|
||||
const fstr = "%6s | %11s | %11s\n"
|
||||
|
||||
print sep
|
||||
|
||||
odd_abundants().first(25).each_kv {|k,n|
|
||||
printf(fstr, k+1, n, n.sigma-n)
|
||||
}
|
||||
|
||||
with (odd_abundants().nth(1000)) {|n|
|
||||
printf(sep + fstr, 1000, n, n.sigma-n)
|
||||
}
|
||||
|
||||
with(odd_abundants(1e9).first) {|n|
|
||||
printf(sep + fstr, '***', n, n.sigma-n)
|
||||
}
|
||||
47
Task/Abundant-odd-numbers/Smalltalk/abundant-odd-numbers.st
Normal file
47
Task/Abundant-odd-numbers/Smalltalk/abundant-odd-numbers.st
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
divisors :=
|
||||
[:nr |
|
||||
|divs|
|
||||
|
||||
divs := Set with:1.
|
||||
"no need to check even factors; we are only looking for odd nrs"
|
||||
3 to:(nr integerSqrt) by:2 do:[:d | nr % d = 0 ifTrue:[divs add:d; add:(nr / d)]].
|
||||
divs.
|
||||
].
|
||||
|
||||
isAbundant := [:nr | (divisors value:nr) sum > nr].
|
||||
|
||||
"from set of abdundant numbers >= minNr, print nMinPrint-th to nMaxPrint-th"
|
||||
printNAbundant :=
|
||||
[:minNr :nMinPrint :nMaxPrint |
|
||||
|count divs|
|
||||
|
||||
count := 0.
|
||||
minNr to:Infinity positive doWithExit:[:nr :exit |
|
||||
(nr odd and:[isAbundant value:nr]) ifTrue:[
|
||||
count := count + 1.
|
||||
count >= nMinPrint ifTrue:[
|
||||
divs := divisors value:nr.
|
||||
Transcript
|
||||
show:nr; show:' -> '; show:divs asArray sorted;
|
||||
show:' sum = '; showCR:divs sum.
|
||||
].
|
||||
count >= nMaxPrint ifTrue: exit
|
||||
]
|
||||
]
|
||||
].
|
||||
|
||||
Transcript showCR:'first 25 odd abundant numbers:'.
|
||||
"from set of abdundant numbers >= 3, print 1st to 25th"
|
||||
printNAbundant value:3 value:1 value:25.
|
||||
|
||||
Transcript cr; showCR:'first odd abundant number above 1000000000:'.
|
||||
"from set of abdundant numbers >= 1000000000, print 1st to 1st"
|
||||
printNAbundant value:1000000000 value:1 value:1.
|
||||
|
||||
Transcript cr; showCR:'first odd abundant number above 1000000000000:'.
|
||||
"from set of abdundant numbers >= 1000000000, print 1st to 1st"
|
||||
printNAbundant value:1000000000000 value:1 value:1.
|
||||
|
||||
Transcript cr; showCR:'the 1000th odd abundant number is:'.
|
||||
"from set of abdundant numbers>= 3, print 1000th to 1000th"
|
||||
printNAbundant value:3 value:1000 value:1000.
|
||||
36
Task/Abundant-odd-numbers/Swift/abundant-odd-numbers.swift
Normal file
36
Task/Abundant-odd-numbers/Swift/abundant-odd-numbers.swift
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
extension BinaryInteger {
|
||||
@inlinable
|
||||
public func factors(sorted: Bool = true) -> [Self] {
|
||||
let maxN = Self(Double(self).squareRoot())
|
||||
var res = Set<Self>()
|
||||
|
||||
for factor in stride(from: 1, through: maxN, by: 1) where self % factor == 0 {
|
||||
res.insert(factor)
|
||||
res.insert(self / factor)
|
||||
}
|
||||
|
||||
return sorted ? res.sorted() : Array(res)
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isAbundant<T: BinaryInteger>(n: T) -> (Bool, [T]) {
|
||||
let divs = n.factors().dropLast()
|
||||
|
||||
return (divs.reduce(0, +) > n, Array(divs))
|
||||
}
|
||||
|
||||
let oddAbundant = (0...).lazy.filter({ $0 & 1 == 1 }).map({ ($0, isAbundant(n: $0)) }).filter({ $1.0 })
|
||||
|
||||
for (n, (_, factors)) in oddAbundant.prefix(25) {
|
||||
print("n: \(n); sigma: \(factors.reduce(0, +))")
|
||||
}
|
||||
|
||||
let (bigA, (_, bigFactors)) =
|
||||
(1_000_000_000...)
|
||||
.lazy
|
||||
.filter({ $0 & 1 == 1 })
|
||||
.map({ ($0, isAbundant(n: $0)) })
|
||||
.first(where: { $1.0 })!
|
||||
|
||||
print("first odd abundant number over 1 billion: \(bigA), sigma: \(bigFactors.reduce(0, +))")
|
||||
68
Task/Abundant-odd-numbers/V-(Vlang)/abundant-odd-numbers.v
Normal file
68
Task/Abundant-odd-numbers/V-(Vlang)/abundant-odd-numbers.v
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
fn divisors(n i64) []i64 {
|
||||
mut divs := [i64(1)]
|
||||
mut divs2 := []i64{}
|
||||
for i := 2; i*i <= n; i++ {
|
||||
if n%i == 0 {
|
||||
j := n / i
|
||||
divs << i
|
||||
if i != j {
|
||||
divs2 << j
|
||||
}
|
||||
}
|
||||
}
|
||||
for i := divs2.len - 1; i >= 0; i-- {
|
||||
divs << divs2[i]
|
||||
}
|
||||
return divs
|
||||
}
|
||||
|
||||
fn sum(divs []i64) i64 {
|
||||
mut tot := i64(0)
|
||||
for div in divs {
|
||||
tot += div
|
||||
}
|
||||
return tot
|
||||
}
|
||||
|
||||
fn sum_str(divs []i64) string {
|
||||
mut s := ""
|
||||
for div in divs {
|
||||
s += "${u8(div)} + "
|
||||
}
|
||||
return s[0..s.len-3]
|
||||
}
|
||||
|
||||
fn abundant_odd(search_from i64, count_from int, count_to int, print_one bool) i64 {
|
||||
mut count := count_from
|
||||
mut n := search_from
|
||||
for ; count < count_to; n += 2 {
|
||||
divs := divisors(n)
|
||||
tot := sum(divs)
|
||||
if tot > n {
|
||||
count++
|
||||
if print_one && count < count_to {
|
||||
continue
|
||||
}
|
||||
s := sum_str(divs)
|
||||
if !print_one {
|
||||
println("${count:2}. ${n:5} < $s = $tot")
|
||||
} else {
|
||||
println("$n < $s = $tot")
|
||||
}
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
const max = 25
|
||||
|
||||
fn main() {
|
||||
println("The first $max abundant odd numbers are:")
|
||||
n := abundant_odd(1, 0, 25, false)
|
||||
|
||||
println("\nThe one thousandth abundant odd number is:")
|
||||
abundant_odd(n, 25, 1000, true)
|
||||
|
||||
println("\nThe first abundant odd number above one billion is:")
|
||||
abundant_odd(1_000_000_001, 0, 1, true)
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
Module AbundantOddNumbers
|
||||
' find some abundant odd numbers - numbers where the sum of the proper
|
||||
' divisors is bigger than the number
|
||||
' itself
|
||||
|
||||
' returns the sum of the proper divisors of n
|
||||
Private Function divisorSum(n As Integer) As Integer
|
||||
Dim sum As Integer = 1
|
||||
For d As Integer = 2 To Math.Round(Math.Sqrt(n))
|
||||
If n Mod d = 0 Then
|
||||
sum += d
|
||||
Dim otherD As Integer = n \ d
|
||||
IF otherD <> d Then
|
||||
sum += otherD
|
||||
End If
|
||||
End If
|
||||
Next d
|
||||
Return sum
|
||||
End Function
|
||||
|
||||
' find numbers required by the task
|
||||
Public Sub Main(args() As String)
|
||||
' first 25 odd abundant numbers
|
||||
Dim oddNumber As Integer = 1
|
||||
Dim aCount As Integer = 0
|
||||
Dim dSum As Integer = 0
|
||||
Console.Out.WriteLine("The first 25 abundant odd numbers:")
|
||||
Do While aCount < 25
|
||||
dSum = divisorSum(oddNumber)
|
||||
If dSum > oddNumber Then
|
||||
aCount += 1
|
||||
Console.Out.WriteLine(oddNumber.ToString.PadLeft(6) & " proper divisor sum: " & dSum)
|
||||
End If
|
||||
oddNumber += 2
|
||||
Loop
|
||||
' 1000th odd abundant number
|
||||
Do While aCount < 1000
|
||||
dSum = divisorSum(oddNumber)
|
||||
If dSum > oddNumber Then
|
||||
aCount += 1
|
||||
End If
|
||||
oddNumber += 2
|
||||
Loop
|
||||
Console.Out.WriteLine("1000th abundant odd number:")
|
||||
Console.Out.WriteLine(" " & (oddNumber - 2) & " proper divisor sum: " & dSum)
|
||||
' first odd abundant number > one billion
|
||||
oddNumber = 1000000001
|
||||
Dim found As Boolean = False
|
||||
Do While Not found
|
||||
dSum = divisorSum(oddNumber)
|
||||
If dSum > oddNumber Then
|
||||
found = True
|
||||
Console.Out.WriteLine("First abundant odd number > 1 000 000 000:")
|
||||
Console.Out.WriteLine(" " & oddNumber & " proper divisor sum: " & dSum)
|
||||
End If
|
||||
oddNumber += 2
|
||||
Loop
|
||||
End Sub
|
||||
End Module
|
||||
36
Task/Abundant-odd-numbers/Wren/abundant-odd-numbers.wren
Normal file
36
Task/Abundant-odd-numbers/Wren/abundant-odd-numbers.wren
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import "/fmt" for Fmt
|
||||
import "/math" for Int, Nums
|
||||
|
||||
var sumStr = Fn.new { |divs| divs.reduce("") { |acc, div| acc + "%(div) + " }[0...-3] }
|
||||
|
||||
var abundantOdd = Fn.new { |searchFrom, countFrom, countTo, printOne|
|
||||
var count = countFrom
|
||||
var n = searchFrom
|
||||
while (count < countTo) {
|
||||
var divs = Int.properDivisors(n)
|
||||
var tot = Nums.sum(divs)
|
||||
if (tot > n) {
|
||||
count = count + 1
|
||||
if (!printOne || count >= countTo) {
|
||||
var s = sumStr.call(divs)
|
||||
if (!printOne) {
|
||||
System.print("%(Fmt.d(2, count)). %(Fmt.d(5, n)) < %(s) = %(tot)")
|
||||
} else {
|
||||
System.print("%(n) < %(s) = %(tot)")
|
||||
}
|
||||
}
|
||||
}
|
||||
n = n + 2
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
var MAX = 25
|
||||
System.print("The first %(MAX) abundant odd numbers are:")
|
||||
var n = abundantOdd.call(1, 0, 25, false)
|
||||
|
||||
System.print("\nThe one thousandth abundant odd number is:")
|
||||
abundantOdd.call(n, 25, 1000, true)
|
||||
|
||||
System.print("\nThe first abundant odd number above one billion is:")
|
||||
abundantOdd.call(1e9+1, 0, 1, true)
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
.model tiny
|
||||
.code
|
||||
.486
|
||||
org 100h
|
||||
;ebp=counter, edi=Num, ebx=Div, esi=Sum
|
||||
start: xor ebp, ebp ;odd abundant number counter:= 0
|
||||
mov edi, 3 ;Num:= 3
|
||||
ab10: mov ebx, 3 ;Div:= 3
|
||||
mov esi, 1 ;Sum:= 1
|
||||
ab20: mov eax, edi ;Quot:= Num/Div
|
||||
cdq ;edx:= 0
|
||||
div ebx ;eax(q):edx(r):= edx:eax/ebx
|
||||
cmp ebx, eax ;if Div > Quot then quit loop
|
||||
jge ab50
|
||||
test edx, edx ;if remainder = 0 then
|
||||
jne ab30
|
||||
add esi, ebx ; Sum:= Sum + Div
|
||||
cmp ebx, eax ; if Div # Quot then
|
||||
je ab30
|
||||
add esi, eax ; Sum:= Sum + Quot
|
||||
ab30: add ebx, 2 ;Div:= Div+2 (only check odd Nums)
|
||||
jmp ab20 ;loop
|
||||
ab50:
|
||||
cmp esi, edi ;if Sum > Num then
|
||||
jle ab80
|
||||
inc ebp ; counter:= counter+1
|
||||
cmp ebp, 25 ; if counter<=25 or counter>=1000 then
|
||||
jle ab60
|
||||
cmp ebp, 1000
|
||||
jl ab80
|
||||
ab60: mov eax, edi ; print Num
|
||||
call numout
|
||||
mov al, ' ' ; print spaces
|
||||
int 29h
|
||||
int 29h
|
||||
mov eax, esi ; print Sum
|
||||
call numout
|
||||
mov al, 0Dh ; carriage return
|
||||
int 29h
|
||||
mov al, 0Ah ; line feed
|
||||
int 29h
|
||||
cmp ebp, 1000 ; if counter = 1000 then
|
||||
jne ab65
|
||||
mov edi, 1000000001-2 ; Num:= 1,000,000,001 - 2
|
||||
ab65: cmp edi, 1000000000 ; if Num > 1,000,000,000 then exit
|
||||
jg ab90
|
||||
ab80: add edi, 2 ;Num:= Num+2 (only check odd Nums)
|
||||
jmp ab10 ;loop
|
||||
ab90: ret
|
||||
|
||||
;Print signed integer in eax with commas, e.g: 12,345,010
|
||||
numout: xor ecx, ecx ;digit counter:= 0
|
||||
no00: cdq ;edx:= 0
|
||||
mov ebx, 10 ;Num:= Num/10
|
||||
div ebx ;eax(q):edx(r):= edx:eax/ebx
|
||||
push edx ;remainder = least significant digit
|
||||
inc ecx ;count digit
|
||||
test eax, eax ;if Num # 0 then NumOut(Num)
|
||||
je no20
|
||||
call no00
|
||||
no20: pop eax ;print digit + '0'
|
||||
add al, '0'
|
||||
int 29h
|
||||
dec ecx ;un-count digit
|
||||
je no30 ;if counter # 0 and
|
||||
mov al, cl ; if remainder(counter/3) = 0 then
|
||||
aam 3
|
||||
jne no30
|
||||
mov al, ',' ; print ','
|
||||
int 29h
|
||||
no30: ret
|
||||
end start
|
||||
25
Task/Abundant-odd-numbers/XPL0/abundant-odd-numbers.xpl0
Normal file
25
Task/Abundant-odd-numbers/XPL0/abundant-odd-numbers.xpl0
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
int Cnt, Num, Div, Sum, Quot;
|
||||
[Cnt:= 0;
|
||||
Num:= 3; \find odd abundant numbers
|
||||
loop [Div:= 1;
|
||||
Sum:= 0;
|
||||
loop [Quot:= Num/Div;
|
||||
if Div > Quot then quit;
|
||||
if rem(0) = 0 then
|
||||
[Sum:= Sum + Div;
|
||||
if Div # Quot then Sum:= Sum + Quot;
|
||||
];
|
||||
Div:= Div+2;
|
||||
];
|
||||
if Sum > 2*Num then
|
||||
[Cnt:= Cnt+1;
|
||||
if Cnt<=25 or Cnt>=1000 then
|
||||
[IntOut(0, Num); ChOut(0, 9);
|
||||
IntOut(0, Sum); CrLf(0);
|
||||
if Cnt = 1000 then Num:= 1_000_000_001 - 2;
|
||||
if Num > 1_000_000_000 then quit;
|
||||
];
|
||||
];
|
||||
Num:= Num+2;
|
||||
];
|
||||
]
|
||||
14
Task/Abundant-odd-numbers/Zkl/abundant-odd-numbers-1.zkl
Normal file
14
Task/Abundant-odd-numbers/Zkl/abundant-odd-numbers-1.zkl
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
fcn oddAbundants(startAt=3){ //--> iterator
|
||||
Walker.zero().tweak(fcn(rn){
|
||||
n:=rn.value;
|
||||
while(True){
|
||||
sum:=0;
|
||||
foreach d in ([3.. n.toFloat().sqrt().toInt(), 2]){
|
||||
if( (y:=n/d) *d != n) continue;
|
||||
sum += ((y==d) and y or y+d)
|
||||
}
|
||||
if(sum>n){ rn.set(n+2); return(n) }
|
||||
n+=2;
|
||||
}
|
||||
}.fp(Ref(startAt.isOdd and startAt or startAt+1)))
|
||||
}
|
||||
12
Task/Abundant-odd-numbers/Zkl/abundant-odd-numbers-2.zkl
Normal file
12
Task/Abundant-odd-numbers/Zkl/abundant-odd-numbers-2.zkl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fcn oddDivisors(n){ // -->sorted List
|
||||
[3.. n.toFloat().sqrt().toInt(), 2].pump(List(1),'wrap(d){
|
||||
if( (y:=n/d) *d != n) return(Void.Skip);
|
||||
if (y==d) y else T(y,d)
|
||||
}).flatten().sort()
|
||||
}
|
||||
fcn printOAs(oas){ // List | int
|
||||
foreach n in (vm.arglist.flatten()){
|
||||
ds:=oddDivisors(n);
|
||||
println("%6,d: %6,d = %s".fmt(n, ds.sum(0), ds.sort().concat(" + ")))
|
||||
}
|
||||
}
|
||||
10
Task/Abundant-odd-numbers/Zkl/abundant-odd-numbers-3.zkl
Normal file
10
Task/Abundant-odd-numbers/Zkl/abundant-odd-numbers-3.zkl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
oaw:=oddAbundants();
|
||||
|
||||
println("First 25 abundant odd numbers:");
|
||||
oaw.walk(25) : printOAs(_);
|
||||
|
||||
println("\nThe one thousandth abundant odd number is:");
|
||||
oaw.drop(1_000 - 25).value : printOAs(_);
|
||||
|
||||
println("\nThe first abundant odd number above one billion is:");
|
||||
printOAs(oddAbundants(1_000_000_000).next());
|
||||
Loading…
Add table
Add a link
Reference in a new issue