diff --git a/build.zig b/build.zig index ea47f11..4789007 100644 --- a/build.zig +++ b/build.zig @@ -1013,7 +1013,7 @@ const exercises = [_]Exercise{ }, .{ .main_file = "095_for3.zig", - .output = "1 2 4 7 8 11 13 14 16 17 19", + .output = "3628800", }, .{ .main_file = "096_memory_allocation.zig", diff --git a/exercises/095_for3.zig b/exercises/095_for3.zig index e4c4662..36a3b07 100644 --- a/exercises/095_for3.zig +++ b/exercises/095_for3.zig @@ -45,29 +45,49 @@ // std.debug.print("{} ", .{n}); // } // -// Let's try out the new form of 'for' to re-implement that -// exercise: +// We can achieve the same, using a 'for' loop and a range, without +// having to initialise 'var n': // -const std = @import("std"); - -pub fn main() void { - - // I want to print every number between 1 and 20 that is NOT - // divisible by 3 or 5. - for (???) |n| { - - // The '%' symbol is the "modulo" operator and it - // returns the remainder after division. - if (n % 3 == 0) continue; - if (n % 5 == 0) continue; - std.debug.print("{} ", .{n}); - } - - std.debug.print("\n", .{}); -} +// for (0..21) |n| { +// // The '%' symbol is the "modulo" operator and it +// // returns the remainder after division. +// if (n % 3 == 0) continue; +// if (n % 5 == 0) continue; +// std.debug.print("{} ", .{n}); +// } // // That's a bit nicer, right? // // Of course, both 'while' and 'for' have different advantages. // Exercises 11, 12, and 14 would NOT be simplified by switching // a 'while' for a 'for'. +// +// +// According to wikipedia: In mathematics, the factorial of a +// non-negative integer n, denoted by n!, is the product of +// all positive integers less than or equal to n. +// +// The factorial can be calculated recursively, but here let's +// try out the new form of 'for' to calculate 10! : +// +const std = @import("std"); + +const FactorialError = error{ NonPositive }; + +pub fn main() !void { + + var factorial: u64 = undefined; + + // I want to print 10! + for (???) |n| { + // 0! is a special case, and by convention equals 1. + // we don't handle that case in this exercise + if (n < 1) return FactorialError.NonPositive; + if (n == 1) { + factorial = 1; + } else factorial = factorial * n; + } + + std.debug.print("{}\n", .{ factorial }); +} + diff --git a/patches/patches/095_for3.patch b/patches/patches/095_for3.patch index ca9e3ad..4abbb2f 100644 --- a/patches/patches/095_for3.patch +++ b/patches/patches/095_for3.patch @@ -1,11 +1,11 @@ ---- exercises/095_for3.zig 2023-10-03 22:15:22.125574535 +0200 -+++ answers/095_for3.zig 2023-10-05 20:04:07.272770937 +0200 -@@ -54,7 +54,7 @@ +--- exercises/095_for3.zig 2024-01-14 10:03:37 ++++ answers/095_for3.zig 2024-01-14 10:17:06 +@@ -79,7 +79,7 @@ + var factorial: u64 = undefined; - // I want to print every number between 1 and 20 that is NOT - // divisible by 3 or 5. + // I want to print 10! - for (???) |n| { -+ for (1..21) |n| { - - // The '%' symbol is the "modulo" operator and it - // returns the remainder after division. ++ for (1.11) |n| { + // 0! is a special case, and by convention equals 1. + // we don't handle that case in this exercise + if (n < 1) return FactorialError.NonPositive;