Use Python programming language to realize Armstrong number check

##一. What is the Armstrong number?

If a positive integer is equal to the sum of the cubes of its numbers, then the number is called an Armstrong number (also known as a narcissistic number).

A positive integer is called the Armstrong order.

Example:

abcd... = an + bn + cn + dn + ...

If it is a 3-digit Armstrong number, the sum of the cubes of each number is equal to the number itself. E.g:

153 = 1*1*1 + 5*5*5 + 3*3*3  // 153是一个阿姆斯特朗数。

##二、Case

1. Check the Armstrong number (3 digits)

Example:

# 检查该数字是否为阿姆斯壮数字的Python程序

# 接受用户的输入
num = int(input("输入一个数字: "))

# 初始化sum
sum = 0

# 求出每个数字的立方和
temp = num
while temp > 0:
   digit = temp % 10
   sum += digit ** 3
   temp //= 10

# 显示结果
if num == sum:
   print(num,"是阿姆斯特朗数")
else:
   print(num,"不是阿姆斯特朗数")

Output 1

Output 2

Code analysis:

The user is required to enter a number, and then to check whether it is an Armstrong number, the cube sum of each number needs to be calculated.

Therefore, initialize the sum to 0 and use the modulo operator (%) to get each number. The remainder of dividing a number by 10 is the last digit of the number. Use the exponential operator to get the cube.

Finally, the sum is compared with the original number, and it is concluded that if they are equal, it is an Armstrong number.

2. The check is Armstrong’s n-digit number

Example:

num = 1634

# 将num变量更改为string
# 并计算出长度(位数)
order = len(str(num))

# 初始化 sum
sum = 0

# 求出每个数字的立方和
temp = num
while temp > 0:
    digit = temp % 10
    sum += digit ** order
    temp //= 10

# 显示结果
if num == sum:
    print(num, "是阿姆斯特朗数")
else:
    print(num, "不是阿姆斯特朗数")

operation result:

Note:

Readers can change the value of num in the source code, and then run it again to test it.

3. Find Armstrong numbers in integers

Example:

# Python程序在整数中查找阿姆斯特朗数

lower = 100
upper = 2000

for num in range(lower, upper + 1):

    # order 个数
    order = len(str(num))

    # 初始化 sum
    sum = 0

    temp = num
    while temp > 0:
        digit = temp % 10
        sum += digit ** order
        temp //= 10

    if num == sum:
        print(num)

operation result:

Note:

The lower limit of 100 is set in the variable lower, and the upper limit of 2000 is set in the variable upper.

A for loop is used to iterate from the variable lower to upper. In the iteration, the value of lower is increased by 1, and it is checked whether it is an Armstrong number.

You can change the scope and test by changing the variables lower and upper. The variable lower should be less than upper for the program to run normally.

Three, summary

Based on the basics of Python, this article introduces what Armstrong numbers are and how to judge them. Check Armstrong numbers, check Armstrong numbers, and find Armstrong numbers in integers. Effective analysis is carried out through case analysis, code demonstration, and effect display.

Using Python language can make readers better understand. For the problems and difficulties encountered in actual projects, effective solutions are provided for readers' reference.

The code is very simple, I hope it can help readers learn better.

If you want to learn more about front-end, Python crawler, big data and other computer knowledge, please go to: http://pdcfighting.com/ To learn more about Python web crawler and data mining, you can go to the professional website: http://pdcfighting.com /

Guess you like

Origin blog.csdn.net/pdcfighting/article/details/113913857