Showing posts with label Project Euler. Show all posts
Showing posts with label Project Euler. Show all posts

Wednesday, 17 February 2016

Project Euler #5: 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?
 There's actually a really easy trick to solving this. It's not immediately obvious and I can't explain how it works well, but I'll do an example. Say we want the least common multiple of 1 through 10 (aka the smallest multiple). What we do is remove the prime factors from each of the numbers in order of smallest to lowest, but when take out a prime factor, we also remove it from any other number it can be removed from. For example
  • 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Remove 2: 1, 1, 3, 2, 5, 3, 7, 4, 9, 5
  • Remove 3: 1, 1, 1, 2, 5, 1, 7, 4, 3, 5
  • Remove 2: 1, 1, 1, 1, 5, 1, 7, 2, 3, 5
  • Remove 5: 1, 1, 1, 1, 1, 1, 7, 2, 3, 1
  • Remove 7: 1, 1, 1, 1, 1, 1, 1, 2, 3, 1
  • Remove 2: 1, 1, 1, 1, 1, 1, 1, 1, 3, 1
  • Remove 3: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Now we multiply all the prime factors we extracted and that's our answer! So 2 x 2 x 2 x 3 x 3x 5 x 7 = 2520 as expected. Alternatively you could've also only factored the non-prime numbers, but that would require knowing which numbers are prime - which isn't immediately obvious at high values. You could also eliminate any number which already has a multiple of itself - so 1, 2, 3, 4, and 5 - if you wanted to. An alternate version can be found here.

Coding this was pretty simple and the runtime is under 10 microseconds for numbers 1 through 20. This is probably the fastest solution I can come up with. The brute force method takes about 3 seconds in comparison.

The code can be found here.

Wednesday, 10 February 2016

Project Euler #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 ?
I started off by recycling the code from PE-10. To find the largest prime factor I'm going to find ALL the prime factors. By finding them in increasing order, I'm guaranteed that the last one I find will be the largest prime factor (side note: this also works if the inputted number is a prime number since the factors are 1 and itself).

I begin by setting the limit to the inputted number and starting at 5 and repeating my earlier method to find prime numbers in increasing order (sieving on 2 and 3, then using the idea that the smallest prime factor must be less than or equal to the square root of the number being tested if it's NOT a prime number). For every prime number I test if it's a factor of the inputted number. If it isn't then I store it in my prime number vector and move on. However, if it is then I set the new limit to be the old limit divided by this prime factor. Then I repeat the process. I continue until I reach the limit.

This works only because I'm finding the prime factors in order. Once we find one, we know the next one must be greater or equal to the one we just found. Thus I can continue on in my for-loop, only having to retest the most recent prime number (e.g. 8 is 2x2x2) before moving on. I like this solution because the amount of numbers we test is quite low so it scales really well.

For example, this problem asked for the prime factors of 600851475143. The prime factors are:
  1. 71, new limit is 8462696833
  2. 839, new limit is 10086647
  3. 1471, new limit is 6857
  4. 6857, new limit is 6857 (since they're equal)
Once the latest prime factor equals the limit we know we're done! So we only had to test roughly ~1142 numbers (6857 / 6 since we sieved). I'm not sure if the runtime I measured using is accurate, but it's returning 502 microseconds. I didn't bother making a naive version for this one since I was lazy.

The code can be found here.

Wednesday, 3 February 2016

Project Euler #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.

I'm feeling lazy so I'll do a quick post about the first problem since it's pretty straightforward. All I did was  loop through every number from 1 until the limit and test if either 3 or 5 was a divisor. Brute force ftw! Including the printing this takes 0.002 seconds.

Even still, I can think of a few things to speed it up if only trivially.
  1. Use something other than mod
  2. Instead of incrementing by 1, keep two counters - one for 3 and the other for 5. Increment whichever one is lower and add it to the sum until both counters reach the limit. This way you save on having to test if the number is a multiple of 3 or 5. You do however need to check if your two counters are equal. If they are then you skip that number.
The second solution might deserve to be coded for clarity if there's any interest. Be sure to check out the solution thread as there are some nice ways to get the answer without any coding.

Edit: The code is not formatted properly when embedded here for whatever reason

Monday, 7 December 2015

Project Euler #10: Summation of primes

This post is copied from another blog I run. I'll be shutting that blog down shortly and all future posts of that kind will go on this blog. These are solutions to various programming brain teasers. Optimizing your solution is the real challenge.

The problem reads as follows:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Right from the start I can think of two methods:
  1. Using a similar solution as for PE-7 but adding the primes instead of storing them
  2. Sieve of Eratosthenes
I opted for the first method since I'm lazy. With minimal modifications, I get 173518 microseconds as my runtime. I was curious about the second method, so I copied and modified a solution found in the problem discussion thread. You can find that one under sieve.cpp. It has a runtime of 221631 microseconds. That's still pretty good.

The third way I can think of is to combine the first two methods. As with PE-7, automatically exclude all multiples of 2 and 3 for a 2/3 reduction in numbers considered. Then find all the primes below the square root of 2 million (so anything below and including 1415) and sieve on the remaining 1/3 with those prime numbers. Could result in a nice speedup over the first method. As usual, another thing you could do is to remove the use of the modulus operand - it's expensive.

The code can be found here