Primes whose sum of digits is 25: Difference between revisions
m
→{{header|Wren}}: Minor tidy
No edit summary |
m (→{{header|Wren}}: Minor tidy) |
||
(11 intermediate revisions by 9 users not shown) | |||
Line 12:
::: <big>(997 ≤ '''n''' ≤ 1,111,111,111,111,111,111,111,111). </big>
<br><br>
=={{header|11l}}==
{{trans|Nim}}
<syntaxhighlight lang="11l">F is_prime(a)
I a == 2
R 1B
I a < 2 | a % 2 == 0
R 0B
L(i) (3 .. Int(sqrt(a))).step(2)
I a % i == 0
R 0B
R 1B
F digit_sum(=n)
V result = 0
L n != 0
result += n % 10
n I/= 10
R result
V c = 0
L(n) 5000
I digit_sum(n) == 25 & is_prime(n)
c++
print(‘#4’.format(n), end' I c % 6 == 0 {"\n"} E ‘ ’)
print()
print(‘Found ’c‘ primes whose sum of digits is 25’)</syntaxhighlight>
{{out}}
<pre>
997 1699 1789 1879 1987 2689
2797 2887 3499 3697 3769 3877
3967 4597 4759 4957 4993
Found 17 primes whose sum of digits is 25
</pre>
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<
BYTE FUNC SumOfDigits(INT x)
Line 44 ⟶ 80:
OD
PrintF("%E%EThere are %I primes",count)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Primes_which_sum_of_digits_is_25.png Screenshot from Atari 8-bit computer]
Line 54 ⟶ 90:
=={{header|ALGOL 68}}==
<
# show all sum25 primes below 5000 #
PR read "primes.incl.a68" PR
Line 76 ⟶ 112:
OD;
print( ( newline, "Found ", whole( p25 count, 0 ), " sum25 primes below ", whole( UPB prime + 1, 0 ), newline ) )
END</
{{out}}
<pre>
Line 87 ⟶ 123:
Uses the [[Miller–Rabin_primality_test#ALGOL 68|Miller Rabin primality test]] and the pow mod procedure from [[ALGOL_68/prelude#prelude.2Fpow_mod.a68|prelude/pow_mod]]<br>
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<
PROC pow mod = (LONG LONG INT b,in e, modulus)LONG LONG INT: (
LONG LONG INT sq := b, e := in e;
Line 140 ⟶ 176:
sum25( 0, 25 );
print( ( "There are ", whole( p25 count, 0 ), " sum25 primes that contain no zeroes", newline ) )
END</
{{out}}
Note that ALGOL 68G under Windows is fully interpreted so runtime is not of the same order as the Phix and Go samples. Under Linux with optimisation and compilation, it should be faster than under Windows.
Line 148 ⟶ 184:
=={{header|ALGOL W}}==
<
% sets p( 1 :: n ) to a sieve of primes up to n %
procedure Eratosthenes ( logical array p( * ) ; integer value n ) ;
Line 187 ⟶ 223:
write( i_w := 1, s_w := 0, "Found ", pCount, " sum25 primes below ", MAX_NUMBER + 1 )
end
end.</
{{out}}
<pre>
Line 197 ⟶ 233:
===Functional===
Not fast. This approach takes over 20 seconds here.
<
use framework "Foundation"
use scripting additions
Line 358 ⟶ 394:
end tell
return ys
end takeWhile</
{{Out}}
<pre>[997,1699,1789,1879,1987,2689,2797,2887,3499,3697,3769,3877,3967,4597,4759,4957,4993]</pre>
Line 366 ⟶ 402:
Primes with silly properties are getting a bit tedious. But hey. This takes just under 0.02 seconds.
<
script o
property numberList : {missing value}
Line 411 ⟶ 447:
-- Task code:
return numbersWhoseDigitsSumTo(sieveOfEratosthenes(4999), 25)</
{{output}}
<
=={{header|Arturo}}==
<
loop split.every: 3 select primes 'p [25 = sum digits p] 'a ->
print map a => [pad to :string & 5]</
{{out}}
Line 433 ⟶ 469:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f PRIMES_WHICH_SUM_OF_DIGITS_IS_25.AWK
BEGIN {
Line 464 ⟶ 500:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 474 ⟶ 510:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
function isprime(num)
for i = 2 to int(sqr(num))
Line 501 ⟶ 537:
print "Se encontraron "; total; " primos sum25 por debajo de "; final
end
</syntaxhighlight>
{{out}}
<pre>
Line 509 ⟶ 545:
=={{header|C}}==
<
#include <stdio.h>
Line 560 ⟶ 596:
return 0;
}</
{{out}}
<pre>997 1699 1789 1879 1987 2689 2797 2887 3499 3697 3769 3877 3967 4597 4759 4957 4993</pre>
Line 567 ⟶ 603:
{{libheader|GMP}}
Stretch goal solved the same way as Phix and Go.
<
#include <chrono>
#include <iomanip>
Line 645 ⟶ 681:
<< std::chrono::duration<double>(end - start).count() << "s\n";
return 0;
}</
{{out}}
Line 666 ⟶ 702:
=={{header|D}}==
{{trans|C}}
<
import std.stdio;
Line 713 ⟶ 749:
}
writeln;
}</
{{out}}
<pre>997 1699 1789 1879 1987 2689 2797 2887 3499 3697 3769 3877 3967 4597 4759 4957 4993 </pre>
Line 721 ⟶ 757:
{{libheader| PrimTrial}}
{{Trans|Ring}}
<syntaxhighlight lang="delphi">
program Primes_which_sum_of_digits_is_25;
Line 760 ⟶ 796:
end;
readln;
end.</
{{out}}
<pre> 997 1699 1789 1879 1987
Line 768 ⟶ 804:
=={{header|F_Sharp|F#}}==
<
// Primes to 5000 who's sum of digits is 25. Nigel Galloway: April 1st., 2021
let rec fN g=function n when n<10->n+g=25 |n->fN(g+n%10)(n/10)
primes32()|>Seq.takeWhile((>)5000)|>Seq.filter fN|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
Line 779 ⟶ 815:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<
: digit-sum ( n -- sum )
Line 786 ⟶ 822:
: lprimes25 ( -- list ) lprimes [ digit-sum 25 = ] lfilter ;
lprimes25 [ 5,000 < ] lwhile [ . ] leach</
{{out}}
<pre style="height:14em">
Line 810 ⟶ 846:
=={{header|Forth}}==
{{works with|Gforth}}
<
: notprime! ( n -- ) here + 1 swap c! ;
Line 857 ⟶ 893:
5000 .prime25
bye</
{{out}}
Line 870 ⟶ 906:
=={{header|FreeBASIC}}==
{{trans|AWK}}
<
Function isprime(num As Ulongint) As Boolean
For i As Integer = 2 To Sqr(num)
Line 895 ⟶ 931:
Next i
Print !"\n\nSe encontraron"; total; " primos sum25 por debajo de"; finalSleep
</syntaxhighlight>
{{out}}
<pre>
Line 904 ⟶ 940:
</pre>
=={{header|Frink}}==
<syntaxhighlight lang="frink">println[select[primes[2,4999], {|x| sum[integerDigits[x]] == 25}]]</syntaxhighlight>
{{out}}
<pre>
[997, 1699, 1789, 1879, 1987, 2689, 2797, 2887, 3499, 3697, 3769, 3877, 3967, 4597, 4759, 4957, 4993]
</pre>
=={{header|Go}}==
{{libheader|GMP(Go wrapper)}}
This uses the Phix routine for the stretch goal though I've had to plug in a GMP wrapper to better the Phix time. Using Go's native big.Int, the time was slightly slower than Phix at 1 minute 28 seconds.
<
import (
Line 1,007 ⟶ 1,049:
fmt.Println("\nThere are", commatize(n), "primes whose digits sum to 25 and include no zeros.")
fmt.Printf("\nTook %s\n", time.Since(start))
}</
{{out}}
Line 1,020 ⟶ 1,062:
=={{header|Haskell}}==
<
import Data.List (replicate)
import Data.List.Split (chunksOf)
Line 1,056 ⟶ 1,098:
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</
{{Out}}
<pre>17 primes (< 5000) with decimal digits totalling 25:
Line 1,065 ⟶ 1,107:
3967 4597 4759 4957
4993</pre>
=={{header|J}}==
<syntaxhighlight lang="j"> (#~ 25 = +/@("."0@":)"0) p: i. _1 p: 5000
997 1699 1789 1879 1987 2689 2797 2887 3499 3697 3769 3877 3967 4597 4759 4957 4993</syntaxhighlight>
=={{header|Java}}==
{{trans|Kotlin}}
<
public class PrimeSum {
Line 1,093 ⟶ 1,139:
System.out.println();
}
}</
{{out}}
<pre>997 1699 1789 1879 1987 2689 2797 2887 3499 3697 3769 3877 3967 4597 4759 4957 4993 </pre>
=={{header|JavaScript}}==
<
"use strict";
Line 1,228 ⟶ 1,274:
return main();
})();</
<pre>997 1699 1789 1879 1987
2689 2797 2887 3499 3697
Line 1,244 ⟶ 1,290:
'''Preliminaries'''
<
def emit_until(cond; stream): label $out | stream | if cond then break $out else . end;</
'''The Task'''
<
def task($sum):
# Input: array of digits
Line 1,256 ⟶ 1,302:
| select(is_prime);
emit_until(. >= 5000; task(25) )</
{{out}}
<pre>
Line 1,279 ⟶ 1,325:
=={{header|Julia}}==
<
let
Line 1,294 ⟶ 1,340:
println("\nTotal found: $pcount")
end
</
<pre>
Primes with digits summing to 25 between 0 and 5000:
Line 1,302 ⟶ 1,348:
=== Stretch goal ===
{{trans|Phix}}
<
function sum25(p::String, rm, res)
Line 1,319 ⟶ 1,365:
@time println("There are ", format(sum25("", 25, 0), commas=true),
" primes whose digits sum to 25 without any zero digits.")
</
<pre>
There are 1,525,141 primes whose digits sum to 25 without any zero digits.
Line 1,326 ⟶ 1,372:
=={{header|Kotlin}}==
<
fun digitSum(bi: BigInteger): Int {
Line 1,352 ⟶ 1,398:
}
println()
}</
{{out}}
<pre>997 1699 1789 1879 1987 2689 2797 2887 3499 3697 3769 3877 3967 4597 4759 4957 4993 </pre>
=={{header|Ksh}}==
<
#!/bin/ksh
Line 1,402 ⟶ 1,448:
done
echo
</syntaxhighlight>
{{out}}<pre>
997 1699 1789 1879 1987 2689 2797 2887 3499 3697 3769 3877 3967 4597 4759 4957 4993 </pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
{{out}}
<pre>{997, 1699, 1789, 1879, 1987, 2689, 2797, 2887, 3499, 3697, 3769, 3877, 3967, 4597, 4759, 4957, 4993}</pre>
=={{header|Nanoquery}}==
<
// https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Pseudocode
def find_primes(upper_bound)
Line 1,450 ⟶ 1,496:
end if
end for
println</
{{out}}
<pre>997 1699 1789 1879 1987 2689 2797 2887 3499 3697 3769 3877 3967 4597 4759 4957 4993 </pre>
Line 1,456 ⟶ 1,502:
=={{header|Nim}}==
===Task===
<
func isPrime(n: Natural): bool =
Line 1,482 ⟶ 1,528:
for i, n in result:
stdout.write ($n).align(4), if (i + 1) mod 6 == 0: '\n' else: ' '
echo()</
{{out}}
Line 1,492 ⟶ 1,538:
{{trans|Julia}}
{{libheader|bignum}}
<
import bignum
Line 1,507 ⟶ 1,553:
let count = $sum25("", 25, 0)
echo &"There are {count.insertSep()} primes whose digits sum to 25 without any zero digits."
echo "\nExecution time: ", getMonoTime() - t0</
{{out}}
Line 1,517 ⟶ 1,563:
added only strechted goal.Generating the combination of the digits for the numbers and afterwards generating the [[Permutations with some identical elements]]<BR>
Now seting one digit out of 1,3,7,9 to the end and permute the rest of the digits in front.<BR>So much less numbers have to be tested.10.5e6 instead of 16.4e6.Generating of the numbers is reduced in the same ratio.
<
//formerly roborally take 5 cards out of 8
{$IFDEF FPC}
Line 1,722 ⟶ 1,768:
mpz_clear(z);
END.
</syntaxhighlight>
{{out}}
<pre>
Line 1,749 ⟶ 1,795:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 1,758 ⟶ 1,804:
is_prime($_) and 25 == sum(split '', $_) and push @p25, $_ for 1..$limit;
say @p25 . " primes < $limit with digital sum 25:\n" . join ' ', @p25;
</syntaxhighlight>
{{out}}
<pre>17 primes < 5000 with digital sum 25:
Line 1,764 ⟶ 1,810:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">sum25</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">),</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">))=</span><span style="color: #000000;">25</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">sum25</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">),</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d sum25 primes less than 5000 found: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #000000;">r</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 1,776 ⟶ 1,822:
===Stretch goal===
{{libheader|Phix/mpfr}}
<!--<
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,804 ⟶ 1,850:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"There are %,d sum25 primes that contain no zeroes\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sum25</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">25</span><span style="color: #0000FF;">))</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 1,814 ⟶ 1,860:
=={{header|Python}}==
<
from itertools import takewhile
Line 1,887 ⟶ 1,933:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>17 primes below 5000 with a decimal digit sum of 25:
Line 1,896 ⟶ 1,942:
3967 4597 4759 4957
4993</pre>
=={{header|Quackery}}==
<code>eratosthenes</code> and <code>isprime</code> are defined at [[Sieve of Eratosthenes#Quackery]].
<syntaxhighlight lang="Quackery"> 5000 eratosthenes
[ 0
[ over while
swap 10 /mod
rot + again ]
nip ] is digitsum ( n --> n )
[] 5000 times
[ i^ isprime not if done
i^ digitsum 25 = if
[ i^ join ] ]
echo
</syntaxhighlight>
{{out}}
<pre>[ 997 1699 1789 1879 1987 2689 2797 2887 3499 3697 3769 3877 3967 4597 4759 4957 4993 ]</pre>
=={{header|Raku}}==
<syntaxhighlight lang="raku"
say "{+$_} primes < $limit with digital sum 25:\n{$_».fmt("%" ~ $limit.chars ~ "d").batch(10).join("\n")}",
with ^$limit .grep: { .is-prime and .comb.sum == 25 }</
{{out}}
<pre>17 primes < 5000 with digital sum 25:
Line 1,911 ⟶ 1,980:
:* the number of columns shown per line ('''COLS''')
;* the target sum ('''TARGET''')
<
parse arg hi cols target . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 5000 /*Not specified? Then use the default.*/
Line 1,959 ⟶ 2,028:
/*──────────────────────────────────────────────────────────────────────────────────────*/
sumDigs: parse arg x 1 s 2 '' -1 z; L= length(x); if L==1 then return s; s= s + z
do m=2 for L-2; s= s + substr(x, m, 1); end; return s</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,977 ⟶ 2,046:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 2,044 ⟶ 2,113:
return 1
ok
</syntaxhighlight>
{{out}}
<pre>
Line 2,058 ⟶ 2,127:
time = 30 mins
done...
</pre>
=={{header|RPL}}==
<code>∑DIGITS</code> is defined at [[Sum digits of an integer#RPL|Sum digits of an integer]]
≪ { } 799 <span style="color:grey">@ 799 is the smallest integer whose digits sum equals 25, and is not prime : 799 = 47 * 17</span>
'''DO'''
NEXTPRIME
IF DUP <span style="color:blue">∑DIGITS</span> 25 == '''THEN''' SWAP OVER + SWAP '''END'''
'''UNTIL''' DUP 5000 > '''END'''
DROP
≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: {997 1699 1789 1879 1987 2689 2797 2887 3499 3697 3769 3877 3967 4597 4759 4957 4993}
</pre>
=={{header|Ruby}}==
<
def digitSum(n)
Line 2,076 ⟶ 2,159:
print p, " "
end
end</
{{out}}
<pre>997 1699 1789 1879 1987 2689 2797 2887 3499 3697 3769 3877 3967 4597 4759 4957 4993 </pre>
Line 2,082 ⟶ 2,165:
=={{header|Sidef}}==
Simple solution:
<
Generate such primes from digits (asymptotically faster):
<
var seq = [p]
Line 2,110 ⟶ 2,193:
}
say primes_with_digit_sum(5000)</
{{out}}
<pre>
Line 2,119 ⟶ 2,202:
{{tcllib|math::numtheory}}
Could be made prettier with the staple helper proc lfilter.
<
package require math::numtheory
namespace path ::tcl::mathop
Line 2,125 ⟶ 2,208:
puts [lmap x [math::numtheory::primesLowerThan 5000] {
if {[+ {*}[split $x {}]] == 25} {set x} else continue
}]</
{{out}}
Line 2,131 ⟶ 2,214:
=={{header|Wren}}==
===Basic===
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./math" for Int
import "./fmt" for Fmt
var sumDigits = Fn.new { |n|
Line 2,154 ⟶ 2,235:
}
System.print("The %(primes25.count) primes under 5,000 whose digits sum to 25 are:")
Fmt.tprint("$,6d", primes25, 6)</syntaxhighlight>
{{out}}
Line 2,163 ⟶ 2,244:
3,967 4,597 4,759 4,957 4,993
</pre>
===Stretch===
{{trans|Go}}
{{libheader|Wren-gmp}}
Run time is about 25.5 seconds.
<syntaxhighlight lang="wren">import "./gmp" for Mpz
import "./fmt" for Fmt
var z = Mpz.new()
var countAll // recursive
countAll = Fn.new { |p, rem, res|
if (rem == 0) {
var b = p[-1]
if ("1379".contains(b)) {
if (z.setStr(p).probPrime(15) > 0) res = res + 1
}
} else {
for (i in 1..rem.min(9)) {
res = countAll.call(p + i.toString, rem - i, res)
}
}
return res
}
var n = countAll.call("", 25, 0)
Fmt.print("There are $,d primes whose digits sum to 25 and include no zeros.", n)</syntaxhighlight>
{{out}}
<pre>
There are 1,525,141 primes whose digits sum to 25 and include no zeros.
</pre>
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
if (N&1) = 0 then \even >2\ return false;
for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false;
I:= I+1;
];
return true;
];
func SumDigits(N); \Return sum of digits in N
int N, Sum;
[Sum:= 0;
repeat N:= N/10;
Sum:= Sum + rem(0);
until N=0;
return Sum;
];
int Cnt, N;
[Cnt:= 0;
for N:= 2 to 5000-1 do
if IsPrime(N) & SumDigits(N) = 25 then
[IntOut(0, N);
Cnt:= Cnt+1;
if rem(Cnt/5) then ChOut(0, 9\tab\) else CrLf(0);
];
CrLf(0);
IntOut(0, Cnt);
Text(0, " primes whose sum of digits is 25.
");
]</syntaxhighlight>
{{out}}
<pre>
997 1699 1789 1879 1987
2689 2797 2887 3499 3697
3769 3877 3967 4597 4759
4957 4993
17 primes whose sum of digits is 25.
</pre>
=={{header|Zig}}==
{{trans|Nim}}
<syntaxhighlight lang="zig">const std = @import("std");</syntaxhighlight>
<syntaxhighlight lang="zig">fn isPrime(n: u64) bool {
if (n < 2) return false;
if (n % 2 == 0) return n == 2;
if (n % 3 == 0) return n == 3;
var d: u64 = 5;
while (d * d <= n) {
if (n % d == 0) return false;
d += 2;
if (n % d == 0) return false;
d += 4;
}
return true;
}</syntaxhighlight>
<syntaxhighlight lang="zig">fn digitSum(n_: u64) u16 {
var n = n_; // parameters are immutable, copy to var
var sum: u16 = 0;
while (n != 0) {
sum += @truncate(u16, n % 10);
n /= 10;
}
return sum;
}</syntaxhighlight>
<syntaxhighlight lang="zig">pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
var result = std.ArrayList(u64).init(arena.allocator());
defer result.deinit();
{
var n: u64 = 3;
while (n <= 5000) : (n += 2)
if (digitSum(n) == 25 and isPrime(n))
try result.append(n);
}
const stdout = std.io.getStdOut().writer();
for (result.items, 0..) |n, i|
_ = try stdout.print("{d:4}{s}", .{ n, if ((i + 1) % 6 == 0) "\n" else " " });
}</syntaxhighlight>
{{out}}
<pre> 997 1699 1789 1879 1987 2689
2797 2887 3499 3697 3769 3877
3967 4597 4759 4957 4993</pre>
|