Skip to content

Instantly share code, notes, and snippets.

@likev
Created May 24, 2024 11:07
Show Gist options
  • Save likev/50784f4467dbaa26e4d77d75aea8aa4e to your computer and use it in GitHub Desktop.
Save likev/50784f4467dbaa26e4d77d75aea8aa4e to your computer and use it in GitHub Desktop.
function ABMN(AP, PB, AR, RC) {
let a1 = AP + PB,
a2 = AR + RC,
k1 = PB / a1,
k2 = RC / a2,
d = a1 ** 2 - a2 ** 2;
let M = ((k1 * a1) ** 2 - (k2 * a2) ** 2 - (k1 + k2) * d / 2) / (k1 - k2) / 2;
let A = (d + 4 * M) / a1 / 4,
B = (4 * M - d) / a2 / 4;
let N = (k2 * a2) ** 2 + M - 2 * k2 * a2 * B;
let result = {
A,
B,
M,
N
};
return result;
}
function check_PQR_90(PB, RC, M, N) {
let cos_PQB = (N + M - PB ** 2) / (M * N) ** 0.5 / 2,
cos_RQC = (N + M - RC ** 2) / (M * N) ** 0.5 / 2;
let result = cos_PQB ** 2 + cos_RQC ** 2;
return result - 1 < 1E-8;
}
function find_ABC(size=10, try_times=1E6) {
let rand1_n = (n=100) => Math.ceil(Math.random() * n);
let check_int = (a, e=1E-8)=> Math.abs(Math.round(a)-a)<e;
const records = new Set();
for (let i = 0; i < try_times; i++) {
let AP = rand1_n(20),
PB = rand1_n(20),
AR = rand1_n(20),
RC = rand1_n(20);
var {
A,
B,
M,
N
} = ABMN(AP, PB, AR, RC);
if (check_PQR_90(PB, RC, M, N)) {
let id = `${AP}-${PB}-${AR}-${RC}`;
if(records.has(id)) continue;
let m = M**0.5, n = N**0.5;
if(check_int(N)){//only output int value
records.add(id)
console.log({
AP,
PB,
AR,
RC,
m,
n,
M,
N
})
}
}
if(records.size >= size ) break;
}
}
find_ABC()
Object { AP: 3, PB: 14, AR: 5, RC: 10, m: 12.409673645990857, n: 8.366600265340756, M: 154, N: 70 }
Object { AP: 5, PB: 7, AR: 3, RC: 9, m: 9.797958971132713, n: 5.744562646538029, M: 96.00000000000003, N: 33 }
Object { AP: 2, PB: 20, AR: 9, RC: 9, m: 15.878007151752739, n: 10.999999999999998, M: 252.11111111111111, N: 120.99999999999997 }
Object { AP: 7, PB: 18, AR: 9, RC: 6, m: 9.999999999999998, n: 11.661903789690601, M: 99.99999999999997, N: 136 }
Object { AP: 10, PB: 6, AR: 8, RC: 12, m: 9.16515138991168, n: 9.16515138991168, M: 84, N: 84 }
Object { AP: 14, PB: 3, AR: 10, RC: 15, m: 10.049875621120892, n: 10.198039027185569, M: 101.00000000000003, N: 103.99999999999997 }
Object { AP: 9, PB: 11, AR: 10, RC: 8, m: 9.539392014169453, n: 9.539392014169456, M: 90.99999999999993, N: 91 }
Object { AP: 7, PB: 20, AR: 19, RC: 8, m: 19.44222209522358, n: 14.7648230602334, M: 378, N: 218 }
Object { AP: 7, PB: 7, AR: 1, RC: 11, m: 11.428035701729321, n: 6.000000000000003, M: 130.6, N: 36.00000000000003 }
Object { AP: 3, PB: 19, AR: 13, RC: 13, m: 21.071307505705477, n: 11, M: 444, N: 121 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment