Sunday, March 29, 2015

Daily Code Puzzles

Here is a giant list of great sites to practice some puzzles for fun! You can do them when you want or make a goal to do one or a few a day. My current plan is to do one reddit daily planner when I can (ideally once a day, but hard to do with work schedule.. and *cough* DOTA 2!).

I suggest making a solution for your work and organizing it by puzzle #. I put each puzzle # in a file labeled 0-50, 51-100, 101-50, etc.. then picked a naming convention and I make one class page for each problem then make an instance of it in the console app and pass in whatever variable (if there is one) from the main console program.cs. Pick one, two sites at the most and stick with it for awhile. It's harder to keep to a goal when it becomes too much so start small! Take a look at my setup!
My Reddit Github repository

Monday, March 16, 2015

Project Euler - Smallest Multiple

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? (LCM of 1 to 20)
https://projecteuler.net/problem=5

public class Euler5
    {
        List> allFactors = new List>();
        List finalFactors = new List();

        public long Solve(int start, int end)
        {
            for (int i = start; i <= end; i++)
            {
                var factor = FindFactors(i);
                allFactors.Add(factor);
            }

            foreach (var factorsList in allFactors) 
            {
                foreach (var factor in factorsList)
                { 
                    if (!finalFactors.Contains(factor)) finalFactors.Add(factor);
                    int countCurrent = factorsList.Where(x => x == factor).Count();
                    while (countCurrent > finalFactors.Where(x => x == factor).Count())
                    {
                        finalFactors.Add(factor);
                        countCurrent--;
                    }
                }
            }

            return finalFactors.Aggregate(1, (a, b) => a * b);
        }

        private List FindFactors(int num)
        {
            List result = new List();

            while (num % 2 == 0)
            {
                result.Add(2);
                num /= 2;
            }

            int factor = 3;
            while (factor * factor <= num)
            {
                if (num % factor == 0)
                {
                    result.Add(factor);
                    num /= factor;
                }
                else factor += 2;
            }

            if (num > 1) result.Add(num);
            return result;
        }
    }

Tuesday, March 3, 2015

Project Euler - Largest palindrome product

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers.
https://projecteuler.net/problem=4

    public class Euler4_Palindrome
    {
        int product;
        string productString;
        string reversedProductString;
        List palindromeList = new List();

        public int Solve() 
        {
            for (int i = 999; i > 99; i--)
            {
                for (int j = 999; j > 99; j--)
                {
                    product = i * j;

                    productString = product.ToString();
                    reversedProductString = Reverse(productString);
                    
                    if (productString == reversedProductString) palindromeList.Add(product); 
                }
            }
            
            palindromeList.Sort();
            return palindromeList.Last();
        }

        public static string Reverse(string s)
        {
            char[] charArray = s.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }
    }