Python은 피보나치 수열, 구구 곱셈표, 피라미드 방법을 실현한다.

9079 단어
 
# 
def fb(max):
    a,b=0,1
    while a<max:
        print(a)
        a,b=b,a+b

fb(100)

귀속 실현 방법 1
def fb1(max,a=1,b=1):
    if a<max:
        print(a)
        fb1(max,b,a+b)
fb1(1000,77,88)

2, , ,
def function(n):
    assert n >= 0, 'n > 0'
    if n<= 1:
        return n
    return function(n-1) + function(n-2)
print(function(4))
for i in range(0,20):
    print(function(i),end=',')
 , , , , , 
def function(n):
    a,b = 0,1
    for i in range(n):
        a,b = b,a+b
    return a
print(function(3))

생성기 구현
def fib(max):
    a,b = 0,1
    while a<max:
        yield a
        a,b = b,a+b
fib_gt=fib(100)
#
print(next(fib_gt))
print(next(fib_gt))

 
  
n = int(input(''))
for i in range(1, n + 1):
    print(' ' * (n - (i - 1)) + '*' * (2 * i - 1))

1
for i in range(1,10):
    for j in range(1,i+1):
        d = i * j
        print('%d*%d=%-2d'%(i,j,d),end = ' ' )
    print()
 
def hanshu(n):
    m = n
    sums = 0
    for j in range(1,n+1):
        sums = m*j
        print("%d*%d=%-2d"%(m,j,sums),end = "  ")
    print("")
def hanshu1():
    for i in range(9,0,-1):
        hanshu(i)
hanshu1()
  
def hanshu(n):
    m = n
    sums = 0
    for k in range(0,10-n):
        print("       ",end = "")
    for j in range(1,n+1):
        sums = m*j
        print("%d*%d=%-2d"%(m,j,sums),end = " ")
    print("")

def hanshu1():
    for i in range(1,10):
        hanshu(i)

hanshu1()
 
def hanshu(n):
    for dix in range(10-n,0,-1):
        print("       ",end = "")
    sums = 0
    m = n
    for j in range(1,n+1):
        sums = m*j
        print("%d*%d=%-2d"%(m,j,sums),end = " ")
    print("")
def hanshu1():
    for i in range(9,0,-1):
        hanshu(i)
hanshu1()

done.

좋은 웹페이지 즐겨찾기