Circular primes: Difference between revisions

m
→‎{{header|Zig}}: update to Zig 0.11.0 - "Allocgate" refactor - casting builtins renaming
(add RPL)
m (→‎{{header|Zig}}: update to Zig 0.11.0 - "Allocgate" refactor - casting builtins renaming)
Line 3,300:
</pre>
=={{header|Zig}}==
{{works with|Zig|0.11.0}}
As of now (Zig release 0.8.1), Zig has large integer support, but there is not yet a prime test in the standard library. Therefore, we only find the circular primes < 1e6. As with the Prolog version, we only check numbers composed of 1, 3, 7, or 9.
<syntaxhighlight lang="zig">
const std = @import("std");
const math = std.math;
const heap = std.heap;
const stdout = std.io.getStdOut().writer();
 
pub fn main() !void {
Line 3,311:
defer arena.deinit();
 
varconst candidatesallocator = std.PriorityQueue(u32).init(&arena.allocator, u32cmp();
 
var candidates = std.PriorityQueue(u32, void, u32cmp).init(allocator, {});
defer candidates.deinit();
 
const stdout = std.io.getStdOut().writer();
 
try stdout.print("The circular primes are:\n", .{});
Line 3,337 ⟶ 3,341:
}
 
fn u32cmp(_: void, a: u32, b: u32) math.Order {
return math.order(a, b);
}
 
fn circular(n0: u32) bool {
if (!isprimeisPrime(n0))
return false
else {
var n = n0;
var d: u32 = @floatToIntintFromFloat(u32, @log10(@intToFloatas(f32, @floatFromInt(n))));
return while (d > 0) : (d -= 1) {
n = rotate(n);
if (n < n0 or !isprimeisPrime(n))
break false;
} else true;
Line 3,359 ⟶ 3,363:
return 0
else {
const d: u32 = @floatToIntintFromFloat(u32, @log10(@intToFloatas(f32, @floatFromInt(n)))); // digit count - 1
const m = math.pow(u32, 10, d);
return (n % m) * 10 + n / m;
Line 3,365 ⟶ 3,369:
}
 
fn isprimeisPrime(n: u32) bool {
if (n < 2)
return false;
59

edits