Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions Collatz Sequence/Collaze-Visualize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import time
import matplotlib.pyplot as plt

def collatz_sequence(n):
"""Generate the Collatz sequence for n."""
steps = [n]
while n != 1:
n = n // 2 if n % 2 == 0 else 3 * n + 1
steps.append(n)
return steps


def visualize(sequence, title="Collatz Sequence"):
plt.clf()
plt.plot(sequence, marker='o')
plt.title(title)
plt.xlabel("Step")
plt.ylabel("Value")
plt.yscale("log") # makes visualization MUCH nicer
plt.grid(True)
plt.pause(0.01)


def auto_mode(interval):
print("\nAuto mode started.")
print("Press SPACE in the plot window to stop.\n")

plt.ion()
stop = False

def on_key(event):
nonlocal stop
if event.key == ' ':
stop = True

fig = plt.figure()
fig.canvas.mpl_connect("key_press_event", on_key)

n = 1
while not stop:
seq = collatz_sequence(n)
visualize(seq, f"Collatz Sequence for n = {n}")
n += 1
time.sleep(interval)

plt.ioff()
plt.show()
print("Auto mode stopped.")


# --- Main Program ---
try:
num = int(input("Enter a positive integer (or -1 for auto mode): "))

if num == -1:
interval = float(input("Enter step interval time (seconds): "))
auto_mode(interval)

elif num <= 0:
print("Please enter a positive number greater than 0.")

else:
seq = collatz_sequence(num)
print("\nCollatz sequence:")
for i, value in enumerate(seq, start=1):
print(f"Step {i}: {value}")

plt.ion()
visualize(seq, f"Collatz Sequence for n = {num}")
plt.ioff()
plt.show()

except ValueError:
print("Invalid input! Please enter a valid number.")
6 changes: 5 additions & 1 deletion Multiply.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
def product(a, b):
# Handle negative values
if b < 0:
return -product(a, -b)

if a < b:
return product(b, a)
elif b != 0:
Expand All @@ -9,4 +13,4 @@ def product(a, b):

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Product is: ", product(a, b))
print("Product is:", product(a, b))
Loading