Additive primes: Difference between revisions

Added Modula-3
(Add Miranda)
(Added Modula-3)
(3 intermediate revisions by 2 users not shown)
Line 2,361:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .isPrime = ffn(.i) .i == 2 or .i > 2 and
not any ffn(.x) .i div .x, pseries 2 .. .i ^/ 2
 
val .sumDigits = ffn(.i) fold ffn{+}, s2n string .i
 
writeln "Additive primes less than 500:"
Line 2,372:
for .i in [2] ~ series(3..500, 2) {
if .isPrime(.i) and .isPrime(.sumDigits(.i)) {
write $"\{.i:3;} "
.count += 1
if .count div 10: writeln()
Line 2,378:
}
 
writeln $"\n\n\{.count;} additive primes found.\n"
</syntaxhighlight>
 
Line 2,570:
WriteLn();
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}}
<pre> 2 3 5 7 11 23 29 41 43 47
Line 4,024 ⟶ 4,081:
 
0 OK, 0:176</pre>
 
=={{header|Uiua}}==
{{works with|Uiua|0.10.0-dev.1}}
<syntaxhighlight lang="Uiua">
[] # list of primes to be populated
↘2⇡500 # candidates (starting at 2)
 
# Take the first remaining candidate, which will be prime, save it,
# then remove every candidate that it divides. Repeat until none left.
⍢(▽≠0◿⊃⊢(.↘1)⟜(⊂⊢)|>0⧻)
# Tidy up.
⇌◌
 
# Build sum of digits of each.
≡(/+≡⋕°⋕)...
# Mask out those that result in non-primes.
⊏⊚±⬚0⊏⊗
# Return values and length.
⧻.
</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]
54
</pre>
=={{header|V (Vlang)}}==
{{trans|go}}
40

edits