Primes whose sum of digits is 25: Difference between revisions

m
m (syntax highlighting fixup automation)
 
(9 intermediate revisions by 6 users not shown)
Line 802:
3769 3877 3967 4597 4759
4957 4993</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
fastfunc nextprim prim .
repeat
prim += 1
until isprim prim = 1
.
return prim
.
func digok n .
while n > 0
dsum += n mod 10
n = n div 10
.
return if dsum = 25
.
p = 2
repeat
if digok p = 1
write p & " "
.
p = nextprim p
until p >= 5000
.
</syntaxhighlight>
{{out}}
<pre>
997 1699 1789 1879 1987 2689 2797 2887 3499 3697 3769 3877 3967 4597 4759 4957 4993
</pre>
 
=={{header|F_Sharp|F#}}==
Line 813 ⟶ 853:
997 1699 1789 1879 1987 2689 2797 2887 3499 3697 3769 3877 3967 4597 4759 4957 4993
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
Line 1,107 ⟶ 1,148:
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}}==
Line 1,938 ⟶ 1,983:
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}}==
Line 2,100 ⟶ 2,168:
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>
 
Line 2,173 ⟶ 2,255:
 
=={{header|Wren}}==
===Basic===
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./math" for Int
{{libheader|Wren-seq}}
import "./fmt" for Fmt
Although do-able, the stretch goal would take too long in Wren so I haven't bothered.
<syntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
import "/seq" for Lst
 
var sumDigits = Fn.new { |n|
Line 2,196 ⟶ 2,276:
}
System.print("The %(primes25.count) primes under 5,000 whose digits sum to 25 are:")
for (chunk in Lst.chunks(primes25, 6)) Fmt.printtprint("$,6d", chunkprimes25, 6)</syntaxhighlight>
 
{{out}}
Line 2,204 ⟶ 2,284:
2,797 2,887 3,499 3,697 3,769 3,877
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>
 
Line 2,249 ⟶ 2,360:
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>
2,083

edits