1. 程式人生 > 實用技巧 >斐波那契數列,質素

斐波那契數列,質素

# 列表構建斐波那契數列
def foo(num):
    foo = []
    a = 0
    b = 1
    for i in range(num):
        foo.append(b)
        a, b = b, a + b
    return foo

res = foo(10)
# print(res)
# 遞迴方式獲取斐波那契數列
lis = []
for i in range(10):
    if i == 0 or i == 1:
        lis.append(1)
    else:
        lis.append(lis[i-2]+lis[i-1])
# print(lis)
# 獲取2到100的質素(3,5,7,11,13)質素能被1和本身相除的數
data = []
for i in range(2, 100):
    for j in range(2, i):
        if i % j == 0:
            break
    else:
        data.append(i)
print(data)
# 水仙花數
153 = 1**3 + 5**3 + 3**3
import math
for i in range(100, 1000):
    x = math.floor(i / 100)
    y = math.floor((i - x * 100)/10)
    z 
= math.floor(i - (x*100 + y*10)) k = x**3 + y**3 + z**3 if i == k: print(i)
import random

def red_package(total, num):
    total = float(total)
    num = int(num)
    min = 0.01
    if num < 1:
        return
    if num == 1:
        print("第%d個人拿到紅包金額為:%.2f", (num, total))
        
return i = 1 while i < num: max = total - min*(num-i) k = int((num-i)/2) if num - i <= 2: k = num - i max = max/k money = random.randint(int(min*100), int(max*100)) money = float(money)/100 total = total - money print("第%d個人拿到的紅包數為:%.2f,餘額為:%.2f" % (i, money, total)) i += 1 print("第%d個人拿到的紅包數為:%.2f,餘額為:%.2f" % (i, total, 0.0)) if __name__ == '__main__': total = input("輸入紅包的總金額:") num = input("輸入紅包的數量: ") red_package(total, num)