Additive primes: Difference between revisions

m
→‎{{header|ALGOL 68}}: Avoid line wrap
m (→‎{{header|ALGOL 68}}: Avoid line wrap)
(4 intermediate revisions by 3 users not shown)
Line 428:
FI
OD;
print( ( newline, "Found ", whole( additive count, 0 ), " additive primes below ", whole( UPB prime + 1, 0 ), newline ) );
print( ( " additive primes below ", whole( UPB prime + 1, 0 ), newline ) )
END</syntaxhighlight>
{{out}}
Line 2,361 ⟶ 2,362:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">val .isPrime = fn(.i) .i == 2 or .i > 2 and not any fn(.x) .i div .x, pseries 2 .. .i ^/ 2{
.i == 2 or .i > 2 and
not any fn(.x) { .i div .x }, pseries 2 .. .i ^/ 2
}
 
val .sumDigits = fn(.i) { fold ffn{+}, s2n string .i }
 
writeln "Additive primes less than 500:"
Line 2,569 ⟶ 2,573:
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 2,621 ⟶ 2,682:
 
Number of additive primes found: 54</pre>
 
=={{header|Oberon-07}}==
{{Trans|Modula-3}}
<syntaxhighlight lang="modula2">
MODULE AdditivePrimes;
 
IMPORT
Out;
 
CONST
Max = 500;
 
VAR
Count, n :INTEGER;
Prime :ARRAY Max + 1 OF BOOLEAN;
 
PROCEDURE DigitSum( n :INTEGER ):INTEGER;
VAR result :INTEGER;
BEGIN
result := 0;
IF n < 10 THEN result := n
ELSE result := ( n MOD 10 ) + DigitSum( n DIV 10 )
END
RETURN result
END DigitSum;
 
PROCEDURE Sieve;
VAR i, j :INTEGER;
BEGIN
Prime[ 0 ] := FALSE; Prime[ 1 ] := FALSE;
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
Sieve;
FOR n := 2 TO Max DO
IF Prime[ n ] & Prime[ DigitSum( n ) ] THEN
Out.Int( n, 4 );
Count := Count + 1;
IF Count MOD 20 = 0 THEN Out.Ln END
END
END;
Out.Ln;Out.String( "There are " );Out.Int( Count, 1 );
Out.String( " additive primes less than " );Out.Int( Max, 1 );
Out.String( "." );Out.Ln
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|OCaml}}==
3,021

edits