我正在跟踪激磁锈蚀轨迹,遇到了一个问题(我对锈蚀非常陌生)
这是一个计算整数勾股三元组的函数:
use std::collections::HashSet;
use rayon::prelude::*;
pub fn find(sum: u32) -> HashSet<[u32; 3]> {
let a_b_plus_c: Vec<(u32; 2)> = (1_u32..(sum / 3_u32)).into_par_iter()
.filter_map(|a| {
let b_plus_c: u32 = sum - a;
let whole_number_check: Option<u32> = (b_plus_c.pow(2) - a.pow(2)).checked_rem(b_plus_c * 2);
match whole_number_check {
Some(0) => Some((a, b_plus_c)),
Some(_) => None,
None => None,
}
}).collect::<Vec<(u32; 2)>>();
a_b_plus_c.into_par_iter().filter_map(|a, b_plus_c| {
let b: u32 = (b_plus_c.pow(2) - a.pow(2))/(b_plus_c * 2);
let c: u32 = b_plus_c - b;
match b {
b if b > a => [a, b, c]
_ => None,
}}
).collect::<HashSet<[u32; 3]>>();
}
或者更确切地说,如果它有效的话。。。
目前的问题是:
let a_b_plus_c: Vec<(u32; 2)> = (1_u32..(sum / 3_u32)).into_par_iter()
它说,在解析a_b_plus_c
的类型时,它期望有一个符号,但找到了;
。根据我所看到的(不多),这是定义元组向量的正确方法,每个元组都有两个类型为u32
的元素。
正如我所说,这对我来说是一次学习,所以如果有人能帮助我,我将感谢你提供详细的答案:)
值得一提的是,这可能会帮助你评论我的代码,这就是数学:
a + b + c = sum
a² + b² = c²
Rearrange for b:
b = ((b + c)² - a²) / (2(b + c))
So, iterate through a to get b+c, since (b+c) = sum - a
Then solve the above equation to get a, b+c, and b
Confirm that a < b
Then solve for c:
c = (b + c) - b
然后,它应该将它们全部吐出到a,b,c
的数组的HashSet中
您应该在定义中枚举每个元组的元素类型。这应该有效:
let a_b_plus_c: Vec<(u32, u32)> = (1_u32..(sum / 3_u32)).into_par_iter()