Additive primes: Difference between revisions
Content added Content deleted
Langurmonkey (talk | contribs) |
Deadmarshal (talk | contribs) (Added Modula-3) |
||
Line 2,570: | Line 2,570: | ||
WriteLn(); |
WriteLn(); |
||
END AdditivePrimes.</syntaxhighlight> |
END AdditivePrimes.</syntaxhighlight> |
||
{{out}} |
|||
<pre> 2 3 5 7 11 23 29 41 43 47 |
|||
61 67 83 89 101 113 131 137 139 151 |
|||
157 173 179 191 193 197 199 223 227 229 |
|||
241 263 269 281 283 311 313 317 331 337 |
|||
353 359 373 379 397 401 409 421 443 449 |
|||
461 463 467 487 |
|||
There are 54 additive primes less than 500.</pre> |
|||
=={{header|Modula-3}}== |
|||
{{trans|Modula-2}} |
|||
<syntaxhighlight lang="modula3">MODULE AdditivePrimes EXPORTS Main; |
|||
IMPORT SIO,Fmt; |
|||
CONST |
|||
Max = 500; |
|||
VAR |
|||
Count:CARDINAL := 0; |
|||
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 J: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; |
|||
INC(J,I) |
|||
END |
|||
END |
|||
END; |
|||
END Sieve; |
|||
BEGIN |
|||
Sieve(); |
|||
FOR N := 2 TO Max DO |
|||
IF Prime[N] AND Prime[DigitSum(N)] THEN |
|||
SIO.PutText(Fmt.F("%4s",Fmt.Int(N))); |
|||
INC(Count); |
|||
IF Count MOD 10 = 0 THEN SIO.Nl() END |
|||
END |
|||
END; |
|||
SIO.PutText(Fmt.F("\nThere are %s additive primes less than %s.\n", |
|||
Fmt.Int(Count),Fmt.Int(Max))); |
|||
END AdditivePrimes. |
|||
</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> 2 3 5 7 11 23 29 41 43 47 |
<pre> 2 3 5 7 11 23 29 41 43 47 |