Algorithm requirements:
For an array, such as [12,15], first the mass factorization of each element,
For example, 12 =2*2*3, with a mass factor of 2, 3.
15 =3*5, with a mass factor of 3, 5.
The array has different mass factor 2,3,5.
Then, according to the order from small to large: 2 of the quality factor has 12;3 of the quality factor has: 12, 15;5 The quality factor has: 15.
You can output [(2,12), (3,12+15), (5,15)] => simplification [(2,12), (3,27), (5,15)].
Note that the mass factor of the negative integer is the same as the mass factor of the corresponding positive integer.
First, my solution
Use Std::collections::{hashmap, hashset}; fn sum_of_divided (l:vec<i64>)-> vec< (i64, i64) > {//Your code//10-6//let mut
t; (i64, i64) > = Vec::new (); 2: There are 2 quality factors and, 3: There are 3 quality factors and println!
("l:{:?}", L);
Let Mut data:hashmap<i64, vec<i64>> = Hashmap::new (); L.clone (). Iter_mut (). Map (|x| {prime_factors (*x). ABS ())-ITER (). Map (|w|
{if Data.contains_key (w) {Data.get_mut (W). Unwrap (). push (*X); else {Data.insert (*w, vec![
*X]);
} W}) .collect::<vec<_>> (). Len () as i64
}) .collect::<vec<i64>> (); Let result:hashmap<i64, i64> = Data.clone (). Into_iter (). Map (| ( K, V) |
(K,v.iter (). SUM ())
. Collect (); Sort_vec (reSult)} fn Sort_vec (data:hashmap<i64, i64>)-> vec< (i64, i64) > {//let mut output:vec< (i64, i64) &G T
= Vec::new ();
Let Mut ky:vec<i64> = Data.keys (). Into_iter (). Map (|&x| x). Collect ();
Ky.sort (); Ky.into_iter (). Map (|x| (x, * (Data.get (&x). Unwrap ())). collect::<vec< (i64, i64) >> ()} fn is_prime (n:i64)-> bool {! (
2. (n). ABS ()). Any (|x| n% x = = 0)} fn prime_factors (n:i64)-> vec<i64> {//Your code if Is_prime (n) { Return vec!
[n];
Let mut m = N.abs ();
Let Mut output:vec<i64> = Vec::new ();
For I-2..n.abs () + 1 {if m < I {break;
} if!is_prime (i) {continue;
While m% i = = 0i64 {if!output.contains (&i) {Output.push (i);
} m = m/i;
} output.sort (); Output//println!
("output:{:?}", output); }
Second, the wonderful solution
1,
Use Std::collections::{btreeset, hashset};
fn prime_factors (mut n:i64)-> hashset<i64> {Let
mut i = 2;
Let Mut res = Hashset::new ();
While I <= n/i {while
n% i = = 0 {
Res.insert (i);
n/= i;
}
i + 1;
}
If n > 1 {
res.insert (n);
}
Res
}
fn sum_of_divided (l:vec<i64>)-> vec< (i64, i64) > {
l.iter ()
. Flat_map (| &x| Prime_factors (X.abs ())
.collect::<btreeset<_>> (). Into_iter ().
Map (|p:i64| (P, L.iter (). cloned (). filter (|x| x p = = 0). sum ()). Collect ()
}
2,
fn Is_prime (&x: &i64)-> bool {
(2 ...). Take_while (|p| p*p <= x). All (|p| x P!= 0)
}
fn sum_of_divided (l:vec<i64>)-> vec< (i64, i64) > {let
mx = l.iter (). Map (|&x| x.abs ()). Max (). Unwrap_or (0);
(2.)
. Take_while (|&p| p <= mx)
. Filter (is_prime).
Filter_map (|p| {Let
mut nums = L.iter (). cloned (). Filter (|x| x.abs ()% P = 0). peekable ();
Nums.peek (). cloned (). Map (|_| (p, {nums}.sum ())
})
. Collect ()
}
3,
fn sum_of_divided (l:vec<i64>)-> vec< (i64, i64) > {if L.len () = = 0 {return vec![]}
Let Max = L.iter (). Max_by_key (|x| x.abs ()). Unwrap (). ABS (); (2i64.). Filter (|&x| is_prime (x)). Take_while (|&x| x <= max). Fold (Vec::new (), |mut acc, prime| {if L.iter (). Filter (|&i| i% prime = 0). Count () > 0 {acc.push (Prime, L.iter (). Filter (|& i|
I% Prime = = 0). sum ());
"ACC})} fn Is_prime (n:i64)-> bool {if n = 2 {return true;}
If n < 3 {return false;}
If n% 2 = 0 {return false;}
Let Sqrt_limit = (n as f64). sqrt () as i64;
Let mut i:i64 = 3;
While I <= sqrt_limit {if n% i = = 0 {return false;}
i + 2; } true}