Definitions
In mathematics, additive primes are prime numbers for which the sum of their decimal digits are also primes.



Task
Write a program to determine (and show here) all additive primes less than 500.

Optionally, show the number of additive primes.



Also see

   the OEIS entry:   A046704 additive primes.
   the prime-numbers entry:   additive primes.
   the geeks for geeks entry: additive prime number.
   the prime-numbers fandom: additive primes.


 
/* ARM assembly Raspberry PI  */
/*  program additivePrime.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 MAXI,      500
 
 
/*********************************/
/* Initialized data              */
/*********************************/
.data
szMessResult:        .asciz "Prime  : @ \n"
szMessCounter:      .asciz "Number found : @ \n" 
szCarriageReturn:   .asciz "\n"
 
/*********************************/
/* UnInitialized data            */
/*********************************/
.bss  
sZoneConv:                  .skip 24
TablePrime:                 .skip   4 * MAXI 
/*********************************/
/*  code section                 */
/*********************************/
.text
.global main 
main:                               @ entry of program 
 
    bl createArrayPrime
    mov r5,r0                       @ prime number
 
    ldr r4,iAdrTablePrime           @ address prime table
    mov r10,#0                      @ init counter
    mov r6,#0                       @ indice
1:
    ldr r2,[r4,r6,lsl #2]           @ load prime
    mov r9,r2                       @ save prime
    mov r7,#0                       @ init digit sum
    mov r1,#10                      @ divisor
2:                                  @ begin loop
    mov r0,r2                       @ dividende
    bl division
    add r7,r7,r3                    @ add digit to digit sum
    cmp r2,#0                       @ quotient null ?
    bne 2b                          @ no -> comppute other digit
 
    mov r8,#1                       @ indice
4:                                  @ prime search loop 
    cmp r8,r5                       @ maxi ?
    bge 5f                          @ yes
    ldr r0,[r4,r8,lsl #2]           @ load prime
    cmp r0,r7                       @ prime >= digit sum ?
    addlt r8,r8,#1                  @ no -> increment indice
    blt 4b                          @ and loop
    bne 5f                          @ > 
    mov r0,r9                       @ equal
    bl displayPrime
    add r10,r10,#1                  @ increment counter
5:
    add r6,r6,#1                    @ increment first indice
    cmp r6,r5                       @ maxi ?
    blt 1b                          @ and loop
 
    mov r0,r10                      @ number counter
    ldr r1,iAdrsZoneConv
    bl conversion10                 @ call décimal conversion
    ldr r0,iAdrszMessCounter
    ldr r1,iAdrsZoneConv            @ insert conversion in message
    bl strInsertAtCharInc
    bl affichageMess                @ display message
 
100:                                @ standard end of the program 
    mov r0, #0                      @ return code
    mov r7, #EXIT                   @ request to exit program
    svc #0                          @ perform the system call
iAdrszCarriageReturn:     .int szCarriageReturn
iAdrszMessResult:         .int szMessResult
iAdrszMessCounter:        .int szMessCounter
iAdrTablePrime:           .int TablePrime
/******************************************************************/
/*      créate prime array                                       */ 
/******************************************************************/
createArrayPrime:
    push {r1-r7,lr}          @ save registers
    ldr r4,iAdrTablePrime    @ address prime table
    mov r0,#1                      
    str r0,[r4]              @ store 1 in array
    mov r0,#2
    str r0,[r4,#4]           @ store 2 in array
    mov r0,#3
    str r0,[r4,#8]           @ store 3 in array
    mov r5,#3                @ prine counter 
    mov r7,#5                @ first number to test
1:
    mov r6,#1                @ indice
2:
    mov r0,r7                @ dividende
    ldr r1,[r4,r6,lsl #2]    @ load divisor
    bl division
    cmp r3,#0                @ null remainder ?
    beq 3f                   @ yes -> end loop
    cmp r2,r1                @ quotient < divisor
    strlt r7,[r4,r5,lsl #2]  @ dividende is prime store in array
    addlt r5,r5,#1           @ increment counter
    blt 3f                   @ and end loop
    add r6,r6,#1             @ else increment indice
    cmp r6,r5                @ maxi ?
    blt 2b                   @ no -> loop
3:
    add r7,#2                @ other odd number
    cmp r7,#MAXI             @ maxi ?
    blt 1b                   @ no -> loop
    mov r0,r5                @ return counter
100:
    pop {r1-r7,pc}
/******************************************************************/
/*      Display prime table elements                                */ 
/******************************************************************/
/* r0 contains the prime */
displayPrime:
    push {r1,lr}                    @ save registers
    ldr r1,iAdrsZoneConv
    bl conversion10                 @ call décimal conversion
    ldr r0,iAdrszMessResult
    ldr r1,iAdrsZoneConv            @ insert conversion in message
    bl strInsertAtCharInc
    bl affichageMess                @ display message
100:
    pop {r1,pc}
 
iAdrsZoneConv:                   .int sZoneConv  
/***************************************************/
/*      ROUTINES INCLUDE                           */
/***************************************************/
.include "../affichage.inc"
 
 