Created
October 1, 2014 11:31
-
-
Save bcho/01dd907c00e4870052e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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