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.


MODULE AdditivePrimes;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;
 
CONST
    Max = 500;
 
VAR
    N: CARDINAL;
    Count: CARDINAL;
    Prime: ARRAY [2..Max] OF BOOLEAN;
 
PROCEDURE DigitSum(n: CARDINAL): CARDINAL;
BEGIN
    IF n < 10 THEN 
        RETURN n;
    ELSE 
        RETURN (n MOD 10) + DigitSum(n DIV 10);
    END;
END DigitSum;
 
PROCEDURE Sieve;
VAR i, j, max2: CARDINAL;
BEGIN
    FOR i := 2 TO Max DO
        Prime[i] := TRUE;
    END;
 
    FOR i := 2 TO Max DIV 2 DO
        IF Prime[i] THEN;
            j := i*2;
            WHILE j <= Max DO 
                Prime[j] := FALSE;
                j := j + i;
            END;
        END;
    END;
END Sieve;
 
BEGIN
    Count := 0;
    Sieve();
    FOR N := 2 TO Max DO
        IF Prime[N] AND Prime[DigitSum(N)] THEN
            WriteCard(N, 4);
            Count := Count + 1;
            IF Count MOD 10 = 0 THEN WriteLn(); END;
        END;
    END;
    WriteLn();
    WriteString('There are '); WriteCard(Count,0);
    WriteString(' additive primes less than '); WriteCard(Max,0);
    WriteString('.');
    WriteLn();
END AdditivePrimes.