Cubic permutations are cubic numbers that are made up of the same digits.
For example, the cube of 5027 is 127035954683, which can be permuted to obtain the cube of 7061, 352045367981. In this way, these two cube numbers are cubic permutations of one another.
Your task is to find the five smallest positive integers that result in cubic permutations of one another when cubed.
Here is the solution to this problem. We define two functions to determine the five smallest (in numerical value) cubic numbers that are permutations of one another and their cube roots.
In the find_permutation
function, we declare a dictionary named hashmap
, and an infinite while
loop which terminates only when the first five cubic permutations are found, such that they are permutations of one another.
To bootstrap our program, we declare an integer named number
with value 0. With each iteration of the loop, this number is incremented and used to obtain the next cubic number.
The dictionary hashmap
stores the unique and sorted permutation of cubic numbers as its key and the occurrence of the corresponding permutation as its value. After a cubic number has been calculated, the program checks whether or not the sorted permutation of this number is present in the hashmap
. In case it is present, the corresponding value in the key-value pair is incremented by one; otherwise, a new key-value pair is added to hashmap
. Its value is set to 1 by default, as this occurrence has to be the first one.
Taking the example given above, the occurrence of 127035954683 will result in the following key-value pair in hashmap
: <012334556789 , 1>. The occurrence of 352045367981 will result in the value of the aforementioned key-value pair being updated. It will be incremented by 1, as both cubic numbers will result in the same sorted permutation. The updated key-value pair will be as follows: <012334556789 , 2>.
After each iteration, the program checks if any value in hashmap
is equal to five. Since Python dictionaries may be used as hash-tables, we can perform hashing to insert or search key value pairs in hashmap
. Finally, the find_roots
function is used to find and print the corresponding cube-roots of the first five cubic numbers that were permutations of each other.
def find_roots(permutation):x = 0count = 0while(1):cube1 = x*x*xtemp = []for i in range(len(str(cube1))):temp.append(int(str(cube1)[i]))temp.sort()s = [str(i) for i in temp]cube2 = "".join(s)if (cube2==permutation):print(x)count += 1if count == 5:returnx=x+1def find_permutation():hashmap = {}permutation = 0x = 1while(1):cube1 = x*x*xtemp = []for i in range(len(str(cube1))):temp.append(int(str(cube1)[i]))temp.sort()s = [str(i) for i in temp]cube2 = "".join(s)ret_val = cube2 in hashmap.keys()if ret_val == True:hashmap[cube2] += 1else:hashmap[cube2] = 1if 5 in hashmap.values():for key, value in hashmap.items():if 5 == value:permutation = keybreakbreakx = x+1find_roots(permutation)def main():print("The 5 smallest positive integers that, when cubed, result into cubic permutations of one another are:")find_permutation()main()
Free Resources