Example #1
0
        static void Main(string[] args)
        {
            DisplayScreen pb8display = new DisplayScreen();

            pb8display.ProblemTitle  = "Problem 8";
            pb8display.ProblemHeader = "Largest product in a series";
            pb8display.Description   = "The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.\nFind the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?";
            pb8display.DisplayHeader();

            //////////////////////////////////////////////////////////////////
            string filePath = @"numberTable.txt";

            StreamProcess.DisplayFile(filePath);
            StreamReader sr = new StreamReader(filePath);

            string line = StreamProcess.ConvertToString(sr);

            //Console.WriteLine("converted string : " + line);
            long[] tab   = StringProducts.BiggestProduct(13, line);
            int    count = 0;

            foreach (long n in tab)
            {
                count++;
                Console.WriteLine("item " + count + " : " + n + " ");
            }

            /////////////////////////////////////////////////////////////////
            //  For this to Work you need to work with long type not int
            //
            /////////////////////////////////////////////////////////////////
            pb8display.DisplayFooter();
            Console.ReadKey();
        }
Example #2
0
        static void Main(string[] args)
        {
            DisplayScreen pb4display = new DisplayScreen();

            pb4display.ProblemTitle  = "Problem 4";
            pb4display.ProblemHeader = "Largest palindrome product";
            pb4display.Description   = "A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.\nFind the largest palindrome made from the product of two 3 - digit numbers.";
            pb4display.DisplayHeader();



            int biggestPalindrome = 0;

            for (int i = 100; i < 1000; i++)
            {
                for (int j = 100; j < 1000; j++)
                {
                    int result = NumberProduct.Product2Numbers(i, j);
                    if (Palindrome.IsPalindrome(result) && result > biggestPalindrome)
                    {
                        biggestPalindrome = result;
                    }
                }
            }

            pb4display.DisplayAnswer("Biggest Palindrome is :", biggestPalindrome);



            pb4display.DisplayFooter();
            Console.ReadKey();
        }
Example #3
0
        static void Main(string[] args)
        {
            DisplayScreen pb25display = new DisplayScreen();

            pb25display.ProblemTitle  = "Problem 25";
            pb25display.ProblemHeader = "1000-digit Fibonacci number";
            pb25display.Description   = "The Fibonacci sequence is defined by the recurrence relation:\nFn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.\nHence the first 12 terms will be:\nF1 = 1\nF2 = 1\nF3 = 2\nF4 = 3\nF5 = 5\nF6 = 8\nF7 = 13\nF8 = 21\nF9 = 34\nF10 = 55\nF11 = 89\nF12 = 144\nThe 12th term, F12, is the first term to contain three digits.\nWhat is the index of the first term in the Fibonacci sequence to contain 1000 digits ? ";
            pb25display.DisplayHeader();

            //////////////////////////////////////////////////////////////////


            BigInteger fib1  = 1;    //base case for index 1
            BigInteger fib2  = 1;    //base case for index 2
            BigInteger limit = 1000; //number of digits

            BigInteger fib3 = Fibonacci.fibDigitLimit(fib1, fib2, limit);

            Console.WriteLine("Fibonacci index  is: {0} for first Fibonacci number of {1} digits", fib3, limit);



            //////////////////////////////////////////////////////////////////

            pb25display.DisplayFooter();
            Console.ReadKey();
        }
Example #4
0
        static void Main(string[] args)
        {
            DisplayScreen pb22display = new DisplayScreen();

            pb22display.ProblemTitle  = "Problem 22";
            pb22display.ProblemHeader = "Names scores";
            pb22display.Description   = "Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.\nFor example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.\nWhat is the total of all the name scores in the file?";
            pb22display.DisplayHeader();

            //////////////////////////////////////////////////////////////////

            string filePath = @"p022_names.txt";

            string[] namesArray = ProcessNames.FileToArray(filePath);
            int      alphaValue, positionValue;

            ProcessNames.SortByAlphabet(namesArray);
            foreach (string name in namesArray)
            {
                alphaValue    = ProcessNames.GetNameAlphaValue(name);
                positionValue = ProcessNames.GetNameAlphaPosition(namesArray, name);
                ProcessNames.NameScore(alphaValue, positionValue);
            }

            Console.WriteLine("Total name score is {0}", ProcessNames.score);

            //////////////////////////////////////////////////////////////////

            pb22display.DisplayFooter();
            Console.ReadKey();
        }
