Skip to content

Instantly share code, notes, and snippets.

@AnweshGangula
Created December 17, 2020 10:05
Show Gist options
  • Save AnweshGangula/7ddacadf0bdfe19bfefa8da8f67b18c7 to your computer and use it in GitHub Desktop.
Save AnweshGangula/7ddacadf0bdfe19bfefa8da8f67b18c7 to your computer and use it in GitHub Desktop.
Prime Numbers - Visualization (python)
# This is just an additional file. This is an alternative for p5.js script
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def check_prime(num):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
return False
break
else:
return True
else:
return False
x,y = [0,1,2], [0,0,0]
Curr_X, Curr_Y = x[-1], y[-1]
a,b = 1,1
j,k = 0,1
num = 3
limit = 30000
while num <= limit:
if check_prime(num):
a,b = b*-1,a*1
j,k=k,j
Curr_X = Curr_X + (a*j)
Curr_Y = Curr_Y + (b*k)
x.append(Curr_X)
y.append(Curr_Y)
num += 1
#Uncomment below lines and comment lines 44, 45 & 46 to graph get re-rendered in every loop (animated)
# plt.plot(x, y)
# plt.plot(x[-1],y[-1], marker='o', markersize=3, color="red")
# plt.pause(0.0001)
# if num < limit:
# plt.clf()
plt.figure(dpi=100, figsize = (32,24))
plt.plot(x, y)
plt.plot(x[-1],y[-1], marker='o', markersize=5, color="red")
plt.savefig('C:/Users/AnweshG/Desktop/Prime_Numbers_Vis_till_30K.pdf')
# plt.savefig('C:/Users/AnweshG/Desktop/Prime_Numbers_Vis_till_30K.png')
# plt.savefig('C:/Users/AnweshG/Desktop/Prime_Numbers_Vis_till_30K.eps', format='eps')
# plt.savefig('C:/Users/AnweshG/Desktop/Prime_Numbers_Vis_till_30K.svg', format='svg', dpi=2400)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment