コード例 #1
0
ファイル: Problem68.cs プロジェクト: andyhuey/euler
 public bool isRotationOf(FiveGonRing ring)
 {
     // I'm sure there's a much better way of doing this...
     // (and no longer technically needed.)
     int[] t1 = new int[] { 0, 1, 2, 3, 4 };
     // 0,1,2,3,4 -> 1,2,3,4,0
     if (this.checkSeqEq(t1, new int[] { 1, 2, 3, 4, 0 }, ring))
     {
         return(true);
     }
     // 0,1,2,3,4 -> 2,3,4,0,1
     if (this.checkSeqEq(t1, new int[] { 2, 3, 4, 0, 1 }, ring))
     {
         return(true);
     }
     // 0,1,2,3,4 -> 3,4,0,1,2
     if (this.checkSeqEq(t1, new int[] { 3, 4, 0, 1, 2 }, ring))
     {
         return(true);
     }
     // 0,1,2,3,4 -> 4,0,1,2,3
     if (this.checkSeqEq(t1, new int[] { 4, 0, 1, 2, 3 }, ring))
     {
         return(true);
     }
     // otherwise:
     return(false);
 }
コード例 #2
0
ファイル: Problem68.cs プロジェクト: andyhuey/euler
 private bool checkSeqEq(int[] t1, int[] t2, FiveGonRing ring)
 {
     Debug.Assert(t1.Length == t2.Length);
     for (int i = 0; i < t1.Length; i++)
     {
         if (!this.getTuple(t1[i]).SequenceEqual(ring.getTuple(t2[i])))
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #3
0
ファイル: Problem68.cs プロジェクト: andyhuey/euler
        public long soln1()
        {
            var sw = Stopwatch.StartNew();

            List <int>         p1         = new List <int>();
            int                nPerms     = 0;
            int                nMagic     = 0;
            List <FiveGonRing> magicRings = new List <FiveGonRing>();

            //Console.WriteLine("There are {0:n0} permutations to consider.", getAllPerms(p1).Count());

            foreach (var p in getAllPerms(p1))
            {
                //Console.WriteLine(string.Join<int>(", ", p));
                var ring = new FiveGonRing(p.ToArray());
                Debug.Assert(ring.isValidRing(), string.Format("{0} is not a valid ring.", ring.ToString()));

                int s = ring.getMagicRingSum();
                if (s > 0)
                {
                    if (!magicRings.Any(r => ring.isRotationOf(r)))
                    {
                        //Console.WriteLine("{0} is a magic ring, with sum {1}.", ring.ToString(), s);
                        nMagic++;
                        magicRings.Add(ring);
                    }
                }
                nPerms++;
            }

            // get rid of any rings where the length of the digit string isn't 16.
            magicRings.RemoveAll(x => x.getDigitString().Length != 16);

            foreach (var ring in magicRings.OrderBy(x => x.getMagicRingSum()))
            {
                Console.WriteLine("{0} is a magic ring, with sum {1}.", ring.ToString(), ring.getMagicRingSum());
            }

            Console.WriteLine("{0} permutations generated.", nPerms);
            Console.WriteLine("{0} magic rings generated.", nMagic);

            Console.WriteLine("The max digit string is {0}.", magicRings.Max(x => x.getDigitString()));

            sw.Stop();
            Console.WriteLine("elapsed: {0} ms", sw.Elapsed.TotalMilliseconds);

            return(0);
        }