Problem Description
The cube, 41063625 (3453), can be permuted to produce two other cubes: 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest cube which has exactly three permutations of its digits which are also cube.
Find the smallest cube for which exactly five permutations of its digits are cube.
Analysis
Well after establishing that 10,000 is as far as we need to go, we brute force a search by cubing all the counting numbers in the loop’s range and sorting the digits in ascending order to use as a hash key. Then we increment a counter, %sd, and assign the first cube to our %cubes
Solution
my $limit = 10_000; my $min_cube = 9e20; for (my $x=100; $x<$limit; $x++, $c=$x**3) { $cs = join('', sort split //,$c); $sd{$cs}++; $cubes{$cs}||=$c; } $min_cube = min_n($min_cube, $cubes{$_}) for grep {$sd{$_}==5} keys %sd; print "Answer to PE62 = $min_cube";
Comments
The Perl short circuit assignment operator, ||=, simply assigns a value to a variable only if that variable is undefined or zero. Just a way to make sure the lowest value is set (since the loop is ascending) and never replaced.
The series of cube roots are: 5027 7061 7202 8288 8384





Discussion
No comments for “Project Euler Problem 62 Solution”