Example #5
0
        static void Main(string[] args)
        {
            DisplayScreen pb23display = new DisplayScreen();

            pb23display.ProblemTitle  = "Problem 23";
            pb23display.ProblemHeader = "Non-abundant sums";
            pb23display.Description   = "A perfect number is a number for which the sum of its proper divisors is exactly equal to the number.\n For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.\nA number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.\nAs 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24.By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers.However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.\nFind the sum of all the positive integers which cannot be written as the sum of two abundant numbers.";
            pb23display.DisplayHeader();

            //////////////////////////////////////////////////////////////////
            int limit = 28123;//28123;

            List <long> list = NonAbundantSums.MakeabundantNumbersList(limit);

            //NonAbundantSums.DisplayList(list);
            bool[] isSumAbundant = NonAbundantSums.MakeSumOf2abundantNumbersList(list, limit);
            long   sum           = NonAbundantSums.NonSumOf2AbundantList(isSumAbundant);

            Console.WriteLine("sum of all numbers not able to be summed by two abundant numbers under {0} \nis {1} ", limit, sum);


            //////////////////////////////////////////////////////////////////

            pb23display.DisplayFooter();
            Console.ReadKey();
        }
Example #6
0
        static void Main(string[] args)
        {
            DisplayScreen pb12display = new DisplayScreen();

            pb12display.ProblemTitle  = "Problem 12";
            pb12display.ProblemHeader = "Highly divisible triangular number";
            pb12display.Description   = "The sequence of triangle numbers is generated by adding the natural numbers.\n So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.\n The first ten terms would be:\n1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...\nLet us list the factors of the first seven triangle numbers:\n1: 1\n3: 1,3\n6: 1,2,3,6\n10: 1,2,5,10\n15: 1,3,5,15\n21: 1,3,7,21\n28: 1,2,4,7,14,28\nWe can see that 28 is the first triangle number to have over five divisors.\nWhat is the value of the first triangle number to have over five hundred divisors?";
            pb12display.DisplayHeader();

            //////////////////////////////////////////////////////////////////
            int  solution = 500;
            long divisors = 1;
            long term     = 1;
            long number   = 1;

            while (divisors < solution)
            {
                number   = Triangle.TriangleNumber(term);
                divisors = Triangle.NumberOfDivisors(number, solution);
                term++;
            }
            Console.WriteLine("value is " + number + " with " + divisors + " divisors");

            // pb12display.DisplayAnswer("Prime number " + order + " is  :", result);

            pb12display.DisplayFooter();
            Console.ReadKey();
        }
Example #7
0
        static void Main(string[] args)
        {
            DisplayScreen pb26display = new DisplayScreen();

            pb26display.ProblemTitle  = "Problem 26";
            pb26display.ProblemHeader = "Reciprocal cycles";
            pb26display.Description   = "A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:\n1 / 2 = 0.5\n1 / 3 = 0.(3)\n1 / 4 = 0.25\n1 / 5 = 0.2\n1 / 6 = 0.1(6)\n1 / 7 = 0.(142857)\n1 / 8 = 0.125\n1 / 9 = 0.(1)\n1 / 10 = 0.1\nWhere 0.1(6) means 0.166666..., and has a 1 - digit recurring cycle. It can be seen that 1 / 7 has a 6 - digit recurring cycle.\nFind the value of d < 1000 for which 1 / d contains the longest recurring cycle in its decimal fraction part.";
            pb26display.DisplayHeader();

            //////////////////////////////////////////////////////////////////


            float   fraction = 1 / 7f;
            double  dbl      = 1 % 7d;
            decimal d        = 1 / 7m;

            Console.WriteLine(fraction);
            Console.WriteLine(dbl);
            Console.WriteLine("d:" + d);
            string s = "";

            s = d.ToString();
            Console.WriteLine(s);



            //////////////////////////////////////////////////////////////////

            pb26display.DisplayFooter();
            Console.ReadKey();
        }
