0

I have been exploring OEIS sequence A229037 aka "fractal smoke" and am trying to write some efficient code to produce it. I also want to look at variations of the sequence (e.g. what does the lexographically 2nd sequence that doesn't include arithmetic progressions look like? Does adding some erroneous points at the start change the long term fractal pattern that emerges?), which is why I can't just copy the coordinates given on the OEIS website.

The current code I'm using is in python (as its the only language I know):

# fractal smoke sequence

l = [(1, 1)]  # sequence
f = []  # forbidden points

for i in range(1000):  # first e.g. 100 terms
    fy = []
    for n in range(len(l) - 1):
        xa = 2 * l[len(l) - 1][0] - l[n][0]  # calculate x value of forbidden point
        ya = 2 * l[len(l) - 1][1] - l[n][1]  # calculate y value of forbidden point
        a = (xa, ya)
        f.append(a)  # append forbidden coordinate to f, then repeat for all other new arithmetic progressions

    for i in f:
        if i[0] == len(l) + 1:
            fy.append(i[1])  # append the y value of all forbidden points which have the x value of the current new sequence value

    l.append((len(l) + 1, min(set(range(1, len(l) + 1)) - set(fy))))  # append the point with the current x value and the minimum number which is not contained in fy

# plotting
x = []
y = []

for i in l:
    x.append(i[0])
    y.append(i[1])
plt.scatter(x, y, s=1)

which produces the correct values, but is very slow. This code can be generalised to e.g. this sequence by adding the gradient and y-intercept of each line that passes through every 2 points, rather than a single point like my above code does.

This code golf includes a python 2 program, but I'm struggling to understand their logic and how the zip() function is being used.

A reddit user here has calculated millions of points. Obviously this took a long time, but can anyone produce code that is efficient enough to do something similar e.g. approx 1 million points in a runtime that is approx 1 day? In python or another coding language?

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • Interesting question, however, I think questions about optimizing existing programs are often seen as off topic. Maybe the software engineering stackexchange or math.stackexchange.com would be suitable forums. Sometimes the OEIS has example code in different languages for each sequence, maybe take a look at that. You might try getting in touch with the people who wrote the code you have linked from your question to see if they have advice. – Robert Dodier Aug 28 '23 at 17:52
  • There are multiple approaches to optimize such program in general. 1) using math tricks, 2) improving the complexity, 3) doing micro-optimization, using a better-suited language, better containers, etc. Only 2) and 3) are on-topic on StackOverflow. IDK if the complexity can be improved (this is generally the first thing to try as a programmer), but please note Python (and more specifically CPython) is generally very slow (e.g. x10 to x1000) compared to native languages. For example `2 * l[len(l) - 1][0] - l[n]` are recomputed twice by the interpreter while this is useless. – Jérôme Richard Aug 28 '23 at 19:49
  • Indeed. One of the first optimization concerning speed, would be to use numpy arrays, and to already create the complete but empty numpy array from the start, to avoid the extremely slow appending to a list. – JohanC Aug 28 '23 at 22:27
  • I'm somewhat confused: the OEIS page for that sequence literally gives you the Python code for the sequence (as well as the mathematica, haskell, and pari code). Surely that'd be the starting point? – Mike 'Pomax' Kamermans Aug 28 '23 at 23:35

0 Answers0