Find the number of daffodils and determine whether a number is prime

1. Find the number of daffodils:

concept:The number of daffodils refers to a 3-digit number, and the sum of the powers of 3 of each digit is equal to itself (for example: 1^3 + 5^3+ 3^3 = 153).

Program realization

Use the most basic while if to achieve the number of daffodils
i = 100
while i < 1000:#循环
    if (i // 100) ** 3 + ((i // 10) % 10) ** 3 + (i % 10) ** 3 == i:#判断求出的百位,十位,个位的三次方相加是否等于i
            print(i)#如果相等输出i
    i += 1#i+1循环
else:
    print('输出完毕')

2. Find prime numbers

concept:A prime number refers to a natural number that has no other factors except 1 and itself among the natural numbers greater than 1. (Meaning that this number can only be divided by itself and 1, other numbers cannot be divided by all)

Program realization

Use the most basic while if-else to determine whether this number is a prime number
a = int(input("请输入一个大于一的整数:"))#输入一个大于一的整数
b = 2
s = 0
while b <= a:
    c = a % b#判断从2开始是否除不尽,有余数
    if c == 0:
        s += 1#除尽加一,证明还有其他数是他的因数
    b += 1
if s > 1:
    print("这个数不是质数")
else:
    print("这个数是质数")

Guess you like

Origin blog.csdn.net/qq_45261963/article/details/107298745
Recommended