Created
January 28, 2021 21:42
-
-
Save lefuturiste/4751f6db83bc56c305d8835272e407b4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from math import * | |
import scipy.integrate as integrate | |
import numpy as np | |
import matplotlib.pyplot as plt | |
## exercice 1 | |
def getPrimes(limit): | |
L = [] | |
a = [True] * limit | |
a[0] = a[1] = False | |
count = 0 | |
for (i, isPrime) in enumerate(a): | |
if isPrime: | |
L.append(i) | |
for n in range(i*i, limit, i): | |
a[n] = False | |
return L | |
def plotPrimes(n): | |
primes = getPrimes(n+1)[2:] | |
x = [] | |
y = [] | |
# add additionals points | |
count = 0 | |
for i in primes: | |
# ajout de deux points | |
x += [i]*2 | |
y.append(count) | |
count += 1 | |
y.append(count) | |
plt.plot(x, y, 'b') | |
# a is const, b is x | |
def customInt(f, a, b, w = 0.0001): | |
L = np.arange(a, b, w) | |
area = 0 | |
for i in L: | |
area += w*f(i) | |
return area | |
def drawAccu(n): | |
plotPrimes(n) | |
w = 0.01 | |
x = np.arange(2, n, w) | |
g = lambda x: x/log(x) | |
plt.plot(x, np.vectorize(g)(x), 'r--') | |
#Li = lambda x: (integrate(lambda t: 1/log(t), 2, x)) | |
#Li = lambda x: integrate.quad(lambda t: 1/log(t), 2, x)[0] | |
f = lambda t: 1/log(t) | |
Li = [0] | |
for i in x: | |
Li.append(Li[-1] + w*f(i)) | |
Li = Li[1:] | |
plt.plot(x, Li, 'g--') | |
plt.legend(['pi(n)', 'x \mapsto x/log(x)', 'integral']) | |
#drawAccu(10000) | |
## Exercice 2 | |
def plotSquarePrime(n = 100): | |
primes = getPrimes((n*n)+1)[2:] | |
for i in range(n+1): | |
for j in range(n+1): | |
if (i*n+j) in primes: | |
# dessin du carré | |
x = [j, j+1, j+1, j, j] | |
y = [i, i, i+1, i+1, i] | |
plt.fill(y, x, 'black') | |
plt.xlim(0, n) | |
plt.ylim(0, n) | |
# plotSquarePrime() | |
## Bonus, Yin Yung | |
# size of the whole figure 2 unit of diameter | |
# (0, 0) is the center of the design | |
# (0, 0.5) is the center of the black circle | |
# (0, -0.5) is the center of the white circle | |
def drawBonus(): | |
# first draw the big circle | |
t = np.arange(0, 2*pi, 0.01) | |
x = list(np.cos(t)) | |
y = list(np.sin(t)) | |
plt.plot(x, y, 'k', lw=0.5) | |
getRange = lambda a, b, s=1: np.arange(a, b, s*0.01) | |
# draw the upper circle | |
t = getRange(pi/2, 3*pi/2) | |
x = [cos(k)*0.5 for k in t] | |
y = [sin(k)*0.5+0.5 for k in t] | |
# draw the lower circle | |
t = getRange(pi/2, -pi/2, -1) | |
x += [cos(k)*0.5 for k in t] | |
y += [sin(k)*0.5-0.5 for k in t] | |
# draw the return to the first points | |
t = getRange(-pi/2, pi/2) | |
x += list(np.cos(t)) | |
y += list(np.sin(t)) | |
plt.fill(x, y, 'k') | |
innerWidth = 0.15 | |
# draw the inner smaller lower circle | |
t = getRange(0, 2*pi) | |
x = [cos(k)*innerWidth for k in t] | |
y = [sin(k)*innerWidth-0.5 for k in t] | |
plt.fill(x, y, 'k') | |
t = getRange(0, 2*pi) | |
x = [cos(k)*innerWidth for k in t] | |
y = [sin(k)*innerWidth+0.5 for k in t] | |
plt.fill(x, y, 'w') | |
plt.axis('scaled') | |
drawBonus() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment