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)
Cubes.Add(Q);
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();
}
System.Threading.Thread.Sleep(30000);
}
}
}