Additive primes: Difference between revisions

Content added Content deleted
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(→‎OCaml: add)
Line 2,182: Line 2,182:


Number of additive primes found: 54</pre>
Number of additive primes found: 54</pre>

=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let rec digit_sum n =
if n < 10 then n else n mod 10 + digit_sum (n / 10)

let is_prime n =
let rec test x =
let q = n / x in x > q || x * q <> n && n mod (x + 2) <> 0 && test (x + 6)
in if n < 5 then n lor 1 = 3 else n land 1 <> 0 && n mod 3 <> 0 && test 5

let is_additive_prime n =
is_prime n && is_prime (digit_sum n)

let () =
Seq.ints 0 |> Seq.take_while ((>) 500) |> Seq.filter is_additive_prime
|> Seq.iter (Printf.printf " %u") |> print_newline</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</pre>


=={{header|Pari/GP}}==
=={{header|Pari/GP}}==
Line 2,195: Line 2,213:
{{out}}
{{out}}
<pre>%1 = [54, 54, 54, 54]</pre>
<pre>%1 = [54, 54, 54, 54]</pre>

=={{header|Pascal}}==
=={{header|Pascal}}==
{{works with|Free Pascal}}{{works with|Delphi}} checking isPrime(sum of digits) before testimg isprime(num) improves speed.<br>Tried to speed up calculation of sum of digits.
{{works with|Free Pascal}}{{works with|Delphi}} checking isPrime(sum of digits) before testimg isprime(num) improves speed.<br>Tried to speed up calculation of sum of digits.