Py_输出中文的九九乘法表

发布于 2019-10-14  25 次阅读


import math
def isPrime(n):
# Linear sieve
    visit = [1 for i in range(n * 10)]
    prime = [0 for i in range(n * 10)]  # temp
    num = 0
    for i in range(2, n + 1):
        if visit[i] == 1:
            num = num + 1
            prime[num] = i
        for j in range(1, num + 1):
            if i * prime[j] <= n:
                visit[i * prime[j]] = 0
                if i % prime[j] == 0: #Pruning
                    break
            else:
                break
    return prime

def fun():
    num = ["一", "二", "三", "四", "五", "六", "七", "八", "九"]
    for j in range(1, 10):
        for k in range(1, j + 1):
            c = k * j

            if c < 10:
                turn1 = num[j - 1]
                turn2 = num[k - 1]
                turn3 = num[c - 1]
                print(turn1 + turn2 + "得" + turn3 + "   ", end=' ')

            elif c == 10:
                print("二五一十" + '\t', end=' ')

            else:
                for i in range(2, 10):
                    if c < i * 10:
                        c -= (i - 1) * 10
                        turn1 = num[j - 1]
                        turn2 = num[k - 1]
                        turn3 = num[c - 1]
                        turn4 = num[i - 2]
                        print(turn1 + turn2 + turn4 + "十" + turn3 + "   ", end=' ')
                        break
        print()

def fun2():
    for i in range(1, 10):
        print()
        for j in range(1, i + 1):
            print('{0}*{1}={2:0>2}'.format(j, i, i * j), end=' ')
    print()

def fun3(n):
    pri = []
    pri = isPrime(int(1e5 + 20))
    pri[0] = 1
    i = 1
    cnt = 1
    if not n ^ 1:
        print(1)
        return
    while n:
        if n >= pri[i]:
            if n % pri[i] == 0:
                if n == pri[i] and not cnt ^ 1:
                    print(str(pri[i]))
                    break
                elif n == pri[i]:
                    print('*' + str(pri[i]))
                    break
                n //= pri[i]
                if not cnt ^ 1:
                    print(pri[i], end='')
                else:
                    print('*' + str(pri[i]), end='')
                cnt += 1
            else:
                i += 1
        else:
            i += 1

if __name__ == '__main__':
    fun()
    fun2()
    while True:
        n = int(input())
        fun3(n)

 

喜欢这篇文章吗,不妨分享给朋友们吧!

科学是第一生产力