Project Euler 357

checkI recently completed Project Euler 357 after working on it here and there over the course of a couple of days. I decided to do this one out of order because it seemed like a fairly straight foward problem.

Consider the divisors of 30: 1,2,3,5,6,10,15,30.
It can be seen that for every divisor d of 30, d+30/d is prime.

Find the sum of all positive integers n not exceeding 100 000 000
such that for every divisor d of n, d+n/d is prime.

This shouldn’t be hard. Just find the divisors of every number, and see if n+1 is prime. Heck, I already have an isPrime() function written! The brute force approach will be easy to code.

I started off with code resembing this:

bool isPrimeGen(int n, bool *primes)
{
    for (int d=1; d<=n; d++)
        if (n % d == 0)
            if (primes[d+n/d] != true)
                return false;
    return true;
}
int main()
{
    int limit = 100000000;
    unsigned long long sum = 1;
    for (int i=2; i<limit; i++)
        if (isPrimeGen(i))
            sum += i;
    printf("%d", sum)
}

Well, this worked… But it took far too long. It took over 4 hours to complete. Obviously this doesn’t fit in with the guidlines that solutions should take less than a minute to complete.

I was able to make a single optimization that got the time down from 4 hours to 12 minutes. A single, tiny change. (It’s actually a similar optimization I made to my prime function when I first started doing these Euler problems…) Instead of looking for every divisor up to the size of n, our number, I put in a square root n, sqrt(n), as the limit for divisor checking.

Another optimization was to make use of looking for patterns in the qualifying numbers.

  • n is always even
  • n is always 1 less than a prime number
  • n/2+2 is always a prime number

Filtering down the potential numbers by using these patterns further reduced the time, but it was still taking longer than I would have liked. I decided to take drastic measures… I pre-computed a ton of primes using a Seive of Eratosthenes.

I made a giant boolean array where any number is marked as a prime or not. This made looking up whether or not primes are numbers super trivial. I’m not sure this is the most elegant solution, but it seemed simple and fast.

// Generate primes using sieve
bool *primes = malloc(limit*sizeof(bool));
for (int i=2; i<limit; i++)
    primes[i] = true;
primes[0] = false;
primes[1] = false;
for (int i=2; i<limit; i++)
    if (primes[i] == true)
    {
        int n = 2;
        while (n*i < limit)
            primes[i*n++] = false;
    }

So, this generates an array, and I pre-load the value for each number as ‘true’ (excepting for 0 and 1 of course). I then go through sequentially, and if I hit a number that is ‘true,’ or prime, every multiple of that number gets marked as ‘false,’ or not prime, until we reach the limit of the array. This is a super fast method for calculating an obscene number of primes very quickly because it doesn’t require any division.

With this pre-loaded table of primes, the code was then refactored to make use of this table rather than my standard isPrime() function.

Putting all these changes together allowed the problem to be solved in under 5 seconds, which is just a tad better than my original 4 hours.

Of course, I won’t reveal the answer here. You’ll need to figure that out on your own. You can, however, check out my full solution on my GitHub account.

Minimal Heroes

My friend is about to have his first son (after multiple daughters), and as such, is beginning to prep the nursery with decor appropriate for a little boy. He requested that I make some minimal designs that he will print on canvas with familiar super hero logos or identities.

The two he wanted most were Iron Man and Captain America. I did a quick design for each based on the most iconic symbol for each.

I’m not a huge fan of “minimalist” design. When I say minimal, I’m speaking mainly of “flat” design. I can see the appeal of flat design; It’s easy to make, easy to print, easy to display. It feels lazy to me though. My friend however wanted them flatter.

I obliged his request. He was ecstatic. The flatness doesn’t speak to me. It feels sterile, and boring. But he’s happy, so I’m happy.

Jumping Flash

Robbit2Growing up, I was not allowed to have any game consoles, until the PlayStation came out. My brother convinced our dad that it was technically advanced enough to merit buying.

One of the first games we played was a demo for Jumping Flash! that came on the sample games CD with the PlayStation. Soon thereafter we purchased it, and I played it ad-naseum for years to come. It’s a very whimsical platform type game that is very light-hearted. The protagonist, Robbit (A robotic rabbit) is sent to save “jet pods” that are located on various pieces of planets that have been stolen by the evil Baron Aloha. One particular level, world 3-2, was the favorite. It had flying whales, catchy music, and fun rainbow roller coasters that you could ride.

This digital painting, like my other ones, was done in Adobe Photoshop with a Bamboo Graphic Pen in my spare time. I still play this game from time to time using an emulator. It’s great fun, and something I’ve wanted to illustrate for quite a while.

© 2007-2015 Michael Caldwell