Monday, March 25, 2013

project euler problem 062

Cubic permutations

I first used brute force method, and it took 3.5s to get the result. Then I revised my code from O(n^2) to O(n). The run time reduce to 0.1s. 

The key of hashtable is important. I found two ways for the key. The first way is to convert the number to string and then sort the string characters in order. A second approach is found from INTERNET, using a long integer, each 6 bit is used as count for a digit (0-9). The maximum count is 2^6 = 64 which is much larger than the number of digits for n^3. Smart!

No comments:

Post a Comment