Example #8
0
        static void Main(string[] args)
        {
            DisplayScreen pb20display = new DisplayScreen();

            pb20display.ProblemTitle  = "Problem 20";
            pb20display.ProblemHeader = "Factorial digit sum";
            pb20display.Description   = "n! means n × (n − 1) × ... × 3 × 2 × 1\nFor example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,\nand the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.\nFind the sum of the digits in the number 100!";
            pb20display.DisplayHeader();


            //////////////////////////////////////////////////////////////////
            int    factorialNumber = 100;
            double sum;
            // To make it work it requires to use BigInteger after adding System.Numerics namespace

            BigInteger factorialResult = 0;

            factorialResult = Factorials.BigFactorial(factorialNumber);
            sum             = Factorials.SumNumbers(factorialResult);

            Console.WriteLine("Factorial " + factorialNumber + "! is:\n\n" + factorialResult + "\n\nsum of all the digits is : \n" + sum);


            //////////////////////////////////////////////////////////////////
            pb20display.DisplayFooter();
            Console.ReadKey();
        }
Example #9
0
        static void Main(string[] args)
        {
            DisplayScreen pb3display = new DisplayScreen();

            pb3display.ProblemTitle  = "Problem 3";
            pb3display.ProblemHeader = "Largest prime factor";
            pb3display.Description   = "The prime factors of 13195 are 5, 7, 13 and 29.\n What is the largest prime factor of the number 600851475143 ?";
            pb3display.DisplayHeader();


            // To make it work I needed long numbers instead of long

            long number = 600851475143; //600851475143 13195

            List <long> List = PrimeFactors.PrimeFactorNumbers(number);

            try
            {
                Console.WriteLine("Largest Prime Factor for " + number + " is : " + List.Max());
            }
            catch (Exception ex)
            {
                Console.WriteLine("There was an error: " + ex.Message);
            }

            pb3display.DisplayFooter();
            Console.ReadKey();
        }
Example #10
0
        static void Main(string[] args)
        {
            DisplayScreen pb6display = new DisplayScreen();

            pb6display.ProblemTitle  = "Problem 5";
            pb6display.ProblemHeader = "Sum square difference";
            pb6display.Description   = "The sum of the squares of the first ten natural numbers is,\n12 + 22 + ... + 102 = 385\nThe square of the sum of the first ten natural numbers is,\n(1 + 2 + ... + 10)2 = 552 = 3025\nHence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.\nFind the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.";
            pb6display.DisplayHeader();

            //////////////////////////////////////////////////////////////////

            long a, b, result;

            a = SumSquareDifference.SumSquares(100);
            b = SumSquareDifference.SquareOfSum(100);

            result = SumSquareDifference.Difference(b, a);
            //Console.WriteLine(result);


            pb6display.DisplayAnswer("difference is :", result);



            pb6display.DisplayFooter();
            Console.ReadKey();
        }
Example #11
0
        static void Main(string[] args)
        {
            DisplayScreen pb10display = new DisplayScreen();

            pb10display.ProblemTitle  = "Problem 10";
            pb10display.ProblemHeader = "Summation of primes";
            pb10display.Description   = "The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.\nFind the sum of all the primes below two million.";
            pb10display.DisplayHeader();

            //////////////////////////////////////////////////////////////////

            bool check;
            int  limit = 2000000;
            long sum   = 5;//sum of 2 and 3

            for (int i = 5; i <= limit; i = i + 2)
            {
                check = IsPrime.CheckIfPrime(i);
                if (check)
                {
                    sum += i;
                }
            }

            Console.WriteLine("\nsum of primes up to " + limit + " is : " + sum);

            /////////////////////////////////////////////////////////////////
            pb10display.DisplayFooter();
            Console.ReadKey();
        }
Example #12
0
        static void Main(string[] args)
        {
            DisplayScreen pb5display = new DisplayScreen();

            pb5display.ProblemTitle  = "Problem 5";
            pb5display.ProblemHeader = "Smallest multiple";
            pb5display.Description   = "2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.\nWhat is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20 ? ";
            pb5display.DisplayHeader();

            int  number  = 2520;
            int  limit   = 20;
            bool isValid = false;

            while (isValid == false)
            {
                number++;
                isValid = SmallestMultiple.SmallestNumber(number, limit);
            }


            Console.WriteLine("Number " + number + " Pass Test: " + isValid);



            //pb5display.DisplayAnswer("number  :"+ number , isValid);



            pb5display.DisplayFooter();
            Console.ReadKey();
        }
