-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharmstrong_range.py
More file actions
49 lines (37 loc) · 1.11 KB
/
Copy patharmstrong_range.py
File metadata and controls
49 lines (37 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Armstrong Numbers
Check if a number is an armstrong number
(sum of the cubes of the digits equal to the number itself)
Print al armstrong numbers in a given range.
Made by Dick Stada, the Netherlands, May 31st, 2018
"""
from re import match
# Input check with regular expression:
pattern = "^[0-9]+$"
while True:
first_nmbr = input("Enter first number: ")
if match(pattern, first_nmbr):
break
else:
print("No number. Please try again.")
while True:
last_nmbr = input("Enter last number: ")
if match(pattern, last_nmbr) and int(last_nmbr) > int(first_nmbr):
break
else:
print("Wrong. Try again.")
print("Armstrong numbers in the range from {0} to {1}: ".format(first_nmbr, last_nmbr))
nmbr_list = []
def check_armstrong(number):
exponent = len(str(number))
sum = 0
for n in str(number):
sum += int(n)**exponent
if sum == number:
nmbr_list.append(number)
for i in range(int(first_nmbr), int(last_nmbr)+1):
check_armstrong(i)
if len(nmbr_list) == 0:
print("No numbers found.")
else:
for x in nmbr_list:
print(x)