Exemplo n.º 1
0
        public Problem0025()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 2;

            // Solution:
            BigInteger number1 = 1;
            BigInteger number2 = 1;
            BigInteger help;

            do
            {
                help     = number2;
                number2 += number1;
                number1  = help;
                result++;
            } while (number2.ToString().Length < 1000);


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    25, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 2
0
        public Problem0002()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 0;
            long n      = 10000;

            // Solution:
            long fibo1 = 1;
            long fibo2 = 1;

            while (fibo1 < n)
            {
                if ((fibo1 = fibo2 + (fibo2 = fibo1)) % 2 == 0 && fibo1 < n)
                {
                    result += fibo1;
                }
            }

            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(2, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 3
0
        public Problem0039()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 0;

            // Solution:
            var maxSolutions = 0;

            for (var i = 1000; i > 2; i--)
            {
                var solutions = numberOfSolutions(i);
                if (solutions > maxSolutions)
                {
                    maxSolutions = solutions;
                    result       = i;
                }
            }


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    39, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 4
0
        public Problem0042()
        {
            var words     = readWords();
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            ulong result = 0;

            // Solution:
            var triangles = generateTriangles(15 * 26);

            foreach (var word in words)
            {
                if (triangles.Contains((int)wordValue(word)))
                {
                    result++;
                }
            }


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    42, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 5
0
        public Problem0018()
        {
            var arr = Fill();

            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 0;

            // Solution:
            for (var i = arr.Length - 1; i > 0; i--)
            {
                for (var j = 0; j < arr[i].Length - 1; j++)
                {
                    arr[i - 1][j] += Math.Max(arr[i][j], arr[i][1 + j]);
                }
            }

            result = arr[0][0];

            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    18, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 6
0
        public Problem0005()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            ulong result = 1;

            // Solution:
            ulong[] primes = { 3, 5, 7, 11, 13, 17, 19 };
            foreach (var p in primes)
            {
                result *= p * (20 / p);
            }


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(5, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 7
0
        public Problem0006()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            ulong result = 0;

            // Solution:
            ulong a = 0;
            ulong b = 0;

            for (ulong i = 1; i < 101; i++)
            {
                a += i;
                b += i * i;
            }

            result = a * a - b;


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(6, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 8
0
        public Problem0056()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            BigInteger result = 0;

            // Solution:
            for (var a = 2; a < 100; a++)
            {
                for (var b = 2; b < 100; b++)
                {
                    var dSum = DigitSum(BigInteger.Pow(a, b));
                    if (dSum > result)
                    {
                        result = dSum;
                    }
                }
            }


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    56, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 9
0
        public Problem0029()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 0;

            // Solution:
            var set = new HashSet <BigInteger>();

            for (var i = 2; i <= 100; i++)
            {
                for (var j = 2; j <= 100; j++)
                {
                    set.Add(BigInteger.Pow(j, i));
                }
            }

            result = set.Count;


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    29, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 10
0
        public Problem0022()
        {
            var names     = File.ReadAllText(@"resources\problem0022_names.txt").Replace("\"", "").Split(",");
            var stopWatch = new Stopwatch();

            stopWatch.Start();

            // Solution:
            Array.Sort(names);
            var result = names.Select(t => t.Aggregate <char, long>(0, (current, c) => current + (c - 64)))
                         .Select((score, i) => score * (i + 1)).Sum();


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    22, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 11
0
        public Problem0001()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 0;

            // Solution:
            long n = 1000;

            result = sum(--n, 3) + sum(n, 5) - sum(n, 15);

            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(1, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 12
0
        public Problem0040()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 0;

            // Solution:
            var concatenated = concatenate(1_000_000);
            var n            = 1;

            result = concatenated[n] - 48;
            for (var i = 0; i < 6; i++)
            {
                n       = int.Parse(n + "0");
                result *= concatenated[n] - 48;
            }


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    40, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 13
0
        public Problem0706()
        {
            var stopWatch = new Stopwatch();
            stopWatch.Start();
            BigInteger result = 0;

            // Solution:
            result = BigF(100_001) % 1_000_000_007;

            Console.WriteLine(
                (3 * 1150500 / 2267
                 + BigInteger.Parse("9" + new string('0', 99999)))
                * 2267 / 6750 % 1_000_000_007
            );


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();
            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                                     " ms"));
            if (ProblemTest.DoBenchmark)
                ProblemBenchmark.AddTime(
                    706, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
        }
Exemplo n.º 14
0
        public Problem0684()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 0;

            // Solution:
            var fibo = Fibo(90);

            for (var k = 2; k <= 90; k++)
            {
                result += ModS(fibo[k]);
                result %= mod;
            }

            if (result < 0)
            {
                result += mod;
            }

            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    684, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 15
0
        public Problem0016()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 0;

            // Solution:
            var n = BigInteger.Pow(2, 1000);

            foreach (var c in n.ToString())
            {
                result += c - 48;
            }


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    16, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 16
0
        public Problem0048()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            BigInteger result = 0;

            // Solution:
            for (var i = 1; i < 1000; i++)
            {
                result += BigInteger.Pow(i, i);
            }


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result.ToString().Substring(result.ToString().Length - 10) + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    48, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 17
0
        public Problem0015()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();

            // Solution:
            BigInteger n = 1;
            BigInteger k = 1;

            for (var i = 21; i < 40; i++)
            {
                n = n * i;
                k = k * (i - 20);
            }

            n = n / k * 2;


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + n + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    15, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 18
0
        public Problem0024()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();

            // Solution:
            var n            = 1000000;
            var permutations = new string[n];
            var first        = "0123456789";

            Permute(permutations, "", first);


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + permutations[n - 1] + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    24, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 19
0
        public Problem0013()
        {
            var numbers   = fill();
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            var result = new BigInteger(0);

            // Solution:
            foreach (var bI in numbers)
            {
                result = result + bI;
            }


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    13, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 20
0
        public Problem0023()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 1;

            // Solution:
            var abundant = new List <int>();
            var sumOfTwo = new bool[28123 + 1];

            for (var i = 2; i <= 28123; i++)
            {
                long sum  = 1;
                var  root = Math.Sqrt(i);
                for (var j = 2; j <= root; j++)
                {
                    if (i % j == 0)
                    {
                        sum += j + (root == j ? 0 : i / j);
                    }
                }

                if (sum > i)
                {
                    abundant.Add(i);
                }
            }

            for (var i = 0; i < abundant.Count; i++)
            {
                for (var j = i; j < abundant.Count; j++)
                {
                    sumOfTwo[abundant[i] + abundant[j] < 28123 ? abundant[i] + abundant[j] : 24] = true;
                }
            }

            for (var i = 2; i < 28123; i++)
            {
                if (!sumOfTwo[i])
                {
                    result += i;
                }
            }

            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    23, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 21
0
        public Problem0206()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            ulong result = 0;

            // Solution:
            var start = (ulong)Math.Sqrt(1020304050607080900.0);
            var end   = (ulong)Math.Sqrt(1929394959697989990.0);

            // String pattern = "1.2.3.4.5.6.7.8.9.0";
            // Regex regex = new Regex(pattern);

            for (var i = start; i <= end; i += 10)
            {
                // if (regex.IsMatch((i * i) + ""))
                // {
                //     result = i;
                //     break;
                // }

                var s = "" + i * i;
                if (s.Length != 19)
                {
                    continue;
                }

                if (s[16] == '9' &&
                    s[14] == '8' &&
                    s[12] == '7' &&
                    s[10] == '6' &&
                    s[8] == '5' &&
                    s[6] == '4' &&
                    s[4] == '3' &&
                    s[2] == '2' &&
                    s[0] == '1' &&
                    s[18] == '0')
                {
                    result = i;
                    break;
                }
            }

            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    206, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 22
0
        public Problem0099()
        {
            var lines   = File.ReadAllLines(@"resources\p099_base_exp.txt");
            var numbers = new int[lines.Length, 2];

            for (var i = 0; i < lines.Length; i++)
            {
                numbers[i, 0] = int.Parse(lines[i].Substring(0, lines[i].IndexOf(',')));
                numbers[i, 1] = int.Parse(lines[i].Substring(lines[i].IndexOf(',') + 1));
            }

            var stopWatch = new Stopwatch();

            stopWatch.Start();
            var maxIndex = 0;

            var number   = numbers[0, 0];
            var exponent = numbers[0, 1];

            for (var i = 1; i < lines.Length; i++)
            {
                if (numbers[i, 1] > exponent)
                {
                    var div = (double)numbers[i, 1] / exponent;
                    if (Math.Pow(numbers[i, 0], div) > number)
                    {
                        maxIndex = i;
                        number   = numbers[i, 0];
                        exponent = numbers[i, 1];
                    }
                }
                else
                {
                    var div = (double)exponent / numbers[i, 1];
                    if (Math.Pow(number, div) < numbers[i, 0])
                    {
                        maxIndex = i;
                        number   = numbers[i, 0];
                        exponent = numbers[i, 1];
                    }
                }
            }

            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + ++maxIndex + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    99, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 23
0
        public Problem0058()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            decimal result = 0;

            // Solution:
            primeArr = PrimeSieveButFast(100_000);
            decimal all    = 5;
            decimal primes = 3;

            decimal bottomLeft = 7;
            decimal topLeft    = 5;
            decimal topRight   = 3;

            for (decimal i = 2;; i++)
            {
                bottomLeft += i * 8 - 2;
                topLeft     = bottomLeft - i * 2;
                topRight    = topLeft - i * 2;
                all        += 4;

                if (IsPrime(bottomLeft))
                {
                    primes++;
                }
                if (IsPrime(topLeft))
                {
                    primes++;
                }
                if (IsPrime(topRight))
                {
                    primes++;
                }
                if (10 * primes < all)
                {
                    result = i * 2 + 1;
                    break;
                }
            }


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    58, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 24
0
        public Problem0021()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 0;

            // Solution:
            for (long i = 2; i <= 10000; i += 2)
            {
                long sum1 = 1;
                var  root = Math.Sqrt(i);
                for (var j = 2; j <= root; j++)
                {
                    if (i % j == 0)
                    {
                        sum1 += j + i / j;
                    }
                }

                if (sum1 == i)
                {
                    continue;
                }

                long sum2 = 1;
                root = Math.Sqrt(sum1);
                for (var j = 2; j <= root; j++)
                {
                    if (sum1 % j == 0)
                    {
                        sum2 += j + sum1 / j;
                    }
                }

                if (sum2 == i)
                {
                    result += i;
                }
            }


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    21, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 25
0
        public Problem0009()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 0;

            // Solution:
            for (var i = 1; i < 500; i++)
            {
                for (var j = i; j < 500; j++)
                {
                    for (var k = j; k < 500; k++)
                    {
                        if (i * i + j * j == k * k && i + j + k == 1000)
                        {
                            result = i * j * k;
                            goto end;
                        }
                    }
                }
            }

            /*
             * // Maurice Solution:
             * for (int i = 1; i < 500; i++)
             * {
             *  for (int j = i; j < 500; j++)
             *  {
             *      int k = 1000 - i - j;
             *      if (i * i + j * j == k * k)
             *      {
             *          result = i * j * k;
             *          goto end;
             *      }
             *  }
             * }
             */

end:
            {
            }

            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(9, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 26
0
        public Problem0045()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 0;

            // Solution:
            for (var i = 144;; i++)
            {
                long h = i * (2 * i - 2);

                long p = 0;
                for (long j = 165; p != h; j++)
                {
                    p = j / 2 * (3 * j + 1);
                    if (p > h)
                    {
                        goto end;
                    }
                }

                long t = 0;
                for (long j = 285; t != h; j++)
                {
                    t = j / 2 * (j + 1);
                    if (t > h)
                    {
                        goto end;
                    }
                }

                result = t;
                break;

end:
                {
                }
            }


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    45, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 27
0
        public Problem0010()
        {
            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 0;

            // Solution:
            // for (long i = 11; i < 2000000; i += 2)
            // {
            //     for (long j = 3; j <= (int) Math.Sqrt(i); j += 2)
            //     {
            //         if (i % j == 0)
            //         {
            //             goto end;
            //         }
            //     }
            //
            //     result += i;
            //     end:
            //     {
            //     }
            // }

            //better (Maurice):
            var bools = new bool[2000000];

            for (var i = 2; i < bools.Length; i++)
            {
                if (!bools[i])
                {
                    result += i;
                    for (var j = i; j < bools.Length; j += i)
                    {
                        bools[j] = true;
                    }
                }
            }


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    10, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 28
0
        public Problem0081()
        {
            var lines  = File.ReadAllLines(@"resources\p081_matrix.txt");
            var matrix = new int[lines.Length, lines.Length];

            for (var i = 0; i < lines.Length; i++)
            {
                var numbers = lines[i].Split(',');
                for (var j = 0; j < numbers.Length; j++)
                {
                    matrix[i, j] = int.Parse(numbers[j]);
                }
            }

            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 0;

            // Solution:
            var gridSize = matrix.GetLength(0);

            for (var i = gridSize - 2; i >= 0; i--)
            {
                matrix[gridSize - 1, i] += matrix[gridSize - 1, i + 1];
                matrix[i, gridSize - 1] += matrix[i + 1, gridSize - 1];
            }

            for (var i = gridSize - 2; i >= 0; i--)
            {
                for (var j = gridSize - 2; j >= 0; j--)
                {
                    matrix[i, j] += Math.Min(matrix[i + 1, j], matrix[i, j + 1]);
                }
            }

            result = matrix[0, 0];


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    81, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 29
0
        public Problem0059()
        {
            var encryptedStrings = File.ReadAllText(@"resources\p059_cipher.txt").Split(',');
            var encrypted        = new char[encryptedStrings.Length];

            for (var i = 0; i < encrypted.Length; i++)
            {
                encrypted[i] = (char)int.Parse(encryptedStrings[i]);
            }

            var stopWatch = new Stopwatch();

            stopWatch.Start();
            long result = 0;

            // Solution:
            for (var a = 'a'; a <= 'z'; a++)
            {
                for (var b = 'a'; b <= 'z'; b++)
                {
                    for (var c = 'a'; c <= 'z'; c++)
                    {
                        var decrypted = new char[encrypted.Length];
                        for (var i = 0; i < decrypted.Length; i++)
                        {
                            decrypted[i] = (char)(encrypted[i] ^ (i % 3 == 0 ? a : i % 3 == 1 ? b : c));
                        }

                        var dec = new string(decrypted);
                        if (!dec.Contains("Euler"))
                        {
                            continue;
                        }
                        // Console.WriteLine("\r" + a + b + c + "\t" + dec);
                        result = decrypted.Aggregate(result, (current, ch) => current + ch);
                    }
                }
            }

            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(
                    59, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }
Exemplo n.º 30
0
        public Problem0004()
        {
            ulong biggestFactor = 9999;
            var   stopWatch     = new Stopwatch();

            stopWatch.Start();
            ulong result = 0;

            // Solution:
            for (var i = biggestFactor * biggestFactor; i > 0; i--)
            {
                var test = (i + "").ToCharArray();
                for (ulong j = 0; j < (ulong)(test.Length / 2); j++)
                {
                    if (test[j] != test[(ulong)test.Length - j - 1])
                    {
                        goto outer;
                    }
                }

                result = i;
                //check if multiple of 3-digit numbers:
                for (var j = biggestFactor; j > Math.Sqrt(result); j--)
                {
                    if (result % j == 0)
                    {
                        goto end;
                    }
                }
outer:
                {
                }
            }

end:
            {
            }


            stopWatch.Stop();
            var elapsedTime = stopWatch.Elapsed.ToString();

            Console.WriteLine("Result:\t" + result + "\tTime:\t" +
                              (double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1, 2)) >= 1
                                   ? double.Parse(elapsedTime.Substring(elapsedTime.LastIndexOf(":") + 1)) + " s"
                                   : double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000 +
                               " ms"));
            if (ProblemTest.DoBenchmark)
            {
                ProblemBenchmark.AddTime(4, double.Parse(elapsedTime.Substring(elapsedTime.IndexOf(".") + 1)) / 10_000);
            }
        }