Skip to content

Instantly share code, notes, and snippets.

@bcho
Created October 1, 2014 11:31
Show Gist options
  • Select an option

  • Save bcho/01dd907c00e4870052e8 to your computer and use it in GitHub Desktop.

Select an option

Save bcho/01dd907c00e4870052e8 to your computer and use it in GitHub Desktop.
use std::collections::HashMap;
use std::string::String;
fn main() {
let mut candidates: HashMap<String, int> = HashMap::new();
let pow = |num: int| { num * num * num };
let sorted_str = |s: String| {
let mut v = Vec::new();
for i in s.as_slice().chars() {
v.push(i);
}
v.sort_by(|a, b| { a.cmp(b) });
String::from_chars(v.as_slice())
};
let hash = |num: int| {
sorted_str(num.to_string())
};
for num in range(1, 10000) {
let num_pow = pow(num);
let num_hashed = hash(num_pow);
let count = match candidates.find(&num_hashed) {
Some(old) => old + 1,
None => 1
};
candidates.insert(num_hashed, count);
}
for num in range(1, 10000) {
let num_pow = pow(num);
let num_hashed = hash(num_pow);
match candidates.find(&num_hashed) {
Some(count) => {
if *count == 5 {
println!("{}", num);
break;
}
}
_ => continue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment