## Coding Challenge I comes to a Close (edited 5 July 2013)

02 Jul

Christian Goldbach

Thanks to everyone who submitted entries to this coding challenge. Amazingly, even though the challenge was open to all languages, every entry was in Python. I was also amazed to see how consistent most answers were in their methods – perhaps that’s what I get for proposing a challenge that included prime-checking and simple math, or perhaps you are all just good programmers and gravitate towards nice, clean, direct code.

Anyway, I chose Eric’s entry as the winner because he provided code that was easy to read, answered the problem elegantly AND also supplied documentation that made it easy to follow the method he was taking.

Please note that I have changed the winner for this competition. I initially mis-attributed Eric’s work to another entrant. To rectify this, both Eric and Russel (whose solution was also very good and similar in approach, but not printed below) have been awarded the prize.

Here’s his code. Copy and paste it into your favorite method of running python programs (I stick to codecademy labs for the most part because I haven’t figured out how to run python reliably anywhere else). Russel has won a free copy of my zombie fractions book, ‘In Parts’ which he can donate to the fraction- and zombie- loving person in his life.

1. # Goldbach’s Conjecture
2. # Eric
3. from math import sqrt,ceil
4. max = 400
5. def primeSieve(max):
6.     “””Lists primes upto ~10,000,000 fairly quickly”””
7.     quickList = [True for i in range (max-1)]
8.     # highest possible prime
9.     maxCheck = ceil(sqrt(max))
10.     i = 2
11.     while i != maxCheck:
12.         # if i is prime
13.         if quickList[i-2] == True:
14.             # k keeps track of looper
15.             k = i
16.             while i + k <= max:
17.                 quickList[i+k-2] = False
18.                 k += i
19.             i += 1
20.         else:
21.             i += 1
22.     primeList = [iNum+2 for iNum,i in enumerate(quickList) if i == True]
23.     return primeList
24. primes = primeSieve(max)
25. # list of even numbers from 4 – max
26. evens = [2*i+4 for i in range((max-2)/2)]
27. # cycle through combinations of sums of primes, record what works
28. sums = []
29. def expressAsSum(target):
30.     for i in primes:
31.         for j in primes:
32.             if i+j == target:
33.                 sums.append(i)
34.                 sums.append(j)
35.                 return True
36.     # if no solutions are found, we’ve found a disclaimer for the conjecture!
37.     return False
38. #for each element in list of evens
39. for i in evens:
40.     if expressAsSum(i) == False:
41.         print (str(i) + “fails Goldbach’s Conjecture”)
42.         break
43. for iNum,i in enumerate(evens):
44.     print (“%d = %d + %d” % (i, sums[2*iNum], sums[2*iNum+1]))

Posted by on July 2, 2013 in Uncategorized

### 3 responses to “Coding Challenge I comes to a Close (edited 5 July 2013)”

1. July 5, 2013 at 9:00 pm

Hi it’s Eric again, I was just wondering why Russel’s entry was chosen the winner but my code is the one that’s been posted here.

• July 5, 2013 at 9:20 pm

Crap – I’m sorry about that Eric. I didn’t mean to misattribute. I’m sending you a person email as well, please comment again here or write to me directly if you do not receive it within the hour.

Sincerely,

Jack

2. July 6, 2013 at 12:22 am

Thanks for the fix! 🙂