You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.5 KiB
85 lines
1.5 KiB
##斐波那契数列
|
|
A=[]
|
|
nterms =100
|
|
n1 = 0
|
|
n2 = 1
|
|
count = 2
|
|
if nterms>1:
|
|
A.append(1)
|
|
while count < nterms:
|
|
nth = n1 + n2
|
|
A.append(nth)
|
|
n1 = n2
|
|
n2 = nth
|
|
count += 1
|
|
|
|
##判断素数函数
|
|
import math
|
|
def isprime1(x):
|
|
if x==1:
|
|
return False
|
|
for i in range(2,int(math.sqrt(x)+1)):
|
|
if x % i == 0:
|
|
return False
|
|
return True
|
|
|
|
##倒序输出字符
|
|
def reverse(x):
|
|
o = 0
|
|
while x:
|
|
p = x % 10
|
|
o = o * 10 + p
|
|
x //= 10
|
|
return o
|
|
|
|
##求随机整数的均值
|
|
import random
|
|
A=range(0,101,1)
|
|
B=random.sample(A,k=10)
|
|
print(B)
|
|
C=random.randint(2, 6)
|
|
D=random.sample(B,k=C)
|
|
print(D)
|
|
average=sum(D)/len(D)
|
|
print(average)
|
|
##圆周率近似值
|
|
y=eval(input())
|
|
n=1
|
|
s=0
|
|
while n>0:
|
|
if (1/(2*n-1)) >= y:
|
|
s=(((-1)**(n-1))/(2*n-1))+s
|
|
n=n+1
|
|
else:
|
|
break
|
|
pi=4*s
|
|
print("pi的近似值为:",pi)
|
|
##阶乘函数
|
|
def jiecheng(x):
|
|
n=1
|
|
if x==0:
|
|
return 1
|
|
elif x>0:
|
|
for i in range(1,x+1):
|
|
n=n*i
|
|
return n
|
|
##组合数公式函数
|
|
def combine(m,n):
|
|
return int(jiecheng(m)/(jiecheng(n)*jiecheng(m-n)))
|
|
##组合数求和函数
|
|
def combinesum(x):
|
|
s=1
|
|
if x==0:
|
|
return 1
|
|
elif x>0:
|
|
for i in range(0,x):
|
|
s=s+combine(x,i)
|
|
return s
|
|
##构造杨辉三角形求和
|
|
def yanghuitriangle(n):
|
|
s=0
|
|
for i in range(0,n+1):
|
|
s=s+combinesum(i)
|
|
return s
|
|
|