1
0

First Commit

This commit is contained in:
2025-10-28 15:36:15 -05:00
commit 15c0ed4dc7
4 changed files with 143 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
.zig-cache
zig-out
+29
View File
@@ -0,0 +1,29 @@
# SieveEratosthenes
Sieve of Eratosthenes to find all 32-bit prime numbers in Zig. Uses the same algorithm as the [Java Version](https://github.com/WWelna/SieveEratosthenes).
```
wwelna@COINTEL-WORKSTATION:~/GitHub/SieveEratosthenes-zig$ zig build -Doptimize=ReleaseFast run
```
## License
Copyright (C) 2025 William Welna (wwelna@occultusterra.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
+30
View File
@@ -0,0 +1,30 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "SieveEratosthenes_zig",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
},
}),
});
exe.linkLibC();
b.installArtifact(exe);
const run_step = b.step("run", "Run the app");
const run_cmd = b.addRunArtifact(exe);
run_step.dependOn(&run_cmd.step);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
}
+82
View File
@@ -0,0 +1,82 @@
// Copyright (C) 2025 William Welna (wwelna@occultusterra.com)
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
const std = @import("std");
const max_num_32 = std.math.maxInt(u32);
const max_u64_need_32 = ((max_num_32 / 8)/8);
inline fn setBit(sieve:[]u64, i:usize) void {
sieve[i/64] |= @as(u64, 1) << @intCast(i % 64);
}
inline fn getBit(sieve:[]u64, i:usize) bool {
if(sieve[i/64] & @as(u64, 1) << @intCast(i % 64)>0) {
return true;
} else return false;
}
inline fn writeToFile(sieve:[]u64) !void {
var buffer:[8096]u8 = undefined;
const file = try std.fs.cwd().createFile("SieveEratosthenes-Zig.bin", .{.truncate=true});
defer file.close();
var writer = file.writer(&buffer);
var x:usize = 2;
while (x <= max_num_32) : (x += 1) {
if(!getBit(sieve, x)) {
const bytes = std.mem.toBytes(std.mem.nativeToBig(u32, @as(u32, @truncate(x))));
_ = try writer.interface.write(&bytes);
}
}
try writer.interface.flush();
}
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
var allocator = arena.allocator();
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout = &stdout_writer.interface;
var sieve_32:[]u64 = undefined;
sieve_32 = try allocator.alloc(u64, max_u64_need_32); @memset(sieve_32, 0);
try stdout.print("Starting...\n", .{});
try stdout.flush();
var x:usize = 2;
while (x * x <= max_num_32) : (x += 1) {
if (!getBit(sieve_32, x)) {
var y:usize = x * x;
while (y <= max_num_32) : (y += x) {
setBit(sieve_32, y);
}
}
}
try stdout.print("Writing to file...\n", .{});
try stdout.flush();
try writeToFile(sieve_32);
try stdout.flush();
arena.deinit();
}