Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

cubic
permutations
python

What are cubic permutations?

Rukhshan Haroon

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.

%0 node_1 012334556789 node_3 127035954683 node_1->node_3 node_4 352045367981 node_1->node_4 node_1624876314658 cube(5027) node_3->node_1624876314658 node_1624876404782 cube(7061) node_4->node_1624876404782
An example of cubic multiples.

Problem

Your task is to find the five smallest positive integers that result in cubic permutations of one another when cubed.

Solution

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 = 0
	count = 0
	while(1):
		cube1 = x*x*x
		temp = []
		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 += 1
			if count == 5:
				return
		x=x+1

def find_permutation():
	hashmap = {}
	permutation = 0
	x = 1
	while(1):
		cube1 = x*x*x
		temp = []
		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] += 1
		else:
			hashmap[cube2] = 1
		if 5 in hashmap.values():
			for key, value in hashmap.items():
				if 5 == value:
					permutation = key
					break
			break
		x = x+1
	find_roots(permutation)
	
def main():
	print("The 5 smallest positive integers that, when cubed, result into cubic permutations of one another are:")
	find_permutation()
main()
Solution implemented using Python3.

RELATED TAGS

cubic
permutations
python

CONTRIBUTOR

Rukhshan Haroon
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring