-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ffc9b3b
Showing
5 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import matplotlib.pyplot as plt | ||
from matplotlib.animation import FuncAnimation | ||
import numpy as np | ||
|
||
# Define RSA functions (simplified) | ||
def gcd(a, b): | ||
while b: | ||
a, b = b, a % b | ||
return a | ||
|
||
def generate_key_pair(p, q): | ||
n = p * q | ||
phi = (p - 1) * (q - 1) | ||
|
||
e = 65537 # A commonly used value for e | ||
while gcd(e, phi) != 1: | ||
e += 2 | ||
|
||
d = pow(e, -1, phi) # Calculating modular inverse | ||
|
||
return (n, e), (n, d) | ||
|
||
def rsa_encrypt(message, public_key): | ||
n, e = public_key | ||
return [pow(ord(char), e, n) for char in message] | ||
|
||
def rsa_decrypt(ciphertext, private_key): | ||
n, d = private_key | ||
return ''.join([chr(pow(char, d, n)) for char in ciphertext]) | ||
|
||
# Example values | ||
p = 61 | ||
q = 53 | ||
public_key, private_key = generate_key_pair(p, q) | ||
message = "HELLO" | ||
ciphertext = rsa_encrypt(message, public_key) | ||
decrypted_message = rsa_decrypt(ciphertext, private_key) | ||
|
||
# Create animation | ||
fig, ax = plt.subplots(figsize=(10, 6)) | ||
ax.set_xlim(0, 1) | ||
ax.set_ylim(0, 1) | ||
text = ax.text(0.5, 0.5, "", fontsize=12, ha='center', va='center') | ||
|
||
steps = [ | ||
f"Original message: {message}", | ||
f"Prime numbers: p = {p}, q = {q}", | ||
f"Generating keys...", | ||
f"Public key (n, e): {public_key}", | ||
f"Private key (n, d): {private_key}", | ||
f"Encrypting message...", | ||
f"Ciphertext: {ciphertext}", | ||
f"Decrypting ciphertext...", | ||
f"Decrypted message: {decrypted_message}", | ||
"RSA process complete." | ||
] | ||
|
||
def animate(frame): | ||
text.set_text(steps[frame]) | ||
plt.pause(2.5) # Pause for 2.5 seconds per frame | ||
|
||
anim = FuncAnimation(fig, animate, frames=len(steps), repeat=False) | ||
|
||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
# Generate a sample signal | ||
sampling_rate = 1000 # Hz | ||
duration = 1 # seconds | ||
t = np.linspace(0, duration, sampling_rate * duration) | ||
frequency1 = 60 # Hz | ||
frequency2 = 120 # Hz | ||
signal = np.sin(2 * np.pi * frequency1 * t) + 0.5 * np.sin(2 * np.pi * frequency2 * t) | ||
|
||
# Compute FFT | ||
fft_result = np.fft.fft(signal) | ||
frequencies = np.fft.fftfreq(len(fft_result), 1/sampling_rate) | ||
magnitude = np.abs(fft_result) | ||
|
||
# Plot the original signal and its FFT | ||
plt.figure(figsize=(12, 6)) | ||
|
||
plt.subplot(2, 1, 1) | ||
plt.plot(t, signal) | ||
plt.title("Original Signal") | ||
plt.xlabel("Time (s)") | ||
plt.ylabel("Amplitude") | ||
|
||
plt.subplot(2, 1, 2) | ||
plt.plot(frequencies, magnitude) | ||
plt.title("FFT of Signal") | ||
plt.xlabel("Frequency (Hz)") | ||
plt.ylabel("Magnitude") | ||
|
||
plt.tight_layout() | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
from matplotlib.animation import FuncAnimation | ||
|
||
# Define a simple hash function (modulus) | ||
def basic_hash_function(key, size): | ||
return key % size | ||
|
||
# Create a hash table | ||
hash_table_size = 10 | ||
hash_table = [None] * hash_table_size | ||
|
||
# Generate example data | ||
data = list(range(101)) | ||
|
||
# Animation setup | ||
fig, ax = plt.subplots(figsize=(8, 6)) | ||
plt.axis('off') | ||
frames = len(data) | ||
|
||
def animate(frame): | ||
ax.clear() | ||
value = data[frame] | ||
index = basic_hash_function(value, hash_table_size) | ||
|
||
if hash_table[index] is None: | ||
hash_table[index] = [value] | ||
else: | ||
hash_table[index].append(value) | ||
|
||
for i, bucket in enumerate(hash_table): | ||
if bucket is None: | ||
bucket_str = "Empty" | ||
else: | ||
bucket_str = " | ".join(map(str, bucket)) | ||
|
||
plt.text(0.1, 0.9 - i*0.1, f'Bucket {i}: {bucket_str}', transform=plt.gca().transAxes) | ||
|
||
plt.title(f"Inserting {value} (Hashed to Bucket {index})") | ||
plt.axis('off') | ||
|
||
anim = FuncAnimation(fig, animate, frames=frames, repeat=False) | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
def mandelbrot(c, max_iter): | ||
z = c | ||
for i in range(max_iter): | ||
if abs(z) > 2.0: | ||
return i | ||
z = z * z + c | ||
return max_iter | ||
|
||
def create_mandelbrot(width, height, x_min, x_max, y_min, y_max, max_iter): | ||
x = np.linspace(x_min, x_max, width) | ||
y = np.linspace(y_min, y_max, height) | ||
mandelbrot_set = np.zeros((width, height)) | ||
|
||
for i in range(width): | ||
for j in range(height): | ||
mandelbrot_set[i, j] = mandelbrot(x[i] + 1j * y[j], max_iter) | ||
|
||
return mandelbrot_set | ||
|
||
def plot_mandelbrot(mandelbrot_set, x_min, x_max, y_min, y_max): | ||
plt.imshow(mandelbrot_set, extent=(x_min, x_max, y_min, y_max)) | ||
plt.colorbar() | ||
plt.title("Mandelbrot Set") | ||
plt.xlabel("Real") | ||
plt.ylabel("Imaginary") | ||
plt.show() | ||
|
||
def main(): | ||
width = 800 | ||
height = 800 | ||
x_min, x_max = -2.0, 1.0 | ||
y_min, y_max = -1.5, 1.5 | ||
max_iter = 1000 | ||
|
||
mandelbrot_set = create_mandelbrot(width, height, x_min, x_max, y_min, y_max, max_iter) | ||
plot_mandelbrot(mandelbrot_set, x_min, x_max, y_min, y_max) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from matplotlib.animation import FuncAnimation | ||
|
||
def apply_rule(rule, state, idx): | ||
key = str(state[max(idx - 1, 0)]) + str(state[idx]) + str(state[(idx + 1) % len(state)]) | ||
return int(rule[key]) | ||
|
||
def generate_next_state(rule, current_state): | ||
return [apply_rule(rule, current_state, idx) for idx in range(len(current_state))] | ||
|
||
def simulate_rule_110(steps, width): | ||
rule110 = { | ||
"111": 0, "110": 1, "101": 1, "100": 0, | ||
"011": 1, "010": 1, "001": 1, "000": 0 | ||
} | ||
|
||
initial_state = [0] * width | ||
initial_state[width // 2] = 1 | ||
|
||
states = [initial_state] | ||
for _ in range(steps): | ||
next_state = generate_next_state(rule110, states[-1]) | ||
states.append(next_state) | ||
|
||
return states | ||
|
||
def animate(states): | ||
fig, ax = plt.subplots(figsize=(10, 6)) | ||
ax.set_title("Rule 110 Cellular Automaton") | ||
ax.set_xlabel("Cell") | ||
ax.set_ylabel("Time Step") | ||
|
||
def update(frame): | ||
ax.cla() | ||
ax.imshow([states[frame]], cmap="binary", aspect="auto", extent=[-0.5, len(states[frame]) - 0.5, len(states) - frame - 0.5, len(states) - frame + 0.5]) | ||
ax.set_title(f"Time Step {frame}") | ||
ax.set_xlabel("Cell") | ||
ax.set_ylabel("Time Step") | ||
|
||
ani = FuncAnimation(fig, update, frames=len(states), interval=500) | ||
plt.show() | ||
|
||
def main(): | ||
steps = 50 | ||
width = 101 | ||
|
||
states = simulate_rule_110(steps, width) | ||
animate(states) | ||
|
||
if __name__ == "__main__": | ||
main() |