Example #13
0
        static void Main(string[] args)
        {
            DisplayScreen pb16display = new DisplayScreen();

            pb16display.ProblemTitle  = "Problem 16";
            pb16display.ProblemHeader = "Power digit sum";
            pb16display.Description   = "2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.\nWhat is the sum of the digits of the number 21000 ? ";
            pb16display.DisplayHeader();


            //////////////////////////////////////////////////////////////////
            int        sum    = 0;
            int        power  = 1000;
            BigInteger number = 2;

            number = BigInteger.Pow(number, power);
            string s = number.ToString();

            //Console.WriteLine(s);
            foreach (char item in s)
            {
                sum += Convert.ToInt32(char.GetNumericValue(item));//converts char to double then converts double to int32
            }

            Console.WriteLine("sum is : " + sum);
            //////////////////////////////////////////////////////////////////
            pb16display.DisplayFooter();
            Console.ReadKey();
        }
Example #14
0
        static void Main(string[] args)
        {
            DisplayScreen pb9display = new DisplayScreen();

            pb9display.ProblemTitle  = "Problem 9";
            pb9display.ProblemHeader = "Special Pythagorean triplet";
            pb9display.Description   = "A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,\na2 + b2 = c2\nFor example, 32 + 42 = 9 + 16 = 25 = 52.\nThere exists exactly one Pythagorean triplet for which a +b + c = 1000.\nFind the product abc.";
            pb9display.DisplayHeader();

            //////////////////////////////////////////////////////////////////
            long a, b, c, d, limit, sum;

            c   = 1;
            b   = 1;
            sum = 0;
            long product;

            limit = 500;

            for (a = 2; a < limit; a++)
            {
                for (b = 1; b < limit / 2; b++)
                {
                    d = (a * a) + (b * b);
                    //Console.WriteLine(d);
                    c = Convert.ToInt32(Math.Sqrt(d));
                    //Console.WriteLine(c);

                    sum = a + b + c;
                    //Console.WriteLine("a = " + a + " , b = " + b + " , c = " + c);

                    //Console.WriteLine("Sum is : " + sum);
                    if (sum == 1000 && ((a * a) + (b * b) == (c * c)))
                    {
                        Console.WriteLine("\nSuccess!!!!!!!!!!\n a = " + a + " , b = " + b + " , c = " + c);
                        product = a * b * c;
                        Console.WriteLine("product is: " + product);
                        break;
                    }
                }
            }



            Console.WriteLine("YEAH!");



            /////////////////////////////////////////////////////////////////
            pb9display.DisplayFooter();
            Console.ReadKey();
        }
Example #15
0
        static void Main(string[] args)
        {
            DisplayScreen pb17display = new DisplayScreen();

            pb17display.ProblemTitle  = "Problem 17";
            pb17display.ProblemHeader = "Number letter counts";
            pb17display.Description   = "If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.\nIf all the numbers from 1 to 1000(one thousand) inclusive were written out in words, how many letters would be used?\n\nNOTE: Do not count spaces or hyphens. For example, 342(three hundred and forty - two) contains 23 letters and 115(one hundred and fifteen) contains 20 letters.The use of \"and\" when writing out numbers is in compliance with British usage.";
            pb17display.DisplayHeader();


            //////////////////////////////////////////////////////////////////
            Console.BufferHeight = Int16.MaxValue - 1; // ***** Alters the BufferHeight *****

            for (int i = 1; i <= 1000; i++)
            {
                if (i <= 20)
                {
                    CountLetters.LetterSumLess20(i);
                    Console.WriteLine();
                }
                else if (i > 20 && i < 100)
                {
                    CountLetters.LetterSumOver20Less100(i);
                    Console.WriteLine();
                }
                else if (i >= 100 && i < 1000)
                {
                    CountLetters.LetterSumOver100(i);
                    Console.WriteLine();
                }
                else if (i == 1000)
                {
                    CountLetters.LetterSum1000(i);
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("Case not handled");
                    Console.WriteLine();
                }
            }


            Console.WriteLine("Total number of letters used is : " + CountLetters.lettersSum);


            //////////////////////////////////////////////////////////////////
            pb17display.DisplayFooter();
            Console.ReadKey();
        }
