Skip to content

Instantly share code, notes, and snippets.

View Gadiguibou's full-sized avatar
🐢

Gabriel Lacroix Gadiguibou

🐢
View GitHub Profile
@Gadiguibou
Gadiguibou / lib.rs
Created October 10, 2020 00:47
Combinations in Rust
fn comb<T>(slice: &[T], k: usize) -> Vec<Vec<T>>
where
T: Copy,
{
// If k == 1, return a vector containing a vector for each element of the slice.
if k == 1 {
return slice.iter().map(|x| vec![*x]).collect::<Vec<Vec<T>>>();
}
// If k is exactly the slice length, return the slice inside a vector.
if k == slice.len() {
@Gadiguibou
Gadiguibou / lib.rs
Created October 10, 2020 04:15
Factorial Trailing Zeros in Arbitrary Base
use std::collections::HashMap;
fn prime_factors(number: i32) -> HashMap<i32, i32> {
let mut prime_factors: HashMap<i32, i32> = HashMap::new();
let mut candidate_factor = 2;
let mut n = number;
while n > 1 {
while n % candidate_factor == 0 {
if let Some(count) = prime_factors.get_mut(&candidate_factor) {