Created
March 26, 2024 17:23
-
-
Save kcinnu/80b2fabae9675067b5d94a57f457e423 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
pub fn main() !void { | |
const limit = 100; | |
// check for squares with diagonal pythagorean triples | |
// doesnt deduplicate symmetric solutions | |
// for (1..limit) |a1_| { | |
// if (a1_ % 100 == 0) std.debug.print("progress: {d}\n", .{a1_}); | |
// const a1: i32 = @intCast(a1_); | |
// for (1..limit) |c1_| { | |
// const c1: i32 = @intCast(c1_); | |
// for (pyth(a1, c1).slice()) |b1| { | |
// for (1..limit) |a3_| { | |
// const a3: i32 = @intCast(a3_); | |
// for (pyth(a1, a3).slice()) |a2| { | |
// for (pyth(c1, a3).slice()) |b2| { | |
// for (pyth(a2, b2).slice()) |c2| { | |
// for (pyth(b1, b2).slice()) |b3| { | |
// for (pyth(c1, c2).slice()) |c3| { | |
// if (!ispyth(a1, b2, c3)) continue; | |
// if (!ispyth(a3, b3, c3)) continue; | |
// std.debug.print("{d:>6} {d:>6} {d:>6}\n{d:>6} {d:>6} {d:>6}\n{d:>6} {d:>6} {d:>6}\n\n", .{ a1, b1, c1, a2, b2, c2, a3, b3, c3 }); | |
// } | |
// } | |
// } | |
// } | |
// } | |
// } | |
// } | |
// } | |
// } | |
// check for squares without caring about diagonal pythagorean triples | |
// doesnt deduplicate symmetric solutions | |
for (1..limit) |a1_| { | |
// if (a1_ % 100 == 0) std.debug.print("progress: {d}\n", .{a1_}); | |
const a1: i32 = @intCast(a1_); | |
for (1..limit) |c1_| { | |
const c1: i32 = @intCast(c1_); | |
if (in(c1, .{a1})) continue; | |
for (pyth(a1, c1).slice()) |b1| { | |
if (in(b1, .{ a1, c1 })) continue; | |
for (1..limit) |a3_| { | |
const a3: i32 = @intCast(a3_); | |
if (in(a3, .{ a1, c1, b1 })) continue; | |
for (pyth(a1, a3).slice()) |a2| { | |
if (in(a2, .{ a1, c1, b1, a3 })) continue; | |
for (1..limit) |c3_| { | |
const c3: i32 = @intCast(c3_); | |
if (in(c3, .{ a1, c1, b1, a3, a2 })) continue; | |
for (pyth(a3, c3).slice()) |b3| { | |
if (in(b3, .{ a1, c1, b1, a3, a2, c3 })) continue; | |
for (pyth(c1, c3).slice()) |c2| { | |
if (in(c2, .{ a1, c1, b1, a3, a2, c3, b3 })) continue; | |
for (pyth(a2, c2).slice()) |b2| { | |
if (in(b2, .{ a1, c1, b1, a3, a2, c3, b3, c2 })) continue; | |
if (!ispyth(b1, b2, b3)) continue; | |
std.debug.print("{d:>6} {d:>6} {d:>6}\n{d:>6} {d:>6} {d:>6}\n{d:>6} {d:>6} {d:>6}\n\n", .{ a1, b1, c1, a2, b2, c2, a3, b3, c3 }); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
fn in(x: anytype, y: anytype) bool { | |
inline for (y) |v| { | |
if (v == x) return true; | |
} | |
return false; | |
} | |
fn pyth(x: i32, y: i32) std.BoundedArray(i32, 3) { | |
var res = std.BoundedArray(i32, 3){}; | |
inline for (.{ .{ 1, 1 }, .{ -1, 1 }, .{ 1, -1 } }) |sgn| { | |
const tmp = @sqrt(@as(f64, @floatFromInt(x * x * sgn[0] + y * y * sgn[1]))); | |
if (@floor(tmp) == tmp and tmp != 0) res.append(@intFromFloat(tmp)) catch unreachable; | |
} | |
return res; | |
} | |
fn ispyth(x: i32, y: i32, z: i32) bool { | |
inline for (.{ .{ 1, 1, -1 }, .{ 1, -1, 1 }, .{ -1, 1, 1 } }) |sgn| { | |
if (x * x * sgn[0] + y * y * sgn[1] + z * z * sgn[2] == 0) return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment