diff options
author | Tom Barrett <tom@tombarrett.xyz> | 2021-03-07 17:58:54 +0100 |
---|---|---|
committer | Tom Barrett <tom@tombarrett.xyz> | 2021-03-07 17:58:54 +0100 |
commit | 75dc4585f943a579cdc0fcdc415afb8d7e40468c (patch) | |
tree | 7cb0eef62bcebf0a4512c814a0fd4bf89ee6569f /src |
starting out
Diffstat (limited to 'src')
-rw-r--r-- | src/main.zig | 101 |
1 files changed, 101 insertions, 0 deletions
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(); +} |