File tree 2 files changed +48
-2
lines changed
2 files changed +48
-2
lines changed Original file line number Diff line number Diff line change 1
- mod p1790 ;
1
+ mod p1726 ;
2
2
3
3
pub fn main ( ) {
4
- p1790 :: run ( ) ;
4
+ p1726 :: run ( ) ;
5
5
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments