Exemplo n.º 1
0
        /**
         * The cube, 41063625 (345^3), can be permuted to produce two other cubes: 56623104 (384^3) and 66430125 (405^3).
         * 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.
         */
        public void Problem62()
        {
            var cubes = new HashSet<String>();

            var maxCubeRoot = new BigInteger(10000);

            for (var c = new BigInteger(1); c <= maxCubeRoot; c++)
            {
                cubes.Add(_lib.ToThePower(c, 3).ToString());
            }

            var maxCube = _lib.ToThePower(maxCubeRoot, 3);

            Console.Out.WriteLine("Number of Cubes: {0}, Max Cube: {1}", cubes.Count, maxCube);
            Console.Out.WriteLine();

            var sortedCubes = new Dictionary<String, List<String>>();

            foreach (var cube in cubes)
            {
                var c = cube.ToCharArray().ToList().Select(e => int.Parse(e.ToString(CultureInfo.InvariantCulture))).ToList();
                c.Sort();

                var s = "";

                foreach (var i in c)
                {
                    s += i.ToString(CultureInfo.InvariantCulture);
                }

                if (sortedCubes.ContainsKey(s))
                {
                    sortedCubes[s].Add(cube);
                }
                else
                {
                    sortedCubes.Add(s, new List<string> { cube });
                }

            }

            foreach (var sortedCube in sortedCubes)
            {
                if (sortedCube.Value.Count == 5)
                {
                    Console.Out.WriteLine("base Key: {0}", sortedCube.Key);
                    Console.Out.WriteLine();
                    Console.Out.WriteLine("Sorted Big Integers : ");
                    Console.Out.WriteLine();

                    var cus = new List<BigInteger>();

                    sortedCube.Value.ForEach(x => cus.Add(BigInteger.Parse(x)));

                    cus.Sort();

                    cus.ForEach(e => Console.Out.WriteLine(e));

                    Console.Out.WriteLine();

                    for (var c = new BigInteger(1); c <= maxCubeRoot; c++)
                    {
                        if (_lib.ToThePower(c, 3).Equals(cus[0]))
                        {
                            Console.Out.WriteLine("Cube root is : {0}", c);
                        }
                    }
                    //answer is 127035954683

                    Console.Out.WriteLine();

                }
            }
        }