Example #16
0
        static void Main(string[] args)
        {
            DisplayScreen pb67display = new DisplayScreen();

            pb67display.ProblemTitle  = "Problem 67";
            pb67display.ProblemHeader = "Number letter counts";
            pb67display.Description   = "If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.\nIf all the numbers from 1 to 1000(one thousand) inclusive were written out in words, how many letters would be used?\n\nNOTE: Do not count spaces or hyphens. For example, 342(three hundred and forty - two) contains 23 letters and 115(one hundred and fifteen) contains 20 letters.The use of \"and\" when writing out numbers is in compliance with British usage.";
            pb67display.DisplayHeader();


            //////////////////////////////////////////////////////////////////
            //string filePath = @"TriangleExample.txt";
            //string filePath = @"NumbersTriangle.txt";
            string       filePath  = @"p067_triangle.txt";
            StreamReader sr        = new StreamReader(filePath);
            string       line      = sr.ReadLine();
            int          pathSteps = 100;//number of lines in triangle or file

            int[,] triangleArray = new int[pathSteps, pathSteps];
            int column = 0;
            int row    = 0;

            //trying solution on example
            while (line != null)
            {
                // Console.WriteLine(line);
                t = line.Split(' ');
                foreach (string item in t)
                {
                    triangleArray[column, row] = int.Parse(item);
                    row++;
                }
                line = sr.ReadLine();
                column++;
                row = 0;
            }

            computeTriangle.DisplayArray(triangleArray, pathSteps);

            //Console.WriteLine("length:" + triangleArray.Length);
            computeTriangle.CompareTriangle(triangleArray, pathSteps);

            //Console.WriteLine("new array: ");
            //computeTriangle.DisplayArray(triangleArray);

            //////////////////////////////////////////////////////////////////
            pb67display.DisplayFooter();
            Console.ReadKey();
        }
Example #17
0
        static void Main(string[] args)
        {
            DisplayScreen pb1display = new DisplayScreen();

            pb1display.ProblemTitle  = "Problem 1";
            pb1display.ProblemHeader = "Multiples of 3 and 5";
            pb1display.Description   = "If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.Find the sum of all the multiples of 3 or 5 below 1000.";
            pb1display.DisplayHeader();

            int s = Multiples.SumOfMultiples(3, 5, 1000);

            pb1display.DisplayAnswer("the total sum of multiples is", s);


            Console.ReadKey();
        }
Example #18
0
        static void Main(string[] args)
        {
            DisplayScreen pb11display = new DisplayScreen();

            pb11display.ProblemTitle  = "Problem 11";
            pb11display.ProblemHeader = "Largest product in a grid";
            pb11display.Description   = "In the 20×20 grid below, four numbers along a diagonal line have been marked in red.\nThe product of these numbers is 26 × 63 × 78 × 14 = 1788696.\nWhat is the greatest product of four adjacent numbers in the same direction(up, down, left, right, or diagonally) in the 20×20 grid ? ";
            pb11display.DisplayHeader();

            //////////////////////////////////////////////////////////////////
            // Creating Text File with grid
            // grid.txt
            // moved it to bin/debug

            string       filePath = @"grid.txt";
            StreamReader sr       = new StreamReader(filePath);

            string[,] grid = Grid.PopulateArrayFromStream2D(20, sr);

            Grid.DisplayGrid(grid);
            long biggestProduct = 0;
            //Console.WriteLine(grid[2, 2]);

            long productRight = Grid.BiggestProductRight(grid, 4);
            // Console.WriteLine("\nProduct is :" + productRight);
            long productDown = Grid.BiggestProductDown(grid, 4);
            //Console.WriteLine("\nProduct is :" + productDown);
            long productDiagLeft = Grid.BiggestProductDiagonalLeft(grid, 4);
            //Console.WriteLine("\nProduct is :" + productDiagLeft);
            long productDiagRight = Grid.BiggestProductDiagonalRight(grid, 4);

            //Console.WriteLine("\nProduct is DiagRight:" + productDiagRight);

            sr.Close();

            biggestProduct = Grid.BiggestProduct(productRight, productDown, productDiagLeft, productDiagRight);

            Console.WriteLine("\n\nTHE BIGGEST PRODUCT IS : " + biggestProduct);
            // pb11display.DisplayAnswer("Prime number " + order + " is  :", result);

            pb11display.DisplayFooter();
            Console.ReadKey();
        }
