1
0
Fork 0
mirror of https://codeberg.org/ziglings/exercises.git synced 2025-06-08 10:07:01 +09:00

changed to require exact start and end values

Resolves: #39
This commit is contained in:
Anthon van der Neut 2024-01-14 10:19:49 +01:00
parent 58dff3f504
commit d729c3544f
3 changed files with 49 additions and 29 deletions

View file

@ -1013,7 +1013,7 @@ const exercises = [_]Exercise{
}, },
.{ .{
.main_file = "095_for3.zig", .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", .main_file = "096_memory_allocation.zig",

View file

@ -45,29 +45,49 @@
// std.debug.print("{} ", .{n}); // std.debug.print("{} ", .{n});
// } // }
// //
// Let's try out the new form of 'for' to re-implement that // We can achieve the same, using a 'for' loop and a range, without
// exercise: // having to initialise 'var n':
// //
const std = @import("std"); // for (0..21) |n| {
// // The '%' symbol is the "modulo" operator and it
pub fn main() void { // // returns the remainder after division.
// if (n % 3 == 0) continue;
// I want to print every number between 1 and 20 that is NOT // if (n % 5 == 0) continue;
// divisible by 3 or 5. // std.debug.print("{} ", .{n});
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", .{});
}
// //
// That's a bit nicer, right? // That's a bit nicer, right?
// //
// Of course, both 'while' and 'for' have different advantages. // Of course, both 'while' and 'for' have different advantages.
// Exercises 11, 12, and 14 would NOT be simplified by switching // Exercises 11, 12, and 14 would NOT be simplified by switching
// a 'while' for a 'for'. // 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 });
}

View file

@ -1,11 +1,11 @@
--- exercises/095_for3.zig 2023-10-03 22:15:22.125574535 +0200 --- exercises/095_for3.zig 2024-01-14 10:03:37
+++ answers/095_for3.zig 2023-10-05 20:04:07.272770937 +0200 +++ answers/095_for3.zig 2024-01-14 10:17:06
@@ -54,7 +54,7 @@ @@ -79,7 +79,7 @@
var factorial: u64 = undefined;
// I want to print every number between 1 and 20 that is NOT // I want to print 10!
// divisible by 3 or 5.
- for (???) |n| { - for (???) |n| {
+ for (1..21) |n| { + for (1.11) |n| {
// 0! is a special case, and by convention equals 1.
// The '%' symbol is the "modulo" operator and it // we don't handle that case in this exercise
// returns the remainder after division. if (n < 1) return FactorialError.NonPositive;