Пример #1
0
        static void Main(string[] args)
        {
            List <int>                  primes     = ACM.GeneratePrimes(80000);
            List <BigInteger>           powers     = new List <BigInteger>();
            Dictionary <string, string> powerNames = new Dictionary <string, string>();

            // This problem is the WORST.. too much stuff going on
            // Let's just brute force generate a ton of things
            // and hope for the best
            foreach (int p in primes)
            {
                for (int i = 1; i < 64; i++)
                {
                    BigInteger result = BigInteger.Pow(p, i);
                    powers.Add(result);
                    powerNames.Add(result.ToString(), p + "^" + i);
                }
            }
            powers.Sort();

            Console.Write("Enter a positive integer: ");
            int n = Console.ReadLine().ToInteger();

            string ending = ".";

            string pname = powerNames[powers[n - 1] + ""];

            if (!pname.EndsWith("^1"))
            {
                ending = " = " + pname + ".";
            }

            Console.WriteLine("The " + nth(n) + " prime power is " + powers[n - 1] + ending);
        }
Пример #2
0
        static void Main(string[] args)
        {
            Console.Write("Enter a positive integer: ");

            int n = Console.ReadLine().ToInteger();

            primes = ACM.GeneratePrimes(n);

            int cost = CalculateCost(n);

            Console.WriteLine("Cost: {0}", cost);
        }
Пример #3
0
        static void Main(string[] args)
        {
            Console.Write("Input: ");
            int n = Console.ReadLine().ToInteger();

            primes = ACM.GeneratePrimes(n);

            long sum = 0;

            for (int i = 2; i <= n; i++)
            {
                int gpf = g(i);
                sum += gpf;
            }

            Console.WriteLine("s({0}) = {1}", n, sum);
        }
Пример #4
0
        static void Main(string[] args)
        {
            Console.Write("Enter k and n: ");
            List <int> nums = Console.ReadLine().ToIntegerList();

            int count = 0;

            ACM.GeneratePrimes(nums[1]);  // We need to generate a massive list of primes first

            for (int i = 2; i <= nums[1]; i++)
            {
                if (i.GetPrimeFactors().Max() <= nums[0])  // Uses ACMHelper GetPrimeFactors integer extension method
                {
                    count++;
                }
            }

            Console.WriteLine("{0}", count + 1);  // Literally because we also include 1 as a k-smooth number
        }
Пример #5
0
        static void Main(string[] args)
        {
            // Generate list of primes less than or equal to 5000
            List <int> primes       = ACM.GeneratePrimes(5000);
            List <int> primesFaster = ACM.GeneratePrimesParallel(5000);

            List <int> factors = ACM.GetPrimeFactors(38);  // [2, 19]

            int        fact     = 88;
            List <int> factors2 = fact.GetPrimeFactors(); // [2, 2, 2, 11]

            int gpf = 4997.GreatestPrimeFactor();         // 263

            // Parse input strings
            List <int>    nums    = "1 2 3 4 5 6 7 8 9 10".ToIntegerList();
            List <string> strings = "apple banana carrot".ToStringList();

            int    num  = "1".ToInteger();
            long   num2 = "2".ToLong();
            double val  = "6.5".ToDouble();

            // Reverse a string
            String s = "taco";

            s = s.Reverse();  // "ocat"

            // Turn an int into a different arbitrary base (binary = 2, octal = 8, hex = 16)
            string output = 42.ToBase(2);                          // 101010
            string roman  = 57.ToRoman();                          // LVII

            string everything  = ACM.ConvertBase("101010", 2, 10); // 42
            string everything2 = "101010".ConvertBase(2, 10);      // 42

            // one hundred and twenty-three million four hundred and fifty-six thousand seven hundred and eighty-nine
            string numberwords = 123456789.ToWords();

            int solution = "2 + (3 * 4)".Evaluate();  // 14

            string longest = "apple car banana grapefruit".ToStringList().OrderByDescending(sp => sp.Length).First();

            string a = "the cat in the hat", b = "I left the cat in the garage";
            string common = ACM.LongestCommonSubstring(a, b);  // "the cat in the "

            // Generates all possible substrings of a string
            List <string> allSubstrings = "i love cats".FindAllSubstrings().ToList();
            string        prefix        = "the cat in the hat:the cat loves me:the cat eats mice".ToStringList(":").ShortestCommonPrefix(); // "the cat"


            // Greatest common divisor (biggest number that divides 12 and 8)
            int gcd = ACM.GreatestCommonDivisor(12, 8);  // 4

            // Least common multiple (first number that is a multiple of a and b)
            int lcm = ACM.LeastCommonMultiple(2, 7);  // 14

            // Check if two lists contain indentical elements (order matters)
            List <int> aq = 123456789.ToIntegerList(); // [1,2,3,4,5,6,7,8,9]
            List <int> bq = 123456789.ToIntegerList(); // [1,2,3,4,5,6,7,8,9]
            List <int> cq = 987654321.ToIntegerList(); // [9,8,7,6,5,4,3,2,1]

            bool equals  = aq.SequenceEqual(bq);       // true because both lists have same numbers in same sequence
            bool equals2 = aq.SequenceEqual(cq);       // false, same numbers but different order
            bool equals3 = aq.All(k => cq.Contains(k)) && aq.Count == cq.Count;


            List <int> digits = 12345.ToIntegerList();
            int        least  = digits.Min();

            // Rotate a list
            List <int> rotateLeft  = nums.Rotate(2);
            List <int> rotateRight = nums.Rotate(-2);

            // Generate permutations of a list
            List <int> plist = 1234.ToIntegerList();

            var permutations = plist.Permute().ToList();

            foreach (var permutation in permutations)
            {
                List <int> perm = permutation.ToList();
            }

            List <int> blist      = 12344321.ToIntegerList();
            bool       increasing = blist.IsIncreasing(0, 3);

            int revint = 12345.Reverse();

            bool res = 123457789.ToIntegerList().IsIncreasing();
        }