Example #19
0
        static void Main(string[] args)
        {
            DisplayScreen pb23display = new DisplayScreen();

            pb23display.ProblemTitle  = "Problem 24";
            pb23display.ProblemHeader = "Lexicographic permutations";
            pb23display.Description   = "A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:\n012   021   102   120   201   210\nWhat is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 ? ";
            pb23display.DisplayHeader();

            //////////////////////////////////////////////////////////////////
            // More info on implemented algorithm on Wikipedia
            // https://en.wikipedia.org/wiki/Permutation
            //////////////////////////////////////////////////////////////////

            int[] items = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int   size  = items.Length;
            int   limit = 1000000;
            int   count = 1;
            long  possiblePermutations = Factorials.Factorial(size);

            Console.WriteLine("possible permutations :{0}\n", possiblePermutations);
            int k = 0;
            int l;

            while (count != limit)
            {
                //Implementing Generation in lexicographic order
                k = Permutations.FindK(items);
                l = Permutations.FindL(items, k);
                Permutations.SwapValues(k, l, items);
                Permutations.ReverseSeq(items, k);
                count++;
            }

            Console.Write("Permutation number {0}:   ", count);
            Permutations.DisplayArray(items);

            //////////////////////////////////////////////////////////////////

            pb23display.DisplayFooter();
            Console.ReadKey();
        }
Example #20
0
        static void Main(string[] args)
        {
            DisplayScreen pb2display = new DisplayScreen();

            pb2display.ProblemTitle  = "Problem 2";
            pb2display.ProblemHeader = "Even Fibonacci numbers";
            pb2display.Description   = "Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:\n 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... \n By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.";
            pb2display.DisplayHeader();


            int limit = 4000000;
            int total = Fibonacci.SumEvenTerms(limit);



            //Console.WriteLine("Fibonaaci sum for term " + term + " is " + a);
            pb2display.DisplayAnswer("Fibonacci sum for even numbers under 4000000 is : ", total);

            Console.ReadKey();
        }
Example #21
0
        static void Main(string[] args)
        {
            DisplayScreen pb7display = new DisplayScreen();

            pb7display.ProblemTitle  = "Problem 7";
            pb7display.ProblemHeader = "10001st prime";
            pb7display.Description   = "By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.\nWhat is the 10 001st prime number ? ";
            pb7display.DisplayHeader();

            //////////////////////////////////////////////////////////////////

            int order = 10001;

            int result = PrimeNumberOrder.PrimeNumberST(order);

            pb7display.DisplayAnswer("Prime number " + order + " is  :", result);


            pb7display.DisplayFooter();
            Console.ReadKey();
        }
Example #22
0
        static void Main(string[] args)
        {
            DisplayScreen pb14display = new DisplayScreen();

            pb14display.ProblemTitle  = "Problem 14";
            pb14display.ProblemHeader = "Longest Collatz sequence";
            pb14display.Description   = "The following iterative sequence is defined for the set of positive integers:\n\tn → n / 2(n is even)\n\tn → 3n + 1(n is odd)\nUsing the rule above and starting with 13, we generate the following sequence:\n\t13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1\nIt can be seen that this sequence(starting at 13 and finishing at 1) contains 10 terms.\nAlthough it has not been proved yet(Collatz Problem), it is thought that all starting numbers finish at 1.\nWhich starting number, under one million, produces the longest chain?\nNOTE: Once the chain starts the terms are allowed to go above one million.";
            pb14display.DisplayHeader();


            //////////////////////////////////////////////////////////////////
            long startingNumber = 0;
            long steps          = 0;
            int  limit          = 1000000;

            long[] table        = new long[limit / 2 + 1];//no need to have a bigger table I suppose, halfway the limit is the fastest
            long   biggestChain = 0;


            for (long i = 1; i < limit; i++)
            {
                steps = Collatz.CountCollatzNoRecurrence(table, i);
                if (i < table.Length)
                {
                    table[i] = steps;
                }
                if (steps >= biggestChain)
                {
                    biggestChain   = steps;
                    startingNumber = i;
                }
            }

            Console.WriteLine("for number " + startingNumber + " there are " + biggestChain + " steps ");


            pb14display.DisplayFooter();
            Console.ReadKey();
        }
