diff --git a/exercises/108_threading2.zig b/exercises/108_threading2.zig index e1b35ac..f143b40 100644 --- a/exercises/108_threading2.zig +++ b/exercises/108_threading2.zig @@ -28,9 +28,9 @@ // We will not go into the formula and its derivation in detail, but // will deal with the series straight away: // -// 4 4 4 4 4 -// PI = --- - --- + --- - --- + --- ... -// 1 3 5 7 9 +// 4 4 4 4 4 +// PI = + --- - --- + --- - --- + --- ... +// 1 3 5 7 9 // // As you can clearly see, the series starts with the whole number 4 and // approaches the circle number by subtracting and adding smaller and @@ -71,27 +71,28 @@ const std = @import("std"); pub fn main() !void { - const count = 1_000_000_000; + const count = 500_000_000; + var pi_plus: f64 = 0; var pi_minus: f64 = 0; { // First thread to calculate the plus numbers. - const handle1 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_plus, 5, count }); + const handle1 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_plus, 1, count }); defer handle1.join(); // Second thread to calculate the minus numbers. ??? - } + // Here we add up the results. - std.debug.print("PI ≈ {d:.8}\n", .{4 + pi_plus - pi_minus}); + std.debug.print("PI ≈ {d:.8} (error = {e:.1})\n", .{ pi_plus - pi_minus, std.math.pi - (pi_plus - pi_minus) }); } -fn thread_pi(pi: *f64, begin: u64, end: u64) !void { - var n: u64 = begin; - while (n < end) : (n += 4) { - pi.* += 4 / @as(f64, @floatFromInt(n)); +fn thread_pi(pi: *f64, begin: u64, count: u64) !void { + for (0..count) |i| { + const term = 4 / @as(f64, @floatFromInt(begin + 4 * i)); + pi.* += term; } } // If you wish, you can increase the number of loop passes, which @@ -106,5 +107,6 @@ fn thread_pi(pi: *f64, begin: u64, end: u64) !void { // And you should remove the formatting restriction in "print", // otherwise you will not be able to see the additional digits. // -// If count = 10_000_000_000_000 you should see the following: +// If count = 2_500_000_000_000, you should see the following: // 3.141592653589 +// (after waiting on the order of 1000 seconds, or 17 minutes) diff --git a/patches/patches/108_threading2.patch b/patches/patches/108_threading2.patch index c06a985..2cd49d8 100644 --- a/patches/patches/108_threading2.patch +++ b/patches/patches/108_threading2.patch @@ -1,13 +1,12 @@ ---- exercises/108_threading2.zig 2026-04-14 06:44:18.848246237 +0200 -+++ answers/108_threading2.zig 2026-04-14 08:15:30.894485037 +0200 -@@ -81,8 +81,8 @@ +--- exercises/108_threading2.zig 2026-06-29 23:25:53 ++++ answers/108_threading2.zig 2026-06-29 23:25:25 +@@ -82,7 +82,8 @@ defer handle1.join(); // Second thread to calculate the minus numbers. - ??? -- + const handle2 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_minus, 3, count }); + defer handle2.join(); } + // Here we add up the results. - std.debug.print("PI ≈ {d:.8}\n", .{4 + pi_plus - pi_minus}); diff --git a/rivendell/elrond.zig b/rivendell/elrond.zig index 2d26ce9..f9ca0c4 100644 --- a/rivendell/elrond.zig +++ b/rivendell/elrond.zig @@ -1173,7 +1173,7 @@ const exercises = [_]Exercise{ }, .{ .main_file = "108_threading2.zig", - .output = "PI ≈ 3.14159265", + .output = "PI ≈ 3.14159265 (error = 1.0e-9)", }, .{ .main_file = "109_files.zig",