Rust,元组向量的类型定义

时间:2019-01-23 14:14:00

标签: arrays syntax rust

我正在跟踪准分子锈病的踪迹,但遇到了一个问题(我对锈病非常陌生)

这是用于计算整数的勾股三元组的函数:

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数组中

0 个答案:

没有答案