const std = @import("std"); const ArrayList = std.ArrayList; const print = std.debug.warn; 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; var biggest: usize = 0; for (primes) |prime| { if (n % prime == 0) { biggest = prime; } } std.debug.warn("{}\n", .{biggest}); } fn p4() void { var i: usize = 0; var j: usize = 0; var str: [6]u8 = undefined; var largest: usize = 0; while (i < 1000) : (i += 1) { j = 0; while (j < 1000) : (j += 1) { if (std.fmt.bufPrint(&str, "{}", .{i * j})) |val| { if (i * j > largest and str[0] == str[5] and str[1] == str[4] and str[2] == str[3]) { largest = i * j; } } else |err| { print("{}\n", .{err}); } } } print("{}\n", .{largest}); } fn p5() void { var num: usize = 20; var i: usize = 1; var j: usize = 0; var found: bool = false; while (i < 1_500_000_000) : (i += 1) { j = num; found = false; while (j != 0) : (j -= 1) { if (i % j != 0) { found = true; break; } } if (!found) { print("{}\n", .{i}); break; } } } fn p6() void { var sum_0: usize = 0; var sum_1: usize = 0; var i: usize = 1; while (i <= 100) : (i += 1) { sum_0 += i * i; sum_1 += i; } sum_1 *= sum_1; print("{}\n", .{sum_1 - sum_0}); } fn p7() void { var i: usize = 1; var j: usize = 0; while (j != 10_002) : (i += 1) { if (is_prime(i)) { j += 1; } } print("{}\n", .{i - 1}); } fn remove(str: []u8, c: u8) void { var tmp: [1024]u8 = undefined; var counter: u16 = 0; for (str) |char| { if (char != c) { tmp[counter] = char; counter += 1; } } insert(str, &tmp); } fn insert(output: []u8, input: []const u8) void { for (input) |_, i| { output[i] = input[i]; } } fn len(str: []u8) usize { var i: usize = 0; for (str) |c| { if (c < 128) { i += 1; } else { break; } } return i; } fn p8() void { var largest: usize = 0; if (std.fs.cwd().openFile("p8", .{ .read = true })) |file| { var str: [1024]u8 = undefined; if (file.read(&str)) |val| { remove(&str, '\n'); var i: usize = 0; while (i < len(&str) - 12) : (i += 1) { var product: usize = 1; for (str[i .. i + 13]) |c| { var n: usize = c - 48; if (n < 10) { product *= c - 48; } else { break; } } if (product > largest) { largest = product; } } } else |err| { print("{}\n", .{err}); } } else |err| { print("{}\n", .{err}); } print("{}\n", .{largest}); } fn sqrt(x: f64) f64 { var z: f64 = 1; var i: u8 = 1; while (i <= 10) : (i += 1) { z -= (z * z - x) / (2 * z); } return z; } fn p9() void { var c: usize = 1; main: while (c <= 1000) : (c += 1) { //var c2: usize = c*c; //while (b var b: usize = 1; while (b < c) : (b += 1) { var a: usize = 1; while (a < b) : (a += 1) { if (a * a + b * b == c * c and a + b + c == 1000) { print("{}\n", .{a * b * c}); break :main; } } } } } fn p10() void { const size: usize = 2_000_001; var primes: [size]bool = undefined; for (primes) |prime, i| { primes[i] = true; } var i: usize = 2; while (i < size) : (i += 1) { if (primes[i]) { var j: usize = 2; while (j * i < size) : (j += 1) { primes[j * i] = false; } } } var sum: usize = 0; for (primes) |prime, num| { if (prime and num != 1) { sum += num; } } print("{}\n", .{sum}); } fn strtoint(str: []u8) usize { var sum: usize = 0; var str_len: usize = len(str); for (str) |c, j| { sum += (str[str_len - (j + 1)] - 48) * (exp(10, j)); } return sum; } fn exp(x: usize, y: usize) usize { var i: usize = 0; var total: usize = 1; while (i < y) : (i += 1) { total *= x; } return total; } fn p11() void { var array: [20][20]usize = undefined; var i: usize = 0; var j: usize = 0; var k: usize = 0; if (std.fs.cwd().openFile("p11", .{ .read = true })) |file| { var str: [4096]u8 = undefined; if (file.read(&str)) |str_len| { while (i < str_len) : (i += 3) { array[j][k] = strtoint(str[i .. i + 2]); if (j == 19) { j = 0; k += 1; } else { j += 1; } } } else |err| { print("{}\n", .{err}); } } else |err| { print("{}\n", .{err}); } var largest: usize = 0; var product: usize = 0; i = 0; while (i < 16) : (i += 1) { j = 0; while (j < 16) : (j += 1) { product = array[i][j] * array[i + 1][j] * array[i + 2][j] * array[i + 3][j]; if (product > largest) { largest = product; } product = array[i][j] * array[i][j + 1] * array[i][j + 2] * array[i][j + 3]; if (product > largest) { largest = product; } product = array[i][j] * array[i + 1][j + 1] * array[i + 2][j + 2] * array[i + 3][j + 3]; if (product > largest) { largest = product; } product = array[i + 3][j] * array[i + 2][j + 1] * array[i + 1][j + 2] * array[i][j + 3]; if (product > largest) { largest = product; } } } print("{}\n", .{largest}); } fn gen_primes() ArrayList(usize) { var bool_primes = ArrayList(bool).init(std.heap.page_allocator); const size: usize = 10_000_000; var i: usize = 0; while (i < size) : (i += 1) { if (bool_primes.append(true)) |val| {} else |err| { print("{}", .{"allocation error"}); } } i = 2; while (i < size) : (i += 1) { if (bool_primes.items[i]) { var j: usize = 2; while (j * i < size) : (j += 1) { bool_primes.items[j * i] = false; } } } i = 2; var primes = ArrayList(usize).init(std.heap.page_allocator); while (i < size) : (i += 1) { if (bool_primes.items[i]) { if (primes.append(i)) |val| {} else |err| { print("{}", .{"allocation error"}); } } } bool_primes.deinit(); return primes; } const Prime = struct { n: usize, count: usize }; const my_primes_size = 10; fn p12() void { var i: usize = 1; var triangle: usize = 0; var largest: usize = 0; var primes = gen_primes(); while (i < 20000) : (i += 1) { triangle += i; var my_primes: [my_primes_size]Prime = undefined; var count: usize = 0; var j: usize = 0; while (j < my_primes_size) : (j += 1) { my_primes[j].count = 0; } j = 0; while (j < primes.items.len) : (j += 1) { if (triangle % primes.items[j] == 0) { var prime: Prime = .{ .n = primes.items[j], .count = 0 }; my_primes[count] = prime; count += 1; } else if (primes.items[j] > triangle) { break; } } var t: usize = triangle; while (t != 1) { j = 0; while (j < count) : (j += 1) { if (t % my_primes[j].n == 0) { t /= my_primes[j].n; my_primes[j].count += 1; } } } j = 0; var divisor_amount: usize = 1; while (j < my_primes_size) : (j += 1) { if (my_primes[j].count == 0) { break; } divisor_amount *= my_primes[j].count + 1; } if (divisor_amount > 500) { print("{},{}\n", .{ triangle, divisor_amount }); break; } } } pub fn main() !void { //p1(); //p2(); //p3(); //p4(); //p5(); //p6(); //p7(); //p8(); //p9(); //p10(); //p11(); p12(); }