Trong ví dụ này, bạn sẽ học cách kiểm tra xem một số nguyên chữ N có phải là số Armstrong hay không.
Để hiểu ví dụ này, bạn nên có kiến thức về các chủ đề lập trình Python sau:
- Python nếu ... tuyên bố khác
- Python trong khi vòng lặp
Một số nguyên dương được gọi là số lượng đơn đặt hàng n nếu
abcd... = an + bn + cn + dn + ...
Trong trường hợp số lượng 3 chữ số của Armstrong, tổng khối của mỗi chữ số bằng chính với số lượng. Ví dụ:
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.
Mã nguồn: Kiểm tra số Armstrong [cho 3 chữ số]
# Python program to check if the number is an Armstrong number or not
# take input from the user
num = int[input["Enter a number: "]]
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print[num,"is an Armstrong number"]
else:
print[num,"is not an Armstrong number"]
Đầu ra 1
Enter a number: 663 663 is not an Armstrong number
Đầu ra 2
Enter a number: 407 407 is an Armstrong number
Ở đây, chúng tôi yêu cầu người dùng cho một số và kiểm tra xem đó có phải là số Armstrong không.
Chúng ta cần tính toán tổng của khối lập phương của mỗi chữ số. Vì vậy, chúng tôi khởi tạo tổng thành 0 và lấy từng số chữ số bằng cách sử dụng toán tử mô đun %. Phần còn lại của một số khi nó được chia cho 10 là chữ số cuối cùng của số đó. Chúng tôi lấy các khối bằng cách sử dụng toán tử số mũ.
Cuối cùng, chúng tôi so sánh tổng với số gốc và kết luận rằng đó là số Armstrong nếu chúng bằng nhau.
Mã nguồn: Kiểm tra số lượng n chữ số của Armstrong
num = 1634
# Changed num variable to string,
# and calculated the length [number of digits]
order = len[str[num]]
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
# display the result
if num == sum:
print[num,"is an Armstrong number"]
else:
print[num,"is not an Armstrong number"]
Bạn có thể thay đổi giá trị của num trong mã nguồn và chạy lại để kiểm tra nó.
Cải thiện bài viết
Lưu bài viết
Cho một số X, xác định xem số đã cho có phải là số Armstrong hay không. Một số nguyên dương của n chữ số được gọi là số lượng đơn đặt hàng n [thứ tự là số chữ số] nếu.n digits is called an Armstrong number of order n [order is number of digits] if.
abcd... = pow[a,n] + pow[b,n] + pow[c,n] + pow[d,n] + ....
Example:
Input : 153 Output : Yes 153 is an Armstrong number. 1*1*1 + 5*5*5 + 3*3*3 = 153 Input : 120 Output : No 120 is not a Armstrong number. 1*1*1 + 2*2*2 + 0*0*0 = 9 Input : 1253 Output : No 1253 is not a Armstrong Number 1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723 Input : 1634 Output : Yes 1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1634
Python
def
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.0
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.2
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.3
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.6
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.7
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.8
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.9
# Python program to check if the number is an Armstrong number or not
# take input from the user
num = int[input["Enter a number: "]]
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print[num,"is an Armstrong number"]
else:
print[num,"is not an Armstrong number"]
0153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.2
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.3
# Python program to check if the number is an Armstrong number or not
# take input from the user
num = int[input["Enter a number: "]]
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print[num,"is an Armstrong number"]
else:
print[num,"is not an Armstrong number"]
4 # Python program to check if the number is an Armstrong number or not
# take input from the user
num = int[input["Enter a number: "]]
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print[num,"is an Armstrong number"]
else:
print[num,"is not an Armstrong number"]
5 153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.6
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.7
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.8
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.9
Enter a number: 663 663 is not an Armstrong number2
Enter a number: 663 663 is not an Armstrong number3
Enter a number: 663 663 is not an Armstrong number3
# Python program to check if the number is an Armstrong number or not
# take input from the user
num = int[input["Enter a number: "]]
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print[num,"is an Armstrong number"]
else:
print[num,"is not an Armstrong number"]
5Enter a number: 663 663 is not an Armstrong number6__
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.9
Enter a number: 407 407 is an Armstrong number5
Enter a number: 663 663 is not an Armstrong number7
Enter a number: 663 663 is not an Armstrong number2
Enter a number: 663 663 is not an Armstrong number3__
def
num = 1634
# Changed num variable to string,
# and calculated the length [number of digits]
order = len[str[num]]
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
# display the result
if num == sum:
print[num,"is an Armstrong number"]
else:
print[num,"is not an Armstrong number"]
9153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
abcd... = pow[a,n] + pow[b,n] + pow[c,n] + pow[d,n] + ....1
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.6
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
abcd... = pow[a,n] + pow[b,n] + pow[c,n] + pow[d,n] + ....5
abcd... = pow[a,n] + pow[b,n] + pow[c,n] + pow[d,n] + ....6
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.6
abcd... = pow[a,n] + pow[b,n] + pow[c,n] + pow[d,n] + ....9
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.8
abcd... = pow[a,n] + pow[b,n] + pow[c,n] + pow[d,n] + ....1
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
abcd... = pow[a,n] + pow[b,n] + pow[c,n] + pow[d,n] + ....1
Input : 153 Output : Yes 153 is an Armstrong number. 1*1*1 + 5*5*5 + 3*3*3 = 153 Input : 120 Output : No 120 is not a Armstrong number. 1*1*1 + 2*2*2 + 0*0*0 = 9 Input : 1253 Output : No 1253 is not a Armstrong Number 1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723 Input : 1634 Output : Yes 1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 16344
# Python program to check if the number is an Armstrong number or not
# take input from the user
num = int[input["Enter a number: "]]
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print[num,"is an Armstrong number"]
else:
print[num,"is not an Armstrong number"]
0153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.8
Enter a number: 407 407 is an Armstrong number5
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
Enter a number: 407 407 is an Armstrong number5
Enter a number: 663 663 is not an Armstrong number3
Enter a number: 663 663 is not an Armstrong number3
The given number 153 is armstrong number2
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.9
The given number 153 is armstrong number5
def
The given number 153 is armstrong number7
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
abcd... = pow[a,n] + pow[b,n] + pow[c,n] + pow[d,n] + ....1
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
def
1153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
def
3153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
def
5153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
def
7153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.6
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
abcd... = pow[a,n] + pow[b,n] + pow[c,n] + pow[d,n] + ....5
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.02
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.6
abcd... = pow[a,n] + pow[b,n] + pow[c,n] + pow[d,n] + ....9
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.8
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.07
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
def
3# Python program to check if the number is an Armstrong number or not
# take input from the user
num = int[input["Enter a number: "]]
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print[num,"is an Armstrong number"]
else:
print[num,"is not an Armstrong number"]
4 The given number 153 is armstrong number2
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.8
def
7153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
def
7Input : 153 Output : Yes 153 is an Armstrong number. 1*1*1 + 5*5*5 + 3*3*3 = 153 Input : 120 Output : No 120 is not a Armstrong number. 1*1*1 + 2*2*2 + 0*0*0 = 9 Input : 1253 Output : No 1253 is not a Armstrong Number 1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723 Input : 1634 Output : Yes 1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 16344
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.17
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.8
def
3153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
def
3Enter a number: 663 663 is not an Armstrong number3
Enter a number: 663 663 is not an Armstrong number3
The given number 153 is armstrong number2
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.9
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.27
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.30
Enter a number: 407 407 is an Armstrong number5
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.33
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.34
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.35
Enter a number: 407 407 is an Armstrong number5
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.38
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.34
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.35
Độ phức tạp về thời gian: O [[logn] 2]O[[logn]2]
Không gian phụ trợ: O [1] O[1]
Phương pháp: Một số nguyên dương được gọi là số Armstrong nếu số lượng 3 chữ số của Armstrong, tổng số khối của mỗi chữ số bằng chính số. Ví dụ, 153 là một số Armstrong vì & nbsp; & nbsp; 153 = 1*1*1 + 5*5*5 + 3*3*3 & nbsp; & nbsp; Vòng lặp trong khi lặp lại như đầu tiên, nó kiểm tra xem số không bằng 0 hay không. Nếu nó không bằng 0 thì hãy nhập vào vòng lặp và tìm lời nhắc của số EX: 153%10 đưa ra lời nhắc 3. Trong bước tiếp theo, hãy thêm khối của một số vào SUM1 [3*3*3]. Sau đó, bước đưa ra thương số của số [153 // 10 = 15]. Vòng lặp này sẽ tiếp tục cho đến khi số đã cho bằng 0. & NBSP;A positive integer is called an Armstrong number if an Armstrong number of 3 digits, the sum of cubes of each digit
is equal to the number itself. For example, 153 is an Armstrong number because
153 = 1*1*1 + 5*5*5 + 3*3*3
The while loop iterates like first, it checks if the number is not equal to zero or not. if it is not equal to zero then enter into the loop and find the reminder of number ex: 153%10 gives reminder 3. In the next step add the cube of a number to the sum1[3*3*3]. Then the step gives the quotient of the number [153//10=15]. this loop will continue till the
given number is equal to zero.
Python3
abcd... = pow[a,n] + pow[b,n] + pow[c,n] + pow[d,n] + ....1
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4 ________ 133 & nbsp;
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.44
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.46
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.47
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.49
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.50
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.51
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.52
def
7
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.6
abcd... = pow[a,n] + pow[b,n] + pow[c,n] + pow[d,n] + ....5
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.57
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.6
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.7
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.07
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
abcd... = pow[a,n] + pow[b,n] + pow[c,n] + pow[d,n] + ....1
# Python program to check if the number is an Armstrong number or not
# take input from the user
num = int[input["Enter a number: "]]
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print[num,"is an Armstrong number"]
else:
print[num,"is not an Armstrong number"]
4 The given number 153 is armstrong number2
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
def
7153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.70
Input : 153 Output : Yes 153 is an Armstrong number. 1*1*1 + 5*5*5 + 3*3*3 = 153 Input : 120 Output : No 120 is not a Armstrong number. 1*1*1 + 2*2*2 + 0*0*0 = 9 Input : 1253 Output : No 1253 is not a Armstrong Number 1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723 Input : 1634 Output : Yes 1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 16344
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.72
Enter a number: 663 663 is not an Armstrong number7
Enter a number: 663 663 is not an Armstrong number7
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.75
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
abcd... = pow[a,n] + pow[b,n] + pow[c,n] + pow[d,n] + ....1
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
The given number 153 is armstrong number5
Enter a number: 663 663 is not an Armstrong number3
Enter a number: 663 663 is not an Armstrong number3
The given number 153 is armstrong number2
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.2
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.44
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.4
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.87
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.34
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.50
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.91
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.92
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.93
Enter a number: 407 407 is an Armstrong number2
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.95
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.7
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.1
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.34
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.50
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.91
153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.92
# Python program to check if the number is an Armstrong number or not
# take input from the user
num = int[input["Enter a number: "]]
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print[num,"is an Armstrong number"]
else:
print[num,"is not an Armstrong number"]
02Enter a number: 407 407 is an Armstrong number2
Đầu ra
The given number 153 is armstrong number
Vui lòng tham khảo đầy đủ bài viết về chương trình cho các số Armstrong để biết thêm chi tiết!
Độ phức tạp về thời gian: O [logn]O[logn]
Không gian phụ trợ: O [1] O[1]