The following code takes about two minutes to run on Python 3.3, but the equivalent version of VB.net works in less than one second. Is there any particular inefficiency I made here that slows down Python? Or is it just a slow translator? Can a Python math library be much slower? (Initializing x, x1, and x3 to float doesn't really matter).
inc = 2*3*5*7
for x in range(inc,200000,inc):
n = 0
y = x * x + x
for x1 in range(x+1, y):
x2 = x1 / (x1 - x) * x
x3 = round(x2)
if abs(x2 - x3) < 0.0000001:
if x3 < x1: break
n += 1
if n > 500: print(x, n)
(I understand that there are better algorithms to achieve the same. I'm interested in improving the Python implementation of this in order to learn more about Python.)
VB Code:
Dim x, x1, x2, x3, y As Double
Dim n As Integer
For x = 0 To 200000 Step 2 * 3 * 5 * 7
n = 0
y = x * x + x
For x1 = x + 1 To y
x2 = x1 / (x1 - x) * x
x3 = Round(x2)
If Math.Abs(x2 - x3) < 0.0000001 Then
If x3 < x1 Then Exit For
n += 1
End If
Next x1
If n > 500 Then sb.Append(x & " " & x1 & " " & x3 & " " & n & vbCrLf)
Next x
source
share