#๐Ÿ”’ improving this to include tetrations and exporting as a png and a txt file

5 messages ยท Page 1 of 1 (latest)

hazy haloBOT
#

@brittle pulsar

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

brittle pulsar
#
import turtle
import time
import math

def collatz_sequence(n, steps=0, values=None):
    if values is None:
        values = []

    values.append(n)

    if n == 1:
        values.extend([4, 2, 1])
        steps += 3
        return values, steps
    else:
        if n % 2 == 0:
            return collatz_sequence(n // 2, steps + 1, values)
        else:
            return collatz_sequence(3 * n + 1, steps + 1, values)

start_number = 999
print(f"Starting Collatz sequence at: {start_number}")

sequence, total_steps = collatz_sequence(start_number)
print(f"Reached 1 in {total_steps} steps.")

# Turtle setup
screen = turtle.Screen()
screen.title("Collatz Sequence Plot")
screen.bgcolor("black")

plot = turtle.Turtle()
plot.speed(0)
plot.color("cyan")
plot.penup()

screen_width = 800
screen_height = 600
x_pad = 50
y_pad = 30
screen.setup(width=screen_width, height=screen_height)

log_values = [math.log(v + 1) for v in sequence]
max_log_val = max(log_values)

x_scale = (screen_width - x_pad * 2) / len(sequence)
y_scale = (screen_height - y_pad * 2) / max_log_val

plot.goto(-screen_width // 2 + x_pad, -screen_height // 2 + log_values[0] * y_scale)
plot.pendown()

for i, log_val in enumerate(log_values):
    x = -screen_width // 2 + x_pad + i * x_scale
    y = -screen_height // 2 + y_pad + log_val * y_scale
    plot.goto(x, y)

# Save to EPS file
ts = plot.getscreen()
ts.getcanvas().postscript(file="collatz_plot.eps")
print("Plot saved as collatz_plot.eps")

plot.hideturtle()
screen.exitonclick()

#

improving this to include tetrations and exporting as a png and a txt file

hazy haloBOT
#

@brittle pulsar

Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.