Skip to content

Instantly share code, notes, and snippets.

@RandyMcMillan
Forked from rust-play/playground.rs
Last active February 28, 2025 12:39
Show Gist options
  • Save RandyMcMillan/be4c057466e749747fcf5a7ae6217ccc to your computer and use it in GitHub Desktop.
Save RandyMcMillan/be4c057466e749747fcf5a7ae6217ccc to your computer and use it in GitHub Desktop.
index_prime_position.rs
use std::collections::HashMap;
fn is_prime(n: i32) -> bool {
if n <= 1 {
return false;
}
let mut i = 2;
while i * i <= n {
if n % i == 0 {
return false;
}
i += 1;
}
true
}
fn main() {
let mut hashmap: HashMap<i32, (i32, i32)> = HashMap::new();
for count in 0..1000 {
if is_prime(count) {
hashmap.insert(count, (count, count % 13));
} else {
hashmap.insert(count, (0, count % 13));
}
}
let mut sorted_vec: Vec<(&i32, &(i32, i32))> = hashmap.iter().collect();
//(index, (prime, position))
sorted_vec.sort_by_key(|(key, _value)| *key);
for vec in sorted_vec {
println!("{:?}", vec);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment