diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | build.zig | 24 | ||||
-rw-r--r-- | src/main.zig | 101 |
3 files changed, 126 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2040c29 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +zig-cache diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..563d5ec --- /dev/null +++ b/build.zig @@ -0,0 +1,24 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("pe", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..3096ab3 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,101 @@ +const std = @import("std"); +const print = std.debug.warn; +// spalf +// mypassword + +fn p1() void { + var i: usize = 0; + var sum: usize = 0; + + while (i < 1000) : (i += 1) { + if (i % 3 == 0) { + sum += i; + } else if (i % 5 == 0) { + sum += i; + } + } + + std.debug.warn("{}\n", .{sum}); +} + +fn p2() void { + const size: usize = 35; + var array: [size]usize = undefined; + for (array) |_, i| { + array[i] = 0; + } + array[0] = 1; + array[1] = 2; + + var i: usize = 2; + while (i < size) : (i += 1) { + array[i] = array[i - 1] + array[i - 2]; + } + + i = 0; + var sum: usize = 0; + while (array[i] < 4_000_000) : (i += 1) { + if (array[i] % 2 == 0) + sum += array[i]; + } + + std.debug.warn("{}\n", .{sum}); +} + +fn is_prime(k: usize) bool { + var i: usize = 2; + var divided: bool = false; + while (i < k) : (i += 1) { + if (k % i == 0) { + divided = true; + break; + } + } + return !divided; +} + +fn p3() void { + const size: usize = 5000; + var primes: [size]usize = undefined; + + var i: usize = 1; + var j: usize = 0; + while (j < size) : (i += 1) { + if (is_prime(i)) { + primes[j] = i; + j += 1; + } + } + + const n: usize = 600851475143; + //const n: usize = 13195; + for (primes) |prime| { + if (n % prime == 0) { + std.debug.warn("{}\n", .{prime}); + } + } +} + +fn p4() void { + var i: usize = 0; + var j: usize = 0; + var str: [10]u8 = undefined; + while (i < 100) : (i += 1) { + j = 0; + while (j < 100) : (j += 1) { + //std.fmt.bufPrint(&str, "{}", .{i * j}) catch unreachable; + //if (std.fmt.bufPrint(&str, "{}", .{i*j})) |ret| { + // std.debug.warn("{}\n", .{str}); + //} else | err | { + //} + //var str: parseInt(usize, i*j) + } + } +} + +pub fn main() void { + //p1(); + //p2(); + //p3(); + p4(); +} |