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.


 
 
INTEGER PROC FNMathGetSquareRootI( INTEGER xI )
 INTEGER squareRootI = 0
 IF ( xI > 0 )
  WHILE( ( squareRootI * squareRootI ) <= xI )
   squareRootI = squareRootI + 1
  ENDWHILE
  squareRootI = squareRootI - 1
 ENDIF
 RETURN( squareRootI )
END
//
INTEGER PROC FNMathCheckIntegerIsPrimeB( INTEGER nI )
 INTEGER I = 0
 INTEGER primeB = FALSE
 INTEGER stopB = FALSE
 INTEGER restI = 0
 INTEGER limitI = 0
 primeB = FALSE
 IF ( nI <= 0 )
  RETURN( FALSE )
 ENDIF
 IF ( nI == 1 )
  RETURN( FALSE )
 ENDIF
 IF ( nI == 2 )
  RETURN( TRUE )
 ENDIF
 IF ( nI == 3 )
  RETURN( TRUE )
 ENDIF
 IF ( nI MOD 2 == 0 )
  RETURN( FALSE )
 ENDIF
 IF ( ( nI MOD 6 ) <> 1 ) AND ( ( nI MOD 6 ) <> 5 )
  RETURN( FALSE )
 ENDIF
 limitI = FNMathGetSquareRootI( nI )
 I = 3
 REPEAT
  restI = ( nI MOD I )
  IF ( restI == 0 )
   primeB = FALSE
   stopB = TRUE
  ENDIF
  IF ( I > limitI )
   primeB = TRUE
   stopB = TRUE
  ENDIF
  I = I + 2
 UNTIL ( stopB )
 RETURN( primeB )
END
//
INTEGER PROC FNMathCheckIntegerDigitSumI( INTEGER J )
 STRING s[255] = Str( J )
 STRING cS[255] = ""
 INTEGER minI = 1
 INTEGER maxI = Length( s )
 INTEGER I = 0
 INTEGER K = 0
 FOR I = minI TO maxI
  cS = s[ I ]
  K = K + Val( cS )
 ENDFOR
 RETURN( K )
END
//
INTEGER PROC FNMathCheckIntegerDigitSumIsPrimeB( INTEGER I )
 INTEGER J = FNMathCheckIntegerDigitSumI( I )
 INTEGER B = FNMathCheckIntegerIsPrimeB( J )
 RETURN( B )
END
//
INTEGER PROC FNMathGetPrimeAdditiveAllToBufferB( INTEGER maxI, INTEGER bufferI )
 INTEGER B = FALSE
 INTEGER B1 = FALSE
 INTEGER B2 = FALSE
 INTEGER B3 = FALSE
 INTEGER minI = 2
 INTEGER I = 0
 FOR I = minI TO maxI
  B1 = FNMathCheckIntegerIsPrimeB( I )
  B2 = FNMathCheckIntegerDigitSumIsPrimeB( I )
  B3 = B1 AND B2
  IF ( B3 )
   PushPosition()
   PushBlock()
   GotoBufferId( bufferI )
   AddLine( Str( I ) )
   PopBlock()
   PopPosition()
  ENDIF
 ENDFOR
 B = TRUE
 RETURN( B )
END
//
PROC Main()
 STRING s1[255] = "500" // change this
 INTEGER bufferI = 0
 PushPosition()
 bufferI = CreateTempBuffer()
 PopPosition()
 IF ( NOT ( Ask( " = ", s1, _EDIT_HISTORY_ ) ) AND ( Length( s1 ) > 0 ) ) RETURN() ENDIF
 Message( FNMathGetPrimeAdditiveAllToBufferB( Val( s1 ), bufferI ) ) // gives e.g. TRUE
 GotoBufferId( bufferI )
END
 
 