python 实现几种数学算法


循环素数个数

def cycle(num):

strNum = str(num)

length = len(strNum)

cycle_all = []

for i in range(length):

number = int(strNum[i:] + strNum[:i])

cycle_all.append(number)

return cycle_all

def main(inNum):

count = 0

for i in range(2, inNum):

judge = True

for num in cycle(i):

if not prime(num):

judge = False

break

if judge:

count += 1

print(count)

汉诺塔


count = 1

def test(num, src, dst, rest):

global count

if num < 1:

print( False )

elif num == 1:

print( "Move %d: from \t%s to %s" % (count, src, dst))

count += 1

elif num > 1:

test(num - 1, src, rest, dst)

test(1, src, dst, rest)

test(num - 1, rest, dst, src)

num=int(input( 'please input int :' ))

test(num, 'A' , 'C' , 'B' )

python 实现几种数学算法

杨辉三角数值


def triangle(num):

#初始表值为[1]

triangle=[[1]]

#添加i个值([1])至triangle表,eg:[1]*3,triangle=[[1], [1], [1]]

for i in range(2, num+1):

triangle.append([1]*i)

#改变triangle表的值,eg:

#当num=5时,i取5,j取3

#triangle[4][1] = triangle[3][1]+triangle[3][0]

#triangle[4][2] = triangle[3][2]+triangle[3][1]

#triangle[4][3] = triangle[3][3]+triangle[3][2]

#相当于triangle表的第4位的值(这里的值为一个表)的第1,2,3位值等于第3位的值(这里的值也是一个表)的第1,2,3位值和0,1,2的值分别相加(即错位相加)。

for j in range(1, i-1):

triangle[i-1][j] = triangle[i-2][j]+triangle[i-2][j-1]

return triangle

#格式化输出(输出的是一个表)

def printtriangle(triangle, width):

#列宽

column = len(triangle[-1])*width

for sublist in triangle:

result = []

for contents in sublist:

#控制间距

result.append( '{0:^{1}}' .format(str(contents), width))

#控制缩进,{0:^{1}}:空格在两边补齐空位‘^’居中对齐,‘:’号后面带填充的字符

print( '{0:^{1}}' .format( '' .join(result), column))

#启动函数

if __name__ == '__main__' :

#输入整数

num = int(input( 'How many rows do you want:' ))

#打印信息

print( "The triangle rows as follows:" )

triangle = triangle(num)

#列宽

width = len(str(triangle[-1][len(triangle[-1])//2]))+3

printtriangle(triangle, width)

python 实现几种数学算法