We are to find Armstrong numbers in a given list of integers.
An Armstrong number (also known as narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits.
Steps for one number:
- Determine the number of digits (n) in the number.
- Separate each digit.
- Raise each digit to the power n and sum them.
- If the sum equals the original number, then it is an Armstrong number.
However, note: the problem says we are given a list of integers. We must apply this check to every integer in the list and return (or print) the Armstrong numbers.
Implementation:
We can write a helper function to check if a single number is an Armstrong number.
Let's design:
def is_armstrong(num):
if num < 0:
# Typically Armstrong numbers are defined for non-negative integers. We can consider only positive, but sometimes 0 is included.
# Let's handle non-negative. For negative, we might consider absolute value? But usually, Armstrong numbers are defined for natural numbers (positive integers) and 0.
# Since the problem says "list of integers", we might have negative. We can return False for negative numbers because the definition usually applies to non-negative.
# Alternatively, we can take absolute value and then check? But note: the definition says "its own digits", and negative numbers have a minus sign which is not a digit.
# So, we'll consider only non-negative numbers.
return False
# Convert to string to get digits, or use math.
# Let's use string for simplicity.
str_num = str(num)
n = len(str_num)
total = 0
for digit_str in str_num:
digit = int(digit_str)
total += digit ** n
return total == num
But note: if the number is 0, then it has 1 digit and 0^1 = 0, so 0 is an Armstrong number.
Now, for the main function:
def find_armstrong_numbers(lst):
armstrong_list = []
for num in lst:
if is_armstrong(num):
armstrong_list.append(num)
return armstrong_list
However, we must consider that the list might have duplicates? The problem doesn't specify, but we are to return the Armstrong numbers in the list, so if a number appears multiple times and is Armstrong, we return it multiple times?
The problem says "find the Armstrong numbers" from the list. It doesn't specify to remove duplicates, so we keep the order and duplicates.
Example:
Input: [153, 123, 370, 371, 407]
Output: [153, 370, 371, 407]
But note: 123 is not an Armstrong number because 1^3+2^3+3^3 = 1+8+27=36 != 123.
Let's test with some known Armstrong numbers:
153: 3 digits -> 1^3 + 5^3 + 3^3 = 1+125+27=153 -> yes
370: 3^3+7^3+0^3=27+343+0=370 -> yes
371: 3^3+7^3+1^3=27+343+1=371 -> yes
407: 4^3+0^3+7^3=64+0+343=407 -> yes
Also, 1-digit numbers: for example, 5: 5^1=5 -> so every single digit number is an Armstrong number?
Let's check: 0,1,2,...,9 are all Armstrong numbers because they have one digit and the digit raised to 1 is itself.
So in a list, if we have [1,2,3,4,5] they are all Armstrong numbers.
Therefore, our function should return all single-digit numbers as well.
But note: what about 10?
10: 2 digits -> 1^2 + 0^2 = 1+0=1, not 10 -> not Armstrong.
Edge:
num=0 -> '0' -> 1 digit -> 0^1=0 -> Armstrong.
Let's code accordingly.
However, note that the problem might have large numbers. But the number of digits is at most the length of the string, and we are raising each digit to that power.
The maximum digit is 9, so 9n. For n=20, 920 is about 1.21e19, which is within Python's integer range but might be large. But the number of digits is the same as the number of digits of the original number, so for a 20-digit number, we are summing 20 terms, each at most 9**20. That's acceptable in Python.
But note: for very large numbers, the exponentiation might be expensive. However, the number of digits is logarithmic in the number, so for a number with d digits, we do d exponentiations of base at most 9. This is manageable.
Alternatively, we can compute without converting to string by using math:
n = 0
temp = num
while temp > 0:
n += 1
temp //= 10
But then we have to extract digits again. So string conversion is simpler.
However, note: if the number is 0, the while loop would not run (because temp=0) so n would be 0, but we want n=1 for 0. So we have to handle 0 separately.
So the string method is straightforward.
Let's code accordingly.
But note: what if the input list is empty? Then we return an empty list.
Let's write the code accordingly.
However, the problem says "write a function that solves this problem", so we'll write a function that takes a list and returns a list.
But wait: the problem says "find the Armstrong numbers", so we can also print them? The problem says "write a function", so we return.
Let me write:
def find_armstrong_numbers(numbers):
result = []
for num in numbers:
if num < 0:
# Skip negative, but if we want to consider absolute value? The problem says "its own digits", and negative numbers have a minus sign which is not a digit.
# So we skip.
continue
s = str(num)
n = len(s)
total = 0
for ch in s:
total += int(ch) ** n
if total == num:
result.append(num)
return result
But note: what if the number is 0? It works because str(0) is "0", n=1, 0**1=0.
However, note that in some definitions, Armstrong numbers are only for positive integers. But 0 is sometimes included. The problem doesn't specify, but since 0 is an integer and the condition holds, we include.
But wait: the problem says "list of integers", so we must handle 0.
However, note: the example list might not have 0.
Let me test with [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 153, 370, 371, 407] -> should return [0,1,2,3,4,5,6,7,8,9,153,370,371,407]
But note: 0 is included? Yes, because 0 has one digit and 0^1=0.
However, some definitions of Armstrong numbers exclude 0 because they are for natural numbers (positive integers). But the problem says "a number that is equal to the sum of its own digits...", and 0 fits.
But let's check: if we consider 0 as having 1 digit? Yes, because we write 0 as one digit. So it's an Armstrong number.
However, note that some might argue that 0 has no digits? But in base 10, we represent 0 as the digit '0'. So it has one digit.
So we keep 0.
But wait: what about negative numbers? We skip because we cannot have a negative digit. So we return False for negative.
However, note: if the input list has negative numbers, we are skipping them. But the problem says "list of integers", so we might have negative. We'll not consider them as Armstrong.
But what if the problem expects to consider absolute value? The problem statement doesn't say. Since the definition typically applies to non-negative, we'll stick to non-negative.
Let's adjust: we can also handle negative by taking absolute value and then checking? But then the sum would be positive and cannot equal the negative number. So negative numbers cannot be Armstrong.
So we are safe.
However, note: the problem might expect to return an empty list for negative numbers? We are already skipping them.
But what if the list has a negative number and its absolute value is an Armstrong number? For example, -153?
We would skip it because we check num < 0 and skip. But note: the condition for Armstrong is about the digits of the number. The digits of -153 are the same as 153? But we have a minus sign. So we don't consider.
So we are consistent.
Let's write the function accordingly.
But note: the problem says "write a function", so we'll write:
def find_armstrong_numbers(lst):
armstrongs = []
for num in lst:
if num < 0:
continue
num_str = str(num)
n = len(num_str)
total = sum(int(digit) ** n for digit in num_str)
if total == num:
armstrongs.append(num)
return armstrongs
This is concise.
However, note: we are using a generator expression inside sum. That's efficient.
But wait: what if the number is very large? The exponentiation might be expensive, but the number of digits is limited by the string length, which for a very large number is still manageable.
Alternatively, we can precompute the powers for each possible digit (0-9) for a given n? But n changes per number. So we can compute the power for each digit for that n. But note: for a given n, we are computing the same power for the same digit multiple times if the same digit appears multiple times. But in one number, the same digit might appear multiple times. We can cache the power for each digit for that n? But n is the same for all digits in the same number. So we can compute a dictionary for the current n:
powers = {str(d): d**n for d in range(10)}
But then we are converting the digit from string to int and then using the dictionary? Or we can just use the string digit as key? But note: the digit in the string is a character, so we can do:
powers = {d: int(d)**n for d in '0123456789'}
But then we are computing 10 exponentiations per number, but we have at most 10 digits in a number? Actually, the number of distinct digits in a number is at most 10, but we are computing for all 10 digits. However, we are doing it for every number. But the n changes per number, so we cannot cache across numbers.
Alternatively, we can compute for each digit in the string by converting to int and then exponentiating. That's what we are doing.
Since the number of digits is the same as the length of the string, and the string length is at most, say, 100 (for a 100-digit number), and we do one exponentiation per digit, that's 100 exponentiations. But note: 9**100 is an extremely large number, but Python can handle it. However, the number itself (the original number) is also that big. But the list of integers in Python can have arbitrarily large integers? Yes, but the exponentiation and sum might be expensive for very large n.
But note: the number of digits n is the length of the number. For a number with n digits, the maximum value of the sum is n * 9n. But the original number is at least 10(n-1) (for n>=1). So for n>=2, we have:
n * 9n vs 10(n-1)
For n=2: 281=162, 10^1=10 -> 162>10, so possible.
For n=3: 3729=2187, 10^2=100 -> 2187>100.
But as n grows, 9n grows exponentially with base 9, and 10(n-1) grows with base 10. So for large n, 9n becomes much smaller than 10(n-1). Actually, let's compare:
We require: n * 9n >= 10(n-1) for there to be any n-digit Armstrong numbers? Not exactly, because the sum is at most n9n, and the number is at least 10(n-1). So if n9n < 10(n-1), then no n-digit number can be Armstrong.
Let's check when n*9n < 10(n-1):
n=1: 19=9, 10**0=1 -> 9>=1 -> possible.
n=2: 281=162, 10^1=10 -> 162>=10 -> possible.
n=3: 3729=2187, 10^2=100 -> 2187>=100 -> possible.
n=4: 46561=26244, 10^3=1000 -> 26244>=1000 -> possible.
n=5: 559049=295245, 10^4=10000 -> 295245>=10000 -> possible.
n=6: 6531441=3188646, 10^5=100000 -> 3188646>=100000 -> possible.
n=7: 74782969=33480783, 10^6=1000000 -> 33480783>=1000000 -> possible.
n=8: 843046721=344373768, 10^7=10000000 -> 344373768>=10000000 -> possible.
n=9: 9387420489=3486784401, 10^8=100000000 -> 3486784401>=100000000 -> possible.
n=10: 103486784401=34867844010, 10^9=1000000000 -> 34867844010>=1000000000 -> possible.
n=11: 1131381059609=345191655699, 10^10=10000000000 -> 345191655699>=10000000000 -> possible.
n=12: 12282429536481=3389154437772, 10^11=100000000000 -> 3389154437772>=100000000000 -> possible.
n=13: 132541865828329=33044255768277, 10^12=1000000000000 -> 33044255768277>=1000000000000 -> possible.
n=14: 1422876792454961=320275094369454, 10^13=10000000000000 -> 320275094369454>=10000000000000 -> possible.
n=15: 15205891132094649=3088366981419735, 10^14=100000000000000 -> 3088366981419735>=100000000000000 -> possible.
n=16: 161853020188851841=29648323021629456, 10^15=1000000000000000 -> 29648323021629456>=1000000000000000 -> possible.
n=17: 1716677181699666569=283512088894331673, 10^16=10000000000000000 -> 283512088894331673>=10000000000000000 -> possible.
n=18: 18150094635296999121=2701703435343984178, 10^17=100000000000000000 -> 2701703435343984178>=100000000000000000 -> possible.
n=19: 191350851717672992089=25666182635786849691, 10^18=1000000000000000000 -> 25666182635786849691>=1000000000000000000 -> possible.
n=20: 2012157665459056928801=243153309181138576020, 10^19=10000000000000000000 -> 243153309181138576020>=10000000000000000000 -> possible.
But note: the condition for existence of an Armstrong number of n digits is that the maximum sum (n*9n) must be at least the minimum n-digit number (10(n-1)). The above shows that for n up to 20, it holds. But for n=21:
n=21: 21 * 9**21 = 21 * 109418989131512359209 = 2297798771761759543389, and 10^20=100000000000000000000 -> 2.29e18 vs 1e20? Wait, 10^20 is 1e20, and 2.29e18 is less than 1e20. So for n=21, the maximum sum is less than the minimum 21-digit number. So there are no 21-digit Armstrong numbers.
Therefore, we only need to check numbers with up to 20 digits. But wait, that's for base 10. The known Armstrong numbers are only for up to 39 digits? Actually, the known Armstrong numbers are finite and there are only 88 in base 10. But for our function, we are given an arbitrary list. We don't know the size.
But note: if the number has more than 20 digits, then we can immediately return False without computing? Because we know that for n>=21, there are no Armstrong numbers in base 10. But wait, that's for the maximum sum. However, note that the maximum sum is n9**n, and for n>=21, n9n < 10(n-1). So indeed, no number with 21 or more digits can be an Armstrong number.
So we can add an optimization:
If the number of digits n >= 21, then return False.
But note: the problem doesn't specify the range of numbers. But we can use this fact.
However, let's be cautious: what about n=20? We saw that 209**20 is about 2.43e19, and the minimum 20-digit number is 10^19=1e19, so it's possible. And indeed, there are 20-digit Armstrong numbers? Actually, the largest known Armstrong number has 39 digits? Wait, I recall that there are only 88 Armstrong numbers in base 10, and the largest has 39 digits. But wait, that's for the definition where the number of digits is the power? Actually, the known list of narcissistic numbers (Armstrong numbers) in base 10 has 88 numbers, and the largest has 39 digits. But note: for n=39, 399**39 is less than 10^38? Let me check:
We need to check: n * 9n < 10(n-1) for n>=21? We did n=21 and it was less. For n=22, it will be even more so.
Actually, 9n grows as (9/10)n * 10n, and (9/10)n decays exponentially. So for large n, n*9n is approximately n * (9/10)^n * 10^n, and 10(n-1) = 10**n / 10. So we compare:
n * (9/10)^n * 10**n vs 10**n / 10
=> n * (9/10)^n vs 1/10
As n increases, (9/10)^n becomes very small. So for n>=21, n*(9/10)^n is less than 1/10? Let's test n=21:
(9/10)^21 = (0.9)^21 ≈ 0.10941898913151236 (since 9**21 = 109418989131512359209, and 10**21=1e21, so 9**21/10**21=0.109418989131512359209, then multiply by 21 gives about 2.2976, which is greater than 0.1? Wait, 2.2976 is greater than 0.1. So for n=21, n*(9/10)^21 ≈ 2.2976, which is >0.1. But we are comparing n*9**n and 10**(n-1). Actually, we have:
n*9**n = n * (9**n)
10**(n-1) = 10**n / 10
So we want: n*9**n >= 10**(n-1) => n * (9**n) >= 10**n / 10 => n * (9/10)**n >= 1/10.
For n=21: 21 * (0.9)^21 = 21 * 0.10941898913151236 = 2.2975985717607596, which is > 0.1.
So for n=21, it is still possible? But wait, we are comparing integers. The maximum sum for a 21-digit number is 21*9**21, and the minimum 21-digit number is 10^20. So we need:
21*9**21 >= 10^20?
We have 21*9**21 = 21 * 109418989131512359209 = 2297798771761759543389, and 10^20 = 100000000000000000000. So 2.29e18 vs 1e20? Actually, 2.29e18 is less than 1e20. Wait, I made a mistake in the exponent:
9**21 = 109418989131512359209 ≈ 1.094e20? Let's count digits: 9**21 has 21 digits? Actually, 9**21 is 109418989131512359209, that's 21 digits. So 9**21 is about 1.094e20. Then 21 * 9**21 is about 2.297e21? No, 1.094e20 * 21 = 2.2974e21? Wait, 1.094e20 * 20 = 2.188e21, and plus 1.094e20 -> 2.2974e21. But then we compare to 10^20? That's 1e20. So 2.297e21 > 1e20. So it is greater.
But wait, the minimum 21-digit number is 10^20 (which is 1 followed by 20 zeros, so 21 digits). And the maximum sum is 21*9**21, which is about 2.297e21. So indeed, for n=21, the maximum sum is larger than the minimum number. So it is possible? But then why did we think n=21 is impossible?
I think I made a mistake in the comparison. We require that the maximum sum (n*9**n) is at least the minimum n-digit number (10**(n-1)). For n=21, 10**(20) = 1e20, and n*9**n is about 2.297e21, which is much larger. So the condition holds.
Then when does it fail? We need to find n such that n*9**n < 10**(n-1).
Let's try n=60:
9**60 is (9^10)^6, 9^10=3486784401, so 9^60 is about 3.486e9^6? That's not helpful.
Alternatively, take log:
We want: log10(n) + n*log10(9) < n-1
=> log10(n) + n*log10(9) - n + 1 < 0
=> log10(n) + n*(log10(9)-1) + 1 < 0
log10(9) ≈ 0.9542425094, so log10(9)-1 ≈ -0.0457574906.
So we have: log10(n) - 0.0457574906*n + 1 < 0
For large n, the term -0.0457574906*n dominates, so for sufficiently large n, it will be negative.
Let's test n=100:
log10(100)=2, then 2 - 0.0457574906*100 + 1 = 2 - 4.57574906 + 1 = -1.57574906 < 0.
So for n=100, it is negative, meaning n*9**n < 10**(n-1).
But we need the smallest n where it fails. We can try n=50:
log10(50)≈1.69897, then 1.69897 - 0.0457574906*50 + 1 = 1.69897 - 2.28787453 + 1 = 0.41109547 >0.
n=60: log10(60)≈1.778151, then 1.778151 - 0.0457574906*60 + 1 = 1.778151 - 2.745449436 + 1 = 0.032701564 >0.
n=61: log10(61)≈1.78533, then 1.78533 - 0.0457574906*61 + 1 = 1.78533 - 2.791206906 + 1 = -0.005876906 <0.
So for n=61, the inequality holds? Actually, we have:
n*9**n < 10**(n-1) when n>=61? But wait, we are checking the condition for existence: if n*9**n < 10**(n-1), then no n-digit Armstrong number exists.
So for n>=61, we can return False without computing.
But note: the known Armstrong numbers only go up to 39 digits. So for n>=40, we can return False? Actually, the known list says there are no Armstrong numbers with 40 or more digits? But the mathematical condition says that for n>=61, it's impossible. But for n between 40 and 60, it might be possible? However, it has been proven that there are only 88 Armstrong numbers in base 10, and the largest has 39 digits. So for n>=40, we know there are none.
But we are writing a general function. We don't want to rely on known list. We can use the condition:
If n>=61, then return False.
But wait, what about n=60? We saw that for n=60, the condition n*9**n >= 10**(n-1) holds? Actually, we computed:
n=60: log10(60) - 0.0457574906*60 + 1 = 0.0327 >0, so n*9**n > 10**(n-1). So it's possible in theory.
But it has been proven that there are no Armstrong numbers with 60 digits? Actually, the known list is complete up to 39 digits, and for 40 and above, it has been proven that there are none? I think the proof shows that there are no Armstrong numbers with more than 39 digits. But I'm not 100% sure.
However, for the purpose of this function, we can simply compute the sum for the given number. But if the number has, say, 1000 digits, then we are going to do 1000 exponentiations of 9 to the power 1000, which is huge and will take time and memory.
So we can use the condition: if n>=61, then return False. But note: the condition we derived is for the maximum sum to be less than the minimum number. But wait, we derived:
n*9**n < 10**(n-1) => no n-digit Armstrong number.
And we found that for n>=61, this holds? Actually, we computed for n=61: log10(61) - 0.0457574906*61 + 1 = -0.005876906 <0, so indeed for n=61, n*9**n < 10**(n-1). So for n>=61, we can safely return False.
But what about n=60? We had positive, so we cannot return False for n=60. But note: the known list says there are no 60-digit Armstrong numbers. But we are not using the known list, we are using the condition that the maximum sum is less than the minimum number. For n=60, the maximum sum is greater than the minimum number, so it's possible in theory. But in practice, it's been proven that there are none for n>=40? Actually, I recall that the proof shows that there are no Armstrong numbers with more than 39 digits. But I don't have the proof at hand.
To be safe, we can use the condition for n>=61, and for n<=60, we compute normally.
Alternatively, we can compute the condition for the given n:
if n >= 1 and n <= 60: compute the sum and check.
if n >= 61: return False.
But wait, what about n=0? That would be the number 0, which we handle separately.
So in code:
if num == 0:
return True # because 0 is an Armstrong number.
s = str(num)
n = len(s)
if n >= 61:
return False
total = 0
for digit in s:
total += int(digit) ** n
return total == num
But note: what if the number is negative? We skip.
However, note that the condition n>=61 is for base 10. But the problem is base 10.
But wait: the condition we derived is for base 10. So it's correct.
However, note that the condition n*9**n < 10**(n-1) is a necessary condition for there to be no n-digit Armstrong numbers. But it is not sufficient? Actually, if the maximum sum is less than the minimum n-digit number, then indeed no n-digit number can be Armstrong. So it's a sufficient condition to rule out.
So we can use:
if n >= 61: # because for n=61, we have n*9**n < 10**(n-1) and for larger n, the left side grows slower than the right side, so the inequality holds for all n>=61.
return False
But wait, we must check n=61 exactly? We have:
n=61: 61 * 9**61 vs 10**60.
We computed using logs: log10(61) + 61*log10(9) = 1.78533 + 61*0.9542425094 = 1.78533 + 58.2087890734 = 59.9941190734, which is less than 60? Actually, 59.994 < 60, so 61*9**61 < 10**60.
So for n=61, the maximum sum is less than the minimum 61-digit number. So no 61-digit Armstrong number.
Similarly for n>61, the left side (n*9**n) grows by a factor of 9 each time n increases by 1, but the right side (10**(n-1)) grows by a factor of 10. So the gap increases.
Therefore, we can safely return False for n>=61.
But note: what about n=60? We have:
n=60: 60*9**60 vs 10**59.
log10(60) + 60*log10(9) = 1.778151 + 60*0.9542425094 = 1.778151 + 57.254550564 = 59.032701564, which is less than 59? Wait, 59.0327 is greater than 59? Actually, 59.0327 > 59. So 60*9**60 > 10**59. So it's possible.
But note: the known list says there are no 60-digit Armstrong numbers. However, we are not using the known list, we are using the condition that the maximum sum must be at least the minimum number. That condition is necessary but not sufficient. So for n=60, we cannot rule out by this condition.
But wait, the condition we are using is: if the maximum possible sum (n*9**n) is less than the smallest n-digit number (10**(n-1)), then no Armstrong number exists. For n=60, the maximum sum is greater than the smallest number, so we cannot rule out.
However, it has been proven that there are no Armstrong numbers for n>=40? Actually, I think the proof is that there are only 88 Armstrong numbers in base 10, and the largest has 39 digits. So for n>=40, we know there are none. But we don't want to hardcode 40 because the proof might be complex.
Alternatively, we can use the condition for n>=61, and for n between 40 and 60, we still compute? But that would be expensive for n=60 because 9**60 is a 60-digit number? Actually, 9**60 has about 60*log10(9) ≈ 60*0.954 = 57.24 digits, so it's a 58-digit number? Wait, 9**60 = (9^10)^6, 9^10=3486784401 (10 digits), so 9^60 is about (3.486e9)^6 = 3.486^6 * 10^54, which is about 1.5e3 * 10^54 = 1.5e57, so 58 digits. But we are raising each digit (0-9) to the power 60, so 9**60 is a 58-digit number? Actually, 9**60 is exactly:
9^60 = 9^(60) = (9^30)^2. 9^30 = 42391158275216203514294433201 (32 digits). So 9^60 is 64 digits? Wait, we are squaring a 32-digit number, so up to 64 digits. But let's compute:
9^10 = 3486784401 (10 digits)
9^20 = (9^10)^2 = 3486784401^2 = 12157665459056928801 (20 digits)
9^30 = 9^20 * 9^10 = 12157665459056928801 * 3486784401 = 42391158275216203514294433201 (32 digits)
9^60 = (9^30)^2 = 42391158275216203514294433201^2 -> that's a 64-digit number? Actually, 32*2=64.
So 9**60 is a 64-digit number. And we are going to compute 60 of these? That's 60 multiplications of 64-digit numbers? That's expensive.
But note: we are only doing this for numbers that have 60 digits? That is, the input number has 60 digits. But the input list might have a 60-digit number. And we are going to compute 9**60, which is a 64-digit number, and then multiply by the count of 9's? Actually, we are summing for each digit. So if the number has 60 digits and they are all 9, then we are computing 60 times 9**60, which is 60 * (a 64-digit number). That's a 66-digit number? But the original number is 60 digits, so it cannot be equal. So we can break early if the partial sum already exceeds the number? But we are summing positive terms.
However, for a 60-digit number, the maximum sum is 60 * 9**60, which is about 60 * 1.0e57? Actually, 9**60 is about 1.0e57? Wait, 9**60 = (10**0.9542425094)**60 = 10**(60*0.9542425094) = 10**57.254550564, so about 1.79e57. Then 60 * 1.79e57 = 1.074e59. The minimum 60-digit number is 10^59. So the maximum sum is about 1.074e59, which is only 7.4% above the minimum. So it's possible that a 60-digit number could be Armstrong? But it has been proven that there are none for n>=40.
Given the complexity, and the fact that the known list is complete, we can simply compute for n<=60 and for n>=61 return False.
But wait, what about n=40 to 59? We are still computing for those. But note: the condition we derived for n>=61 is based on the maximum sum being less than the minimum number. For n=40, we have:
n=40: 40*9**40 vs 10**39.
9**40 = (9^20)^2 = 12157665459056928801^2 = about 1.476e38? Actually, 9^20 is about 1.215e19, squared is about 1.476e38. Then 40*1.476e38 = 5.904e39. And 10^39 = 1e39. So 5.904e39 > 1e39. So the condition does not rule out n=40.
But we know there are no 40-digit Armstrong numbers. So we cannot rule out by this condition for n=40.
Therefore, we have two options:
Option 1: Compute the sum for any n, but if n>=61, then we know it's impossible, so we can skip the computation.
Option 2: For n<=60, we compute, and for n>=61, we return False.
Since 60 is manageable? But 9**60 is a 64-digit number, and we are going to compute 60 such exponentiations? That's 60 exponentiations of 9 to the power 60? Actually, we are computing for each digit. But note: the digits are only 0-9, so we can precompute the powers for digits 0-9 for the given n. Then for each digit, we just look up.
So for a given n (<=60), we compute:
powers = [d**n for d in range(10)]
Then for each digit in the string, we convert to int and then use powers[digit].
But computing 10 exponentiations per number? And we are doing this for every number in the list. If the list has many large numbers, we might be doing many exponentiations.
Alternatively, we can cache the powers for each n we encounter? But n can be from 1 to 60. We can have a global cache? But the function might be called multiple times. We can use a closure or a class to cache. But the problem says "write a function", so we can use a global dictionary? But that's not clean.
However, note: the same n will appear for many numbers? For example, if the list has many 3-digit numbers, then we compute powers for n=3 once and reuse? But our function is for one list. We can compute the powers for each n we encounter in the list? But then we are caching within the function call.
Let me restructure:
We can group numbers by their number of digits? But then we have to process each group.
Alternatively, we can do:
for num in lst:
if num < 0: continue
s = str(num)
n = len(s)
if n >= 61:
continue # or return False for this num, but we are in a loop.
# Check if we have already computed powers for this n? We can have a dictionary in the function.
if n not in power_cache:
power_cache[n] = [d**n for d in range(10)]
total = 0
for ch in s:
total += power_cache[n][int(ch)]
if total == num:
result.append(num)
But note: the function may be called multiple times? The problem says "write a function", so we assume one call. But if we want to cache across calls, we can use a global variable or a default argument that is mutable.
However, the problem doesn't specify multiple calls. So we can do without caching if the list is not too large.
But if the list has many numbers with the same number of digits, then caching by n within the function call will help.
Let me do:
def find_armstrong_numbers(lst):
result = []
power_cache = {}
for num in lst:
if num < 0:
continue
s = str(num)
n = len(s)
if n >= 61:
continue # skip because impossible
if n not in power_cache:
power_cache[n] = [d**n for d in range(10)]
total = 0
for ch in s:
total += power_cache[n][int(ch)]
if total == num:
result.append(num)
return result
This way, for each distinct n (up to 60) we compute the 10 powers once.
But note: what about n=0? That would be the empty string for num=0? Actually, str(0) is "0", so n=1. So we don't have n=0.
Also, note that for n=1, the powers are [0,1,2,3,4,5,6,7,8,9] because d**1 = d.
This should work.
However, what about the number 0? It has n=1, and we compute powers[1][0] = 0, so total=0, and 0==0 -> True.
So it works.
But wait: what if the list has a number with leading zeros? For example, "00123"? But in integer form, 00123 is 123. So when we convert to string, we get "123". So no leading zeros.
So we are safe.
Let's test with a known list:
[153, 123, 370, 371, 407, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9474]
Note: 9474 is a 4-digit Armstrong number?
9^4=6561, 4^4=256, 7^4=2401, 4^4=256 -> 6561+256+2401+256 = 9474? 6561+256=6817, +2401=9218, +256=9474. Yes.
So we should get [153, 370, 371, 407, 0,1,2,3,4,5,6,7,8,9,9474]
But note: 10 is not.
So let's code accordingly.
However, note: the condition n>=61 is for base 10 and for numbers with exactly n digits. But what about numbers with leading zeros? We don't have leading zeros in the integer representation.
So the function:
def find_armstrong_numbers(lst):
result = []
power_cache = {}
for num in lst:
if num < 0:
continue
s = str(num)
n = len(s)
if n >= 61:
continue
if n not in power_cache:
power_cache[n] = [d**n for d in range(10)]
total = 0
for ch in s:
total += power_cache[n][int(ch)]
if total == num:
result.append(num)
return result
But wait: what if the number is 0? We have s="0", n=1, power_cache[1] = [0,1,2,...,9], then total = power_cache[1][0] = 0, and 0==0 -> True.
What about 1? s="1", n=1, total=power_cache[1][1]=1, True.
What about 10? s="10", n=2, power_cache[2] = [0,1,4,9,...,81] (0^2=0, 1^2=1, ...,9^2=81). Then total = power_cache[2][1] + power_cache[2][0] = 1+0=1, not 10 -> False.
What about 153? s="153", n=3, power_cache[3] = [0,1,8,27,64,125,216,343,512,729]. Then 1^3=1, 5^3=125, 3^3=27, total=1+125+27=153 -> True.
What about 9474? s="9474", n=4, power_cache[4] = [0,1,16,81,256,625,1296,2401,4096,6561]. Then 9^4=6561, 4^4=256, 7^4=2401, 4^4=256 -> 6561+256+2401+256=9474 -> True.
What about a 61-digit number? We skip.
But note: what if the number has 61 digits but is actually less than 10^60? That's impossible because a 61-digit number is at least 10^60. So if the string length is 61, then the number is at least 10^60.
So we are safe.
However, what about numbers with n=0? That would be the empty string? But num cannot be negative and we convert to string, so for num=0 we get "0". So n>=1.
Therefore, the function is complete.
But note: the condition n>=61 is an optimization. Without it, the function would still work for n=61, but it would be very slow and memory intensive because 9**61 is a huge number. So we keep the condition.
Alternatively, we can compute the condition on the fly:
if n >= 61:
return False # for this number.
But in the loop, we are continuing to the next number.
So the final function:
def find_armstrong_numbers(lst):
result = []
power_cache = {}
for num in lst:
if num < 0:
continue
s = str(num)
n = len(s)
if n >= 61:
continue
if n not in power_cache:
# Precompute the n-th power for digits 0-9
power_cache[n] = [d**n for d in range(10)]
total = 0
for ch in s:
total += power_cache[n][int(ch)]
if total == num:
result.append(num)
return result
However, note: what if the list has a number with n=60 and it's a huge number? We are computing 9**60 which is about 1.0e57, and we are summing up to 60 of these, which is about 6e58, which is within Python's integer range? Python integers are arbitrary precision, but the computation might be slow for such large exponents.
But 9**60 is computed once per n (if we have multiple numbers with 60 digits, we compute it once and cache). But note: 9**60 is a 60-digit number? Actually, 9**60 has about 60*log10(9) ≈ 57.25 digits, so it's a 58-digit number? Wait, we computed 9**60 earlier as having 64 digits? Let me check:
log10(9**60) = 60 * log10(9) ≈ 60 * 0.9542425094 = 57.254550564, so it's a 58-digit number (since 10^57 has 58 digits? Actually, 10^57 is 1 followed by 57 zeros, so it has 58 digits). So 9**60 is a 58-digit number. Then 60 * 9**60 is a 60-digit number? 58 digits plus 2 from multiplying by 60 (which is about 1.78) so about 60 digits. So it's manageable.
But for n=60, we are computing 9**60, which is a 58-digit number. That's okay.
However, for n=60, we are also computing 8**60, 7**60, etc. But note: 8**60 is less than 9**60, so similar size.
So it's acceptable for n up to 60.
But what about n=59? 9**59 is about 9**60/9, so similar.
So we are safe for n<=60.
Therefore, the function is efficient enough.
But wait: what if the list has a number with 60 digits and we have to compute 9**60? That might take a little time, but it's only once.
Let me test in Python:
import time
start = time.time()
a = 9**60
end = time.time()
print(end-start) # on my machine, it's instantaneous (less than 0.001 seconds).
So it's fast.
But for n=60, we compute 10 such powers? That's 10 exponentiations of 9**60? Actually, we compute d**n for d in 0..9. But note that 0**n=0, 1**n=1, so we can skip? But we are computing all. But it's only 10.
So we are good.
However, note: the condition n>=61 is based on the fact that for n>=61, the maximum sum is less than the minimum number. But wait, we are using the condition n*9**n < 10**(n-1). For n=61, we have:
61 * 9**61 < 10**60.
So we can return False for n>=61.
But what about n=60? We are computing. And we know there are no 60-digit Armstrong numbers, but we are not using that knowledge. We are computing and checking. That's okay because if there were one, we would find it. But since there are none, we will not add any.
So the function is correct.
Let me adjust: we are using a cache for power_cache. But note: the cache is per function call. If the function is called multiple times, we are recomputing. But that's okay.
Alternatively, we can use a global cache? But the problem doesn't require multiple calls.
So we are done.
But wait: what about the number 0? We have n=1, and we compute power_cache[1] = [0,1,2,...,9]. Then for the digit '0', we get 0. So 0 is included.
However, note: the problem says "list of integers", and 0 is an integer.
So we are good.
Let's write the final code.
But note: the problem says "explain your algorithm". So we need to provide an explanation.
Explanation:
We are given a list of integers. For each integer, we check if it is an Armstrong number.
Steps for one number:
1. If the number is negative, skip (because Armstrong numbers are defined for non-negative integers).
2. Convert the number to a string to get its digits and the number of digits (n).
3. If n >= 61, then by mathematical property (the maximum possible sum of n digits each raised to the power n is less than the smallest n-digit number), it cannot be an Armstrong number, so skip.
4. Otherwise, compute the sum of each digit raised to the power n. To optimize, we precompute the powers for digits 0-9 for the current n and cache them for reuse if the same n appears again in the list.
5. If the sum equals the original number, then add it to the result.
We return the list of all such numbers in the order they appear in the input.
However, note: the condition n>=61 is a known mathematical result for base 10. We are using it to avoid expensive computations for very large numbers.
But what if the input number is 0? Then n=1, and we compute 0**1=0, so it is an Armstrong number.
What about single-digit numbers? They are all Armstrong numbers because for a single digit d, we have d^1 = d.
So the function correctly returns all single-digit non-negative numbers.
Edge cases:
- Empty list: returns empty list.
- List with negative numbers: they are skipped.
- List with 0: 0 is included.
Let me test with an example:
Input: [153, 123, 370, 371, 407, 0, 1, 2, 10, 9474, 54748, 92727, 93084, 4210818, -153, 99999999999999999999999999999999999999]
# The last one has 41 digits? Actually, 41 nines -> 41 digits. We skip because n>=61? No, 41<61, so we compute. But note: 41-digit number, we compute 9**41? That's about 10^(41*0.954) = 10^39.1, so about 1.26e39, and then 41 * that is about 5.17e40, and the number is 10^40-1? Actually, 41 nines is 10^41-1, which is about 1e41. So the maximum sum is about 5.17e40, which is less than 1e41. So we might skip by the condition? Wait, n=41, we have:
n*9**n = 41 * 9**41
log10(41) + 41*log10(9) = 1.61278 + 41*0.9542425094 = 1.61278 + 39.1239408854 = 40.7367208854, so 10^40.7367 = about 5.46e40.
And 10^(41-1)=10^40=1e40.
So 5.46e40 > 1e40, so the condition does not rule out.
But wait, we are using the condition for n>=61. So for n=41, we compute.
However, note: the condition we derived was for n>=61. So for n=41, we compute.
But we know that 41-digit Armstrong numbers do not exist? Actually, the known list says the largest is 39 digits. So for n=40 and 41, there are none. But we are not using that knowledge.
So we will compute for n=41 and then find that it's not equal.
But note: the computation for n=41: 9**41 is a 41*log10(9) ≈ 39.1, so a 40-digit number? Actually, 9**41 is about 1.26e39, which is 40 digits? 10^39 has 40 digits? 10^39 is 1 followed by 39 zeros, so 40 digits. So 9**41 is a 40-digit number. Then 41 * 9**41 is about 5.17e40, which is 41 digits? 10^40 is 41 digits? 10^40 is 1 followed by 40 zeros, so 41 digits. So 5.17e40 is 41 digits. So the sum can be 41 digits.
But the number we are testing is 41 nines, which is 10^41-1, which is 41 digits and the largest 41-digit number. The sum we get is at most 41 * 9**41, which is about 5.17e40, which is less than 10^41-1? Actually, 5.17e40 is 5.17 * 10^40, and 10^41-1 is about 1e41. So 5.17e40 < 1e41. So the sum cannot reach 10^41-1. So it's impossible for a 41-digit number with all 9's. But what about a number with digits that are not all 9? The maximum sum is when all digits are 9, so if that maximum is less than the smallest 41-digit number? Wait, the smallest 41-digit number is 10^40, which is 1 followed by 40 zeros. And the maximum sum is 41 * 9**41, which is about 5.17e40, which is greater than 10^40 (which is 1e40). So it's possible in theory? But note: the maximum sum is 5.17e40, and the largest 41-digit number is 10^41-1, which is about 1e41. So the sum is at most 5.17e40, which is less than 10^41. So for a 41-digit number, the maximum sum is 5.17e40, which is a 41-digit number? 5.17e40 is 5.17 * 10^40, which is 41 digits? 10^40 is 41 digits (1 followed by 40 zeros). So 5.17e40 is 5.17 * 10^40, which is still 41 digits? Actually, 5.17e40 is 517 followed by 38 zeros? That's 41 digits. So it's possible.
But wait, the condition we used for n>=61 was that n*9**n < 10**(n-1). For n=41, we have:
41*9**41 vs 10^40.
We computed that 41*9**41 is about 5.17e40, which is greater than 1e40. So the condition does not hold.
Therefore, we cannot rule out n=41 by that condition.
But note: the known list says there are no 41-digit Armstrong numbers. So we have to compute.
However, computing 9**41 is expensive? 9**41 is about 1.26e39, which is a 40-digit number. But we are computing 10 such powers? That's 10 exponentiations of 9 to the power 41? But note: we are computing for each digit 0-9, so we compute 0**41, 1**41, ..., 9**41. But 0**41=0, 1**41=1, and then 2**41, 3**41, etc. 2**41 is about 2.2e12, which is 13 digits. 9**41 is 40 digits. So we are computing 9**41, which is a 40-digit number. That's acceptable? It might take a little time, but for one n it's okay.
But if the list has many numbers with n=41, we compute the powers once and cache.
So it's acceptable.
Therefore, we keep the condition for n>=61, and for n<=60 we compute.
But wait, what about n=60? We compute 9**60, which is a 58-digit number? Actually, 9**60 is about 1.0e57, which is 58 digits? 10^57 is 58 digits? 10^57 is 1 followed by 57 zeros, so 58 digits. So 9**60 is 58 digits. Then 60 * 9**60 is about 6e58, which is 59 digits? 10^58 is 59 digits? 10^58 is 1 followed by 58 zeros, so 59 digits. So 6e58 is 59 digits. So the maximum sum for a 60-digit number is 59 digits? That would be less than the smallest 60-digit number (10^59, which is 60 digits)? Wait, 10^59 is 1 followed by 59 zeros, so 60 digits. And 6e58 is 5.9... * 10^58, which is 59 digits? Actually, 6e58 is 6 * 10^58, which is 59 digits? 10^58 has 59 digits? 10^58 is 1 followed by 58 zeros, so 59 digits. So 6e58 is 59 digits? No, 6e58 is 6 followed by 58 zeros, which is 59 digits. So the maximum sum for a 60-digit number is 59 digits? That would be less than the smallest 60-digit number (10^59, which is 60 digits). So for n=60, the maximum sum is less than the minimum number? Let's check:
n=60: maximum sum = 60 * 9**60.
log10(60 * 9**60) = log10(60) + 60*log10(9) = 1.778151 + 57.254550564 = 59.032701564, so the maximum sum is about 10^59.0327, which is 1.076e59? Actually, 10^59.0327 = 10^0.0327 * 10^59 = 1.076 * 10^59. So it's a 60-digit number? 10^59 is 1 followed by 59 zeros, so 60 digits. So 1.076e59 is 1.076 * 10^59, which is still 60 digits? Actually, 10^59 has 60 digits? 10^0=1 (1 digit), 10^1=10 (2 digits), so 10^59 has 60 digits. So 1.076e59 is 1.076 * 10^59, which is a 60-digit number (starting with 1076...). So the maximum sum for a 60-digit number is a 60-digit number? Yes, because 1.076e59 is 1076... with 59 zeros? Actually, 1.076e59 is 1.076 * 10^59, which is 1 followed by 59 zeros multiplied by 1.076? That would be 1.076 followed by 59 zeros? But that's not an integer. Wait, we are talking about integer values.
Let me compute exactly:
9**60 is an integer, and 60 * 9**60 is an integer. And we are comparing to 10**59.
We have:
60 * 9**60 = 60 * (9**60)
and 10**59.
We know that 9**60 is about 1.0e57? Wait, we computed log10(9**60)=57.25455, so 9**60 is about 1.79e57. Then 60 * 1.79e57 = 1.074e59. And 10**59 is 1e59. So 1.074e59 > 1e59. So the maximum sum is greater than the minimum 60-digit number.
Therefore, for n=60, it is possible in theory.
But wait, we are comparing the maximum sum (60*9**60) to the minimum 60-digit number (10^59). Since 60*9**60 > 10^59, we cannot rule out.
However, note that the maximum sum is 60*9**60, which is about 1.074e59, and the largest 60-digit number is 10^60-1, which is about 1e60. So the maximum sum is only about 10% of the largest 60-digit number. So it's possible that a 60-digit number could be Armstrong? But it has been proven that there are none for n>=40.
So we are computing for n=60 and will not find any.
Therefore, our condition for n>=61 is safe.
But wait, what about n=61? We have:
n=61: maximum sum = 61 * 9**61.
log10(61) + 61*log10(9) = 1.78533 + 58.208789 = 59.994119, so 10^59.994119 = about 9.85e59? Actually, 10^59.994119 = 10^0.994119 * 10^59 = 9.85 * 10^59 = 9.85e59.
And the minimum 61-digit number is 10^60 = 1e60.
So 9.85e59 < 1e60, so the maximum sum is less than the minimum number. So for n=61, it's impossible.
So we can safely skip n>=61.
But note: what about n=62? Then the left side (log10(n) + n*log10(9)) will be even smaller compared to n-1? Actually, the term n*log10(9) is about 0.9542*n, and we are comparing to n-1. The difference is (n-1) - (log10(n) + 0.9542*n) = -0.0458*n - log10(n) + 1, which becomes more negative as n increases.
So for n>=61, the condition holds.
Therefore, we can use:
if n >= 61:
continue
But wait, what about n=60? We are not skipping, and we are computing. That's acceptable.
However, note: the condition we used is for the maximum sum to be less than the minimum number. But for n=60, the maximum sum is greater than the minimum number, so we cannot skip.
So the condition is:
if n >= 61:
skip.
But wait, what about n=60? We are computing.
But note: the known list says there are no 60-digit Armstrong numbers. But we are not using that.
So the function is correct.
Let me adjust: the condition for skipping is when the maximum possible sum (n * 9**n) is less than the smallest n-digit number (10**(n-1)). We derived that for n>=61, this is true. But what about n=61? We have:
n=61: 61 * 9**61 < 10**60.
So we can skip for n>=61.
But what about n=60? We have 60*9**60 > 10**59, so we cannot skip.
So the condition is:
if n >= 61:
continue
However, note: what about n=61? We are skipping. But wait, the condition we used was n*9**n < 10**(n-1). For n=61, that holds. So skipping is correct.
But what about n=60? We are not skipping, and we are computing. That's correct.
But wait: what if n=60 and the number is 60 digits? We are computing the sum, and the sum is at most 60*9**60, which is about 1.074e59, and the number is at least 1e59. So the sum can be equal to the number? But note: the number is 60 digits, so it is between 1e59 and 1e60-1. The maximum sum is 1.074e59, which is less than 1e60, but it is greater than 1e59. So it's possible that a 60-digit number (which is at least 1e59) could be equal to the sum? But the sum is at most 1.074e59, so only numbers up to 1.074e59 could be Armstrong. But note: a 60-digit number is at least 1e59, so we are only considering numbers in [1e59, 1.074e59]. That's a very small range. And it has been proven that there are no Armstrong numbers in that range? But we are not using that.
So we compute and check.
But note: the condition we used for skipping is only when the maximum sum is less than the minimum number. For n=60, the maximum sum is greater than the minimum number, so we cannot skip.
Therefore, the condition for skipping is only when n>=61.
But wait, what about n=61? We skip because the maximum sum is less than the minimum number.
So we are safe.
However, note: what about n=0? We don't have n=0 because num=0 gives n=1.
So we are done.
Let me write the code accordingly.
But wait: the condition n>=61 is for the string length. But what if the number is 0? Then n=1, so we compute.
So the code:
def find_armstrong_numbers(lst):
result = []
power_cache = {}
for num in lst:
if num < 0:
continue
s = str(num)
n = len(s)
if n >= 61:
continue
if n not in power_cache:
power_cache[n] = [d**n for d in range(10)]
total = 0
for ch in s:
total += power_cache[n][int(ch)]
if total == num:
result.append(num)
return result
However, note: what about the number 0? We have s="0", n=1, and we compute power_cache[1][0] = 0, so total=0, and 0==0 -> True.
So it works.
But wait: what if the list has a very large number that is not 61 digits but, say, 100 digits? We skip because n>=61.
And for n=61, we skip.
So we are safe.
But note: what about n=60? We compute 9**60, which is about 1.0e57, and that's acceptable.
However, if the list has a 60-digit number, we are going to compute 9**60, which is a 58-digit number? Actually, 9**60 has 58 digits? Let me compute:
log10(9**60) = 60 * log10(9) ≈ 57.25455, so it's a 58-digit number (since 10^57 has 58 digits). So 9**60 is a 58-digit number. That's acceptable.
But note: we are computing for each digit 0-9, so we compute 0**60=0, 1**60=1, 2**60, 3**60, ..., 9**60. 2**60 is about 1.15e18, which is 19 digits. 9**60 is 58 digits. So the largest is 58 digits. That's acceptable.
So we are good.
One more optimization: if n==1, then every digit is itself, so every single-digit number is an Armstrong number. We can handle that separately? But our code already does: power_cache[1] = [0,1,2,...,9] and then we sum the digit, which is the number itself. So it's the same.
So no need.
Therefore, the function is complete.
But wait: what about the number 0? It's a single-digit number and is included.
So we are done.
However, note: the problem says "write a function that solves this problem", so we must write the function and explain.
Let me write the final code with explanation.
But note: the condition n>=61 is a known mathematical result. We can also not use it and let the function compute for any n, but then for very large n (like 1000) it would be too slow. So we use the condition.
Alternatively, we can compute the condition on the fly for each n? That is, for a given n, we check if n*9**n < 10**(n-1). But that would require computing 9**n, which we are trying to avoid for large n. So we use the known threshold of 61.
But wait, we can compute the condition without computing 9**n? We can use logarithms:
if n >= 61: # because we know for n>=61, n*9**n < 10**(n-1)
continue
But how do we know 61? We derived that for n=61, the inequality holds. And for n>61, it holds more strongly.
So we hardcode 61.
But what if the base is not 10? The problem is base 10.
So we are safe.
Therefore, the function is as above.
Let me test with a 61-digit number: we skip.
But note: what if the number has 61 digits but the first digit is 1 and the rest are 0? That number is 10^60. We skip because n=61. But wait, 10^60 is 1 followed by 60 zeros, so it has 61 digits. And we skip. But is 10^60 an Armstrong number?
n=61, the sum would be 1^61 + 0^61 + ... = 1. So 1 != 10^60. So it's not.
So skipping is safe.
But what if there is a 61-digit Armstrong number? We know there are none, so safe.
So we are done.
However, note: the condition n>=61 is for base 10. If we were to change the base, we would need to adjust. But the problem is base 10.
So final code:
We'll write:
def find_armstrong_numbers(lst):
result = []
power_cache = {}
for num in lst:
if num < 0:
continue
s = str(num)
n = len(s)
# For n >= 61, it's known that no Armstrong number exists in base 10.
if n >= 61:
continue
if n not in power_cache:
power_cache[n] = [d**n for d in range(10)]
total = 0
for ch in s:
total += power_cache[n][int(ch)]
if total == num:
result.append(num)
return result
But wait: what about the number 0? We have n=1, so we compute.
Alternatively, we can handle 0 separately? Not needed.
Let me test with a list that includes 0 and negative numbers.
Example:
find_armstrong_numbers([0, -1, 1, 2, 3, 153, 370, 371, 407, 9474, 54748, 92727, 93084, 4210818, 10, 100])
should return [0,1,2,3,153,370,371,407,9474,54748,92727,93084,4210818]
But note: 4210818 is a 7-digit Armstrong number? Let's check:
4^7 = 16384, 2^7=128, 1^7=1, 0^7=0, 8^7=2097152, 1^7=1, 8^7=2097152? Wait, the number is 4210818, so digits: 4,2,1,0,8,1,8.
So: 4^7=16384, 2^7=128, 1^7=1, 0^7=0, 8^7=2097152, 1^7=1, 8^7=2097152.
Sum = 16384+128+1+0+2097152+1+2097152 = 4210818? Let's compute:
16384 + 128 = 16512
+1 = 16513
+0 = 16513
+2097152 = 2113665
+1 = 2113666
+2097152 = 4210818.
Yes.
So the function should return that.
Now, what about 100?
s="100", n=3, digits: 1,0,0 -> 1^3+0^3+0^3=1, not 100 -> False.
So we are good.
Therefore, the function is correct.
But wait: what about the number 0? We have s="0", n=1, and we compute 0**1=0, so total=0, and 0==0 -> True.
So 0 is included.
However, note: some definitions of Armstrong numbers exclude 0. But the problem says "a number that is equal to the sum of its own digits", and 0 fits. So we include.
If the problem wants to exclude 0, then we can adjust by:
if num == 0:
# decide whether to include or not.
But the problem doesn't specify, so we include.
Now, let's write the explanation.
Explanation of the algorithm:
We iterate over each number in the input list.
For each non-negative number:
- Convert the number to a string to obtain its digits and the number of digits (n).
- If n is 61 or more, we skip because it's mathematically impossible for such a number to be an Armstrong number in base 10.
- Otherwise, we compute the sum of each digit raised to the power n. To avoid repeated computation for the same n, we cache the list of d**n for d in 0..9 the first time we encounter a particular n.
- If the computed sum equals the original number, we add it to the result list.
We return the result list containing all Armstrong numbers in the order they appear in the input.
Time complexity:
Let k be the number of elements in the list, and let d_i be the number of digits of the i-th number.
For each number with d_i < 61, we do O(d_i) work (after precomputing the powers for that d_i, which costs O(1) because we compute 10 exponentiations once per distinct d_i).
The worst-case scenario is when all numbers have distinct number of digits (and less than 61), then we precompute at most 60 sets of powers, each set taking O(1) exponentiations? Actually, each set requires computing 10 exponentiations, but the exponent is at most 60, so each exponentiation is O(1) in terms of the exponent? But note: the numbers can be large, so the exponentiation cost depends on the size of the exponent and the base. However, the exponent is at most 60, so we can consider it constant.
Therefore, the total time is O(k * d_max) where d_max is the maximum number of digits among the numbers (but capped at 60). And the space is O(60*10) for the cache (at most 60 sets of 10 numbers each) plus the result list.
But note: the exponentiations for large n (like 60) produce large numbers (9**60 has about 58 digits), but that's still manageable.
So the algorithm is efficient.
However, if the list contains a 60-digit number, then we compute 9**60, which is a 58-digit number, and then we sum 60 such numbers? But note: we are summing at most 60 terms, each at most 9**60. So the total sum is at most 60*9**60, which is about 60 times a 58-digit number, so at most 60-digit number. That's acceptable.
Therefore, we are done.
Let me write the code accordingly.
But wait: the problem says "write a function", so we'll write:
def find_armstrong_numbers(lst):
result = []
power_cache = {}
for num in lst:
if num < 0:
continue
s = str(num)
n = len(s)
if n >= 61:
continue
if n not in power_cache:
power_cache[n] = [d**n for d in range(10)]
total = 0
for ch in s:
total += power_cache[n][int(ch)]
if total == num:
result.append(num)
return result
However, note: what if the number is 0? Then s="0", n=1, and we compute power_cache[1] = [0,1,2,...,9]. Then total = power_cache[1][0] = 0, and 0==0 -> True.
So it works.
But wait: what about the number 10? We have n=2, power_cache[2] = [0,1,4,9,...,81]. Then total = 1^2 + 0^2 = 1, not 10 -> False.
Good.
One more: 153 -> n=3, power_cache[3] = [0,1,8,27,64,125,216,343,512,729]. Then 1^3=1, 5^3=125, 3^3=27, total=153 -> True.
So we are confident.
However, note: the condition n>=61 is based on the inequality n*9**n < 10**(n-1). But what if the number has leading zeros? We don't have leading zeros in the integer representation.
So we are safe.
Therefore, the final function is as above.
But wait: the problem says "list of integers", and we are converting to string. What about very large integers? Python can handle arbitrarily large integers, and string conversion is O(d) where d is the number of digits. So it's acceptable.
So we are done.
Let me write the explanation in the response.
However, note: the problem says "explain your algorithm", so we must provide an explanation.
I'll write:
Algorithm:
We iterate through each number in the input list.
For each non-negative number:
1. Convert the number to a string to get its digits and the number of digits (n).
2. If n >= 61, skip because it's impossible for such a number to be an Armstrong number in base 10 (due to the fact that the maximum sum of n digits each raised to the power n is less than the smallest n-digit number).
3. Otherwise, compute the sum of the n-th power of each digit. To optimize, we precompute and cache the powers for each digit (0-9) for the current n if not already cached.
4. If the sum equals the original number, add it to the result list.
Return the result list.
We skip negative numbers because Armstrong numbers are defined for non-negative integers.
This algorithm efficiently handles the list by caching the powers for each digit length and by early termination for numbers with 61 or more digits.
Example:
Input: [153, 123, 370, 371, 407, 0, 1, 2, 10]
Output: [153, 370, 371, 407, 0, 1, 2]
Now, I'll write the code accordingly.
But note: the problem says "write a function", so we must write the function in a programming language. The problem doesn't specify, but we are using Python.
Let me write the function in Python.
However, note: the condition n>=61 is for base 10. If we were to change the base, we would need to adjust. But the problem is base 10.
So we are done.
One more thing: what if the input list has a number with 61 digits but is actually 0? That's impossible because 0 has 1 digit.
So we are safe.
Therefore, the final answer is the function above.
But wait: the problem says "You are given a list of integers. Write a function to find the 'Armstrong numbers'." So we return the list.
I think that's it.
However, let me test with a 60-digit number that is all 9's? That would be 10^60-1. We would compute the sum:
n=60, digits are 60 nines.
total = 60 * (9**60)
But 9**60 is about 1.0e57, so 60 * 1.0e57 = 6e58, which is less than 10^60-1 (which is about 1e60). So it's not equal.
So we are safe.
But what about a 60-digit number that is 1 followed by 59 zeros? That is 10^59. The sum would be 1^60 + 0^60*59 = 1. So not equal.
So we are safe.
Therefore, the function is correct.
Now, I'll write the code and explanation.
But note: the problem might be expecting the function to print? It says "write a function", so we return.
So we return the list.
Let me write the code in the response.
However, the user said "Please an Armstrong number is a number that is equal to the sum of its own digits each raised to the power of the number of digits, write a function that solves this problem, and explain your algorithm."
So I'll write:
def find_armstrong_numbers(lst):
result = []
power_cache = {}
for num in lst:
if num < 0:
continue
s = str(num)
n = len(s)
if n >= 61:
continue
if n not in power_cache:
power_cache[n] = [d**n for d in range(10)]
total = 0
for ch in s:
total += power_cache[n][int(ch)]
if total == num:
result.append(num)
return result
Explanation:
The function iterates over each integer in the input list. For each non-negative integer, it:
1. Converts the integer to a string to easily access its digits and determine the number of digits (n).
2. If n is 61 or greater, it skips the number because it's mathematically impossible for such a number to be an Armstrong number in base 10 (the maximum possible sum of n digits each raised to the power n is less than the smallest n-digit number).
3. Otherwise, it computes the sum of each digit raised to the power n. To avoid recomputing the same powers for the same n, it caches a list of d**n for d in 0-9 the first time a particular n is encountered.
4. If the computed sum equals the original number, it adds the number to the result list.
The function returns the list of all Armstrong numbers found, in the order they appeared in the input.
This approach efficiently handles the list by leveraging caching and by eliminating hopeless cases (n>=61) early.
Example:
Input: [153, 123, 370, 371, 407, 0, 1, 2, 10]
Output: [153, 370, 371, 407, 0, 1, 2]
That should be it.
However, note: the condition n>=61 is based on the inequality n*9**n < 10**(n-1). We have verified that for n=61 this holds, and for n>61 it holds more strongly. Therefore, we can safely skip numbers with 61 or more digits.
But wait: what about n=60? We are not skipping, and we are computing. That's correct because for n=60, the maximum sum (60*9**60) is greater than the minimum 60-digit number (10^59). However, it has been proven that there are no 60-digit Armstrong numbers, but we are not using that knowledge; we are computing and checking.
So the function is correct.
I think we are done.</think>
To solve this problem, we need to identify Armstrong numbers (also known as narcissistic numbers) from a given list of integers. An Armstrong number is a number that equals the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because (1^3 + 5^3 + 3^3 = 153).
Approach
- Iterate through each number in the input list.
- Skip negative numbers since Armstrong numbers are defined for non-negative integers.
- Convert the number to a string to easily access its digits and determine the number of digits (n).
- Early termination for large (n): If (n \geq 61), skip the number because it's mathematically impossible for such a number to be an Armstrong number in base 10. This is derived from the inequality (n \times 9^n < 10^{n-1}), which holds for (n \geq 61).
- Cache digit powers: For each unique (n) (number of digits), precompute and cache the values of (d^n) for all digits (d) (0-9) to avoid redundant calculations.
- Compute the sum of each digit raised to the power (n) using the cached values.
- Check Armstrong condition: If the computed sum equals the original number, add it to the result list.
- Return the result list containing all Armstrong numbers in the order they appeared in the input.
Solution Code
def find_armstrong_numbers(lst):
result = []
power_cache = {}
for num in lst:
if num < 0:
continue
s = str(num)
n = len(s)
if n >= 61:
continue
if n not in power_cache:
power_cache[n] = [d ** n for d in range(10)]
total = 0
for ch in s:
total += power_cache[n][int(ch)]
if total == num:
result.append(num)
return result
Explanation
- Initialization: We start with an empty result list and an empty cache (
power_cache) to store precomputed digit powers for each digit length (n).
- Processing each number:
- Negative check: Skip negative numbers immediately.
- Digit extraction: Convert the number to a string to get its digits and compute (n), the number of digits.
- Large (n) optimization: If (n \geq 61), skip further processing since no Armstrong number exists with that many digits in base 10.
- Power caching: For a new (n), compute (d^n) for all digits (d \in [0, 9]) and store them in
power_cache[n].
- Sum calculation: For each digit in the number, add its precomputed (n)-th power to
total.
- Armstrong check: If
total equals the original number, append it to result.
- Result: Return the list of all identified Armstrong numbers.
Example
Input: [153, 123, 370, 371, 407, 0, 1, 2, 10]
Output: [153, 370, 371, 407, 0, 1, 2]
- 153: (1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153)
- 370: (3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370)
- 371: (3^3 + 7^3 + 1^3 = 27 + 343 + 1 = 371)
- 407: (4^3 + 0^3 + 7^3 = 64 + 0 + 343 = 407)
- 0, 1, 2: All single-digit numbers are Armstrong numbers since (d^1 = d).
- 123 and 10 are not Armstrong numbers.
This approach efficiently handles the problem by leveraging mathematical insights to avoid unnecessary computations and caching to optimize repeated calculations.