0

Which is faster, Python or Go? And by how much? This is the question I found myself asking earlier this week after troubleshooting a script that my son had written in Python to calculate prime numbers.
In The Red Corner – Python
My son worked out a fairly simple algorithm to generate prime numbers which we tweaked slightly to optimize it (things like not bothering to check even numbers, not checking divisors that are larger than 1/3 of the number, not checking any number ending in 5, and so on). I’m not saying that this is production-ready code, nor highly optimized, but it does appear to work, which is what matters. The resulting code looks like this:
#!/usr/bin/python
max = 100000
for tens in xrange(0,max,10):
for ones in (1, 3, 7, 9):
a = tens + ones
halfmax = int(a/3) + 1
prime = True
for divider in xrange (3, halfmax, 2):
if a % divider == 0:
# Note that it's not a prime
# and break out of the testing loop
prime = False
break
# Check if prime is true
if prime == True:
print(a)
# Fiddle to print 2 as prime
if a == 1:
Continue reading