mirror of
https://codeberg.org/ziglings/exercises.git
synced 2025-06-08 01:57:02 +09:00
parent
58dff3f504
commit
d729c3544f
3 changed files with 49 additions and 29 deletions
|
@ -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",
|
||||
|
|
|
@ -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 });
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue