Spread the love

One of the stumbling blocks I have is I feel I am quite out of programming for a change. I tried to do different jobs, was doing my best, yet they happened to end.

Nothing was said that I did things badly, I just didn’t fit in with everyone. Due to that? I really need to get into my programming habits again. I do work externally for a company, it is just slow due to my stumbling blocks…

This brought me to look at bringing a few different sides back into my programming side. It felt slightly bad that I just couldn’t get back into it. I know of lists of places where you can “learn to code” and figured I would take a look. It just happened to be something I didn’t really feel into much.

Reading through my old code projects I saw that I did some Project Euler questions at random before, I looked through it and figured that will be the place to get back into it. I would go I should look up how to work with the Math functions in C# again and such. It just happened to fit.

I figured, it is a good thing to make sure I am spending time on answering questions in coding. So, I figure, I should do roughly 3 problems per post that brings it in for me to fill my gaps. I would also love to hear what people think about my solutions, and how I can make them better.

Problem 1

Multiples of 3 and 5.

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.

Project Euler – Problem 1
        static void Main(string[] args)
        {
            var watch = new System.Diagnostics.Stopwatch();

            watch.Start();
            var answer = 0;
            for (int i = 1; i < 1000; i++)
            {
                float j = (float)i;
                float k = j / 3;
                float l = j / 5;
                if (Math.Round(k) * 3 == j || Math.Round(l) * 5 == j)
                {
                    answer += i;
                }
            }
            watch.Stop();
            Console.WriteLine("Answer 1: " + answer.ToString());
            Console.WriteLine(@"     Exec: " + watch.ElapsedTicks.ToString() + " ticks");

            watch = new System.Diagnostics.Stopwatch();
            watch.Start();
            answer = 0;
            List<int> answerset = new List<int>();
            for (int i = 0; (i * 3) < 1000; i++)
            {
                var j = i * 3;
                answerset.Add(j);
            }
            for (int i = 0; (i * 5) < 1000; i++)
            {
                var j = i * 5;
                if (!answerset.Contains(j))
                    answerset.Add(j);
            }
            for (int i = 0; i < answerset.Count; i++)
            {
                answer += answerset[i];
            }
            watch.Stop();
            Console.WriteLine("Answer 2: " + answer.ToString());
            Console.WriteLine(@"     Exec: " + watch.ElapsedTicks.ToString() + " ticks");
            Console.ReadLine();
        }

You will note here I have 2 answers here, running one after the other. I answered it the cheapest way, then figured why not make it slightly better and more efficient?

Problem 2

Even Fibonacci Numbers

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:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Project euler – problem 2
        static void Main(string[] args)
        {
            var watch = new System.Diagnostics.Stopwatch();
            watch.Start();

            int answer = 0;
            int a = 1;
            int b = 2;
            int c = 0;
            while (a < 4000000)
            {
                if (Math.Floor(a / 2.0) * 2 == a)
                    answer += a;

                c = a + b;
                a = b;
                b = c;
            }

            watch.Stop();
            Console.WriteLine("Answer: " + answer.ToString());
            Console.WriteLine(@"     Exec: " + watch.ElapsedMilliseconds.ToString() + " ms");
            Console.ReadLine();
        }

Problem 3

Largest Prime Factor

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

Project Euler – problem 3
        static void Main(string[] args)
        {
            var watch = new System.Diagnostics.Stopwatch();

            watch.Start();

            List<int> primes = new List<int>();

            double check = 600851475143;
            int index = 1;
            int answer = 0;
            while (check > 1)
            {
                // Start at 2
                index++;
                bool isPrime = true;
                for (int i = 0; i < primes.Count; i++)
                {
                    if (Math.Floor((double)index / primes[i]) * primes[i] == index * primes[i])
                    {
                        isPrime = false;
                    }
                }
                if (isPrime)
                {
                    primes.Add(index);
                    answer = index;

                    double a = Math.Floor(check / index);
                    double b = a * index;
                    double q = b - check;
                    bool c = (a * index) - check == 0;
                    while (c)
                    {
                        check /= index;
                        a = Math.Floor(check / index);
                        b = a * index;
                        c = (a * index) - check == 0;
                        Console.WriteLine(" - " + index.ToString());
                    }
                }
            }

            watch.Stop();
            Console.WriteLine("Answer: " + answer.ToString());
            Console.WriteLine(@"     Exec: " + watch.ElapsedMilliseconds.ToString() + " ms");
            Console.ReadLine();
        }

Thoughts Afterwards

The thoughts are slightly interesting afterwards, it is interesting to have ideas to challenge myself with. Similarly, this can be a little extra fluff around the usual blog posts, sorry.

That being said, I am up to Problem 8 and I am slightly confused. I would love to hear if anyone knows if it can be a random direction for adjacency? Or should we stick to horizontal and vertical?

Similarly, I would love to hear programmers thoughts on my code. If you have thoughts, please be sure to share them. I look forward to that.

Facebook Comments