Example #23
0
        static void Main(string[] args)
        {
            DisplayScreen pb13display = new DisplayScreen();

            pb13display.ProblemTitle  = "Problem 13";
            pb13display.ProblemHeader = "Large sum";
            pb13display.Description   = "Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.";
            pb13display.DisplayHeader();


            //////////////////////////////////////////////////////////////////
            //Make reference to System.Numeric library in assembly
            string       filePath = @"LargeSum.txt";
            StreamReader sr       = new StreamReader(filePath);
            string       solution = "";
            string       line     = sr.ReadLine();
            BigInteger   result   = new BigInteger();//Make reference to System.Numeric library in assembly

            while (line != null)
            {
                Console.WriteLine(line);
                result += BigInteger.Parse(line);

                line = sr.ReadLine();
            }
            sr.Close();
            string resultString = result.ToString();

            for (int i = 0; i < 10; i++)
            {
                solution += resultString[i];
            }


            Console.WriteLine("\nThe first ten digits of the sum : " + solution);

            pb13display.DisplayFooter();
            Console.ReadKey();
        }
Example #24
0
        static void Main(string[] args)
        {
            DisplayScreen pb15display = new DisplayScreen();

            pb15display.ProblemTitle  = "Problem 15";
            pb15display.ProblemHeader = "Lattice paths";
            pb15display.Description   = "Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.\nHow many such routes are there through a 20×20 grid ? ";
            pb15display.DisplayHeader();


            //////////////////////////////////////////////////////////////////
            //The math solution would be to calculate:
            //n=rows+columns
            //nRight, nDown
            // solution = n!/nRight!*nDown!, in this case: 40!/(20!*20!)
            ///////////////////////////////////////////////////////////////////

            ///////////////////////////////////////////////////////////////////
            //Below is a more general programming approach:
            //This is calle Dynamic programming, solve a smaller subproblem to write the full solution
            ///////////////////////////////////////////////////////////////////

            //Below are number of rows and columns for a grid
            int rows    = 20;
            int columns = 20;

            //Creates table with all possible path values for each node
            long[,] gridTable = NumberPaths.CalculateNumberOfPaths(rows, columns);

            long solution = gridTable[rows, columns];//Table value of the End Node

            Console.Write("There are " + solution + " paths in this grid");

            pb15display.DisplayFooter();
            Console.ReadKey();
        }
Example #25
0
        static void Main(string[] args)
        {
            DisplayScreen pb21display = new DisplayScreen();

            pb21display.ProblemTitle  = "Problem 21";
            pb21display.ProblemHeader = "Amicable numbers";
            pb21display.Description   = "Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).\nIf d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.\nFor example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284.The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.\nEvaluate the sum of all the amicable numbers under 10000.";
            pb21display.DisplayHeader();


            //////////////////////////////////////////////////////////////////
            //int number = 284;
            int  limit = 10000;
            long sum = 0;
            int  a, b;

            for (int i = 1; i < limit; i++)
            {
                a = AmicableNumbers.divisorsSum(i);
                b = AmicableNumbers.divisorsSum(a);
                if (b == i && a != b)
                {
                    AmicableNumbers.AmicableAddToList(a, b);
                }
            }

            sum = AmicableNumbers.ReturnSumOfElementsList(AmicableNumbers.amicableNumbers);

            Console.WriteLine("sum of amicable numbers up to " + limit + " is : " + sum);



            //////////////////////////////////////////////////////////////////
            pb21display.DisplayFooter();
            Console.ReadKey();
        }