Python 基础学习1

Python 基础学习1

import math

"""
水仙花数是各位立方和等于这个数本身的数
如: 153 = 1**3 + 5**3 + 3**3
"""


def lily():
    for x in range(10000):
        result = 0
        for item in list(str(x)):
            result += math.pow(int(item), 3)
        if x == result:
            print(x)

"""
完美数是除自身外其他所有因子的和正好等于这个数本身的数
例如: 6 = 1 + 2 + 3, 28 = 1 + 2 + 4 + 7 + 14
"""


def perfect():
    for x in range(10000):
        result = 0
        for y in range(1, int(math.sqrt(x)) + 1):
            if x % y == 0:
                result += y
                if y > 1 and x / y != y:
                    result += x / y
        if x == result:
            print(x)

"""
1只公鸡5元 1只母鸡3元 3只小鸡1元 用100元买100只鸡
问公鸡 母鸡 小鸡各有多少只
"""


def chicken():
    a = 5
    b = 3
    c = 1/3

    for x in range(0, int(100 / a) + 1):
        for y in range(0, int(100 / b) + 1):
            z = 100 - x - y
            if a * x + b * y + c * z == 100 and x + y + z == 100:
                print(str(x) + "," + str(y) + "," + str(z))


"""
输出斐波那契数列的前20个数
1 1 2 3 5 8 13 21 ...
"""


def fibonacci(arr):
    temp = list(arr)
    temp.append(temp[len(temp) - 1] + temp[len(temp) - 2])
    if len(temp) == 20:
        print(temp)
        return
    fibonacci(temp)

def fibonacci2():
    a = 0
    b = 1
    for _ in range(20):
        (a, b) = (b, a + b)
        print(a, end=' ')

if __name__ == '__main__':
    init = [1, 1]
    fibonacci(init)
    fibonacci2()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值