I just recently started reading the python.org documents and it looked like a really interesting language. Now, since Visual Studio 2017 supports Python, I decided to finally actually start learning it. My friend challenged me to write a program that checks if a number is an Armstrong number.
import sys def isArmstrong(number): arr = str(number) count = len(arr) res = 0 for x in range(0, count): res += int(arr[x]) ** count return res == number if len(sys.argv) > 1: arg = sys.argv print(arg + " is an armstrong number: " + str(isArmstrong(int(arg)))) else: print("No arguments passed! :-(");
Now this is the first solution that I could come up with. As you can see, it converts the number to a string to determine the amount of digits, and then handle them individually. Is this the smartest way to do it, or can it be done without converting to a string? If not: Is there anything I can do to improve this code performance-wise?