init
This commit is contained in:
6
problems/p39/Cargo.toml
Normal file
6
problems/p39/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "p39"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
54
problems/p39/src/main.rs
Normal file
54
problems/p39/src/main.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
// 整数边长直角三角形
|
||||
// 考虑三边长$\{a,b,c\}$均为整数的直角三角形,并记其周长为$p$,当$p = 120$时,恰好存在三个不同的解:
|
||||
//
|
||||
// $$\{20,48,52\}, \{24,45,51\}, \{30,40,50\}$$
|
||||
//
|
||||
// 在所有的$p \le 1000$中,$p$取何值时有最多个解?
|
||||
|
||||
// 直角三角形有勾股定理
|
||||
// 可转换为方程的整数解
|
||||
// c^2 = a^2 + b^2
|
||||
// a + b + c = p
|
||||
// a, b, c 属于 Z
|
||||
// a + b > c
|
||||
// a + c > b
|
||||
// b + c > a
|
||||
//
|
||||
// 带入,2c < p
|
||||
// c < p / 2
|
||||
// c > p / 3
|
||||
// 遍历c ,分解c^2 = a^2 + b^2
|
||||
// 分解可使用暴力法或Cornacchia算法
|
||||
// 这里数字较小,可以使用暴力法
|
||||
|
||||
fn two_square(c_pow2: u32) -> Option<(u32, u32)> {
|
||||
if c_pow2 % 4 == 3 {
|
||||
return None;
|
||||
}
|
||||
for a in (1..c_pow2.isqrt()).rev() {
|
||||
let b = (c_pow2 - a * a).isqrt();
|
||||
if b * b + a * a == c_pow2 {
|
||||
return Some((a, b));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut solve_counter = vec![0; 1000];
|
||||
for p in 5..1000 {
|
||||
for c in p / 3..p / 2 {
|
||||
match two_square(c * c) {
|
||||
Some(_) => solve_counter[p as usize] += 1,
|
||||
None => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
let max_index = solve_counter
|
||||
.iter()
|
||||
.enumerate()
|
||||
.max_by_key(|&(_, value)| value)
|
||||
.unwrap()
|
||||
.0;
|
||||
println!("{max_index}");
|
||||
}
|
||||
Reference in New Issue
Block a user