def factorial(x):m=1if x == 0:return 1for i in range(1,x+1):m = m*ireturn mif __name__ == "__main__":x=10v=factorial(x)print("%d 的阶乘是 %d"%(x,v))
极坐标方程为:
ρ=r⋅(1−sinθ)\rho=r\cdot(1-\sin \theta)ρ=r⋅(1−sinθ)
其中,θ∈[0,2π)\theta\in[0,2\pi)θ∈[0,2π)
%matplotlib inline
import numpy as np
import matplotlib.pyplot as pltdef Descartes_heart(r):x=np.linspace(0, 2*np.pi, 500)rho = r*(1-np.sin(x))plt.subplot(polar=True)plt.plot(x, rho, c='r')plt.text(0,0,'Heart', color='m')plt.show()if __name__ == "__main__":a = 6Descartes_heart(a)
SA=π⋅122∗2≈mn\frac{S}{A}=\frac{\pi\cdot 1^2}{2*2}\approx\frac{m}{n} AS=2∗2π⋅12≈nm
则
π→4mn,当n→∞\pi\to\frac{4m}{n}, \;\;\;\;\;\text{当}\;\;\;\; n\to\inftyπ→n4m,当n→∞
def PI_approx_for(n):m=0for i in range(1,n+1):x=random()*2-1 #x的取值范围为[-1,1)y=random()*2-1 #y的取值范围为[-1,1)if x**2+y**2<1:m = m+1return 4*m/nfrom random import randomif __name__ == "__main__":n=100000c1=PI_approx_for(n)print('总实验次数是%d,计算的圆周率是%f'%(n,c1))
def PI_approx_np(n):data = np.random.random((n,2))*2-1plt.scatter(data[:,0],data[:,1],c='blue',marker='.')temp = np.sum(data**2, axis=1)label = temp<1plt.scatter(data[label,0],data[label,1],c='red',marker='.')plt.show()c = 4*np.sum(label)/nreturn c %matplotlib inline
import numpy as np
import matplotlib.pyplot as pltif __name__ == "__main__":n=10000c2 = PI_approx_np(n)print('总实验次数是%d,计算的圆周率是%f'%(n,c2))
判定公历闰年应遵循的一般规律为:四年一闰,百年不闰,四百年再闰.
def RunYear(y):if y%4==0 and y%100!=0 : #能被4整除,但不是百年的是闰年return 1elif y%400==0 : #能被400年整除的是闰年return 1else:return 0 #不是闰年
try:year=int(input('输入年份:'))if year>=0 :r1=RunYear(year)if r1 :print('%d是闰年'%(year))else:print('%d不是闰年'%(year))else:print('输入年份值不对!')
except:print('输入值有误!')
下一篇:为什么搭建指标体系