I am supposed to make list of strong numbers in a given range.
I've written a code for that where I define a function for strong number in which I take the number and append its digits into an empty list(l1)
. Then using for loop I append the factorial of those digits in another empty list(l2)
. Then I return sum(l2)==given number
. I've tried this function for individual numbers and code runs perfectly and gives whether the number is strong or not.
But when i put this function in a for loop to find which are strong numbers in given range by appending the function to another empty list. The resultant comes out to be empty instead of giving list of strong numbers.
My code is: `
import math
s=int(input("enter end range: "))
l1=[]
l2=[]
l3=[]
def strong(x):
for i in str(x):
l1.append(int(i))
for i in l1:
l2.append(math.factorial(i))
return(sum(l2)==int(x))
for i in range(0,s 1):
if strong(i)==True:
l3.append(i)
print(l3)
`
The input and output are supposed to be as follows:
enter end range: 150
list: [1, 2, 145]
But I get:
enter end range: 150
list: []
I get an empty list
CodePudding user response:
The declarations for l1
and l2
should be local to the function:
import math
s=int(input("enter end range: "))
l3=[]
def strong(x):
l1=[]
l2=[]
for i in str(x):
l1.append(int(i))
for i in l1:
l2.append(math.factorial(i))
return(sum(l2)==int(x))
for i in range(0,s 1):
if strong(i)==True:
l3.append(i)
print(l3)
Output:-
enter end range: 150
[1, 2, 145]
CodePudding user response:
There are a few issues with your code that are causing it to produce an empty list.
First, the lists l1, l2, and l3 are defined at the global level, outside of the strong function. This means that they are shared across all invocations of the function, and the values in these lists are not reset between invocations. As a result, the lists will continue to grow with each iteration of the loop, and the function will return incorrect results.
To fix this, you should define these lists inside the strong function, so that they are reset for each invocation of the function.
Second, you are using the math.factorial function to calculate the factorials of the digits, but this function only works for non-negative integers. If a digit is 0 or negative, then the function will raise a ValueError.
To fix this, you should add a check to make sure that the digits are positive before calculating the factorials. You could do this by adding an if statement to the loop that calculates the factorials, or by using a list comprehension to filter out the negative digits.
Finally, the strong function should return a boolean value (True or False) indicating whether the number is strong or not, but your code is returning the result of the comparison sum(l2) == int(x). This will produce a value of True or False, but it will not be a boolean value.
To fix this, you should change the return statement to return sum(l2) == int(x).
Here is the modified code with these changes applied:
import math
def strong(x):
l1 = []
l2 = []
for i in str(x):
l1.append(int(i))
for i in l1:
if i > 0:
l2.append(math.factorial(i))
return sum(l2) == int(x)
s = int(input("enter end range: "))
l3 = []
for i in range(0, s 1):
if strong(i):
l3.append(i)
print(l3)