Skip to content

Commit 21baab6

Browse files
committed
p1726
1 parent dddf826 commit 21baab6

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
mod p1790;
1+
mod p1726;
22

33
pub fn main() {
4-
p1790::run();
4+
p1726::run();
55
}

src/p1726.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use std::collections::HashMap;
2+
3+
pub fn run() {
4+
for i in [
5+
vec![2,3,4,6],
6+
vec![1,2,4,5,10]
7+
] {
8+
println!("{}", tuple_same_product(i));
9+
}
10+
}
11+
12+
pub fn tuple_same_product(nums: Vec<i32>) -> i32 {
13+
let mut pairs: HashMap<i32, i32> = HashMap::new();
14+
15+
for n in 0..nums.len() {
16+
for i in &nums[(n + 1)..nums.len()] {
17+
let p = nums[n] * *i;
18+
match pairs.get_mut(&p) {
19+
Some(m) => {
20+
*m += 1;
21+
},
22+
None => {
23+
pairs.insert(p, 1);
24+
}
25+
}
26+
}
27+
}
28+
29+
let mut count = 0;
30+
31+
for (_, v) in pairs {
32+
if v <= 1 {
33+
continue;
34+
}
35+
36+
count += 8 * compounded_sum(v);
37+
}
38+
39+
count
40+
}
41+
42+
pub fn compounded_sum(i: i32) -> i32 {
43+
// area of triangle
44+
let i = i - 1;
45+
((i * i) + i) / 2
46+
}

0 commit comments

Comments
 (0)