Currently I'm working on Euler Problem 62, Permutations of a cube, the program is searching for a cube that 5 of the permutations are other cubes, I did some online research and found that the cube that im searching for is within the range of an int64, Actually its 12 digits long, however when ever i test the program isnt producing the correct answer. The way i went about solving this problem is as so
step 1- Generating all cubes within a range of 1 digit... //ie 10, 100, 1000, 10000
step 2- I digitize each cube and sort their digits in order
step 3- Next I Reform the sorted digits into number, and place them in an array // (note number is stores in reverse numerical order to prevent leading 0's)
step 4- I loop through the array comparing all of the numbers to and counting them to obtain the most with the same digit //meaning cube is a permutation of another
the code works tested up to 8 digits fine However after there it seems to stop counting correctly, I figured it might have to do with the max size of the int so I changed all variables to a 64 bit integer

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Eu62CubePerm
{
class Cube
{
List<Int64> Cubes;
int size;
public Cube(int Digits)
{
size = Digits;
Cubes = new List<Int64>();
Int64 Base = (Int64)Math.Pow(10, Digits - 1);
Int64 SP = (Int64)Math.Pow(Base, (1.0 / 3.0));
Int64 Q = (Int64)Math.Pow(SP, 3);
for (SP++; Q < Base * 10; SP++)
{
if(Q>=Base)
Q = (Int64)Math.Pow(SP, 3);

}
}
public void Display()
{
foreach (Int64 QB in Cubes)
{
Console.Write(QB + " ");
}
}

public Int64 PermCube ()
{
Int64 [] Digits = new Int64 [Cubes.Count];
int Co = 0;
int MostC = 0;
Int64 LNum = 0;
foreach (Int64 N in Cubes)
{
Int64 Temp = N;
int [] T = new int [size];
for (int Us = size-1; Us >= 0; Us--, Temp/=10)
{
T[Us] = (int)Temp % 10;
}
Array.Sort(T);
for (int x = size-1, c = 1; x >= 0; x--, c++)
{
Digits[Co] +=(T[x]*(Int64)Math.Pow(10, size-c));
}

//Console.WriteLine("#{0}: {1} ", Co ,Digits[Co]);
Co++;
}
for ( int x = 0; x < Co; x++)
{
if (Digits[x] > 0)
{
Int64 Temp = Digits[x];
int Tco = 1;
for (int y = x + 1; y < Co; y++)
{
if (Temp == Digits[y])
{
Tco++;
Digits[y] = 0;
}
}
if (Tco > MostC)
{
MostC = Tco;
LNum = Cubes[x];
}
}
}
Console.WriteLine("Tco: {0}, Cube {1}", MostC, LNum);
return 0;
}

}
class Program
{
static void Main(string[] args)
{
for (int x = 1; x < 15; x++)
{
Cube Test = new Cube(x);
Test.PermCube();
}
}
}
}
``````

There is no 4 digit cube that has 5 permutations of the numbers that are also cubes

That's not what I said. You commented that reading is important but didn't bother to read what I said. Since I have answered that question on project euler, I know what …

## All 4 Replies

You do some odd things with loops that just make your logic more complicated. Lines 45-48 you get the digits 'in order' into the array, then in line 49 you sort the array. Who cares about the order, use the simplest method.

In lines 50-53 you do some convoluted math to build the number back in high digit first order, but take a look at the values of x and size - c while it is running. You'll notice something magic :)

Your counting method in lines 58-78 is poorly done and has a Big-O of n^2. Sort the array and find the change points. The difference in the change points is the number of numbers of that value. For example, if you had an array with the values [1, 1, 2, 2, 2, 3, 3] the changes points for 2 are 2 and 5, 5-2 = 3, which is the number of 2's (counting from zero, of course, as all right thinking programmers do). You can do this in a Big-O of n, which is much much faster.

I'm not sure where your logic error is, but the number you are looking for is a cube of a four digit number, you should find it in about 1 second or so.

You may find my method bizarre but if you read the beginning of the post you would understand why it occurs, BigO notation doesn't mean a thing, when your dealing with a small sample size, and if you base it off of the amount of permutations you would have to do otherwize my method is very efficient.

I am finding a cube that can be permuted to equal 5 other cubes
Therefore after all the cubes are generated,
you sort the digits in the number so if one cube is a permutation of another it will still
appear in as the same ordered number.

Then I reform the number in Reverse so if a sorted number is 00356, it appears as 65300, instead of 356.

I am looking for a cube of a 12 digit number as I specified before. There is no 4 digit cube that has 5 permutations of the numbers that are also cubes

commented: Yes it is, you should try it sometime +0

There is no 4 digit cube that has 5 permutations of the numbers that are also cubes

That's not what I said. You commented that reading is important but didn't bother to read what I said. Since I have answered that question on project euler, I know what the answer is, and it is the cube of a four digit number. The cube is permutated, not the four digit number.

my method is very efficient.

No it isn't. You make very little use of the built in functions, there are better ways to sort the digits, your checking loop is probably the worst way you could do it. The whole problem can be solved in less than 15 lines of code.

commented: point taken +0

However, my objective is not to code like others but, to code the only way I know how to do, Telling me how my code is inefficient does not solve my problem it only creates more, maybe some insite on a better way to solve it would work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.