Posts

Showing posts from April, 2023

Using Python to Find Prime Numbers

Image
What's a prime number? A prime number is a natural number that's only divisible by 1 and itself. Also, a composite number is a natural number with positive factors other than 1 and itself. Thus, we can conclude that a prime number is not a composite number by the definitions. There are many algorithms to find prime numbers. First, we will introduce the simplest way to find prime numbers. Determining whether a number is prime or not is not really very different from determining whether a number is composite or not. To determine whether a number is composite, it is by definition sufficient to find a factor other than 1 and itself. If we find such a factor m for a number n, i.e. m divides n, then we must have m < n. So we only need to find a factor of n in the range from 2 to n-1. Here is the code: However, this brute force algorithm will take a lot of time to find the prime numbers if the range is too large. There is a better algorithm to save the time. In ancient Greece, a ma...

Train a Simple AI Using Jupyter Notebook

Image
The algorithms used to train the AI are backpropagation and gradient descent. Here is my code, for reference only: %load_ext jupyter_black %matplotlib inline import numpy as np import matplotlib.colors as mcolors import matplotlib.pyplot as plt def plot_training(x, y, iterations=10000, aggression=3.5, noise=1): global W1, W2, W3, b1, b2, b3 fig, ax = plt.subplots(figsize=(8, 8), dpi=80) ax.set_xlim([0, 1]) ax.set_ylim([0, 1]) ax.set_aspect(1) xx = np.arange(0, 1.01, 0.01) yy = np.arange(0, 1.01, 0.01) X, Y = np.meshgrid(xx, yy) Z = ((X - 0.5) ** 2 + (Y - 1) ** 2) ** (1 / 2) / (1.25) ** (1 / 2) im = ax.imshow(Z, vmin=0, vmax=1, extent=[0, 1, 1, 0], cmap=blueMap) ax.plot(y[0], y[1], lw=1.5, color=green) while iterations >= 0: j_W1 = J_W1(x, y) * (1 + np.random.randn() * noise) j_W2 = J_W2(x, y) * (1 + np.random.randn() * noise) j_W3 = J_W3(x, y) * (1 + np.random.randn() * noise) j_b1 = J_b1(x, y) * (1 + np...