This repository was archived by the owner on Jul 18, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path18-divide_and_conquer.zig
More file actions
63 lines (52 loc) · 1.63 KB
/
Copy path18-divide_and_conquer.zig
File metadata and controls
63 lines (52 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const std = @import("std");
const print = std.debug.print;
const time = std.time;
const Timestamp = std.Io.Timestamp;
/// Function to calculate `x` raised to the power n using divide and conquer.
///
/// # Parameters
///
/// - `x`: The base value (can be negative or fractional).
/// - `n`: The exponent value (can be negative).
///
/// # Returns
///
/// The result of `x` raised to the power n as a `f64` (double precision).
fn power(x: f64, n: i32) f64 {
if (n == 0) {
return 1.0;
}
var base = x;
var exponent = n;
if (exponent < 0) {
base = 1.0 / base;
exponent = -exponent;
}
const half = power(base, @divFloor(exponent, 2));
if (@mod(exponent, 2) == 0) {
return half * half;
} else {
return base * half * half;
}
}
/// Main function to demonstrate the power function and measure execution time.
pub fn main(init: std.process.Init) !void {
const io = init.io;
const x: f64 = 2.0;
const N_values = [_]i32{ 10, 20, 40, 80, 160, 320, 640 };
for (N_values) |n| {
// Measure start time
const start = Timestamp.now(io, .awake);
// Calculate x^n
const result = power(x, n);
// Measure the end time
const end = Timestamp.now(io, .awake);
// Calculate the elapsed time in seconds
const elapsed: f64 = @floatFromInt(end.nanoseconds - start.nanoseconds);
const time_spent = elapsed / time.ns_per_s;
// Print the result and time
print("Calculating {d} ^ {d}\n", .{ x, n });
print("Result: {e}\n", .{result});
print("Time taken: {d} seconds\n\n", .{time_spent});
}
}