1

code for printing factors of a number

I'm trying to code a program that prints the factors of a number, just as shown in the attached image, but the outcome is totally not what I'm expecting. (I just want the code to return factors of the specified number.) What am I doing wrong and what is the correct way of doing it?

Code:

number=42
factor_list=[]
for num in range(1,number+1):
    num=number/num
    if (number%num)==0:
        factor_list.append(num)
print(factor_list)

Output:

[42.0, 21.0, 14.0, 10.5, 7.0, 6.0, 5.25, 3.5, 3.0, 2.625, 2.0, 1.75, 1.5, 1.3125, 1.0]
4
  • 1
    Post the code in the question directly and not as pictures - stackoverflow.com/help/how-to-ask Commented Feb 24, 2022 at 9:54
  • Please add your code to the question. Hyperlinks to images are not acceptable Commented Feb 24, 2022 at 9:54
  • tutorialspoint.com/How-to-Find-Factors-of-Number-using-Python Commented Feb 24, 2022 at 9:56
  • Thanks for adding the code as text. Would you add the console output as text too, in a separate block? Commented Feb 24, 2022 at 18:44

3 Answers 3

1

Just remove the 4th line i.e num=number/num which produces quotients. you are trying to find modulus of number from those quotients that is the reason you are getting wrong answer. Here is the modified code which works.

number = 42
factor_list=[]
for num in range(1,number+1):
   if(number%num==0):
       factor_list.append(num)
print(factor_list)
Sign up to request clarification or add additional context in comments.

Comments

0

As to why your program does not work, let's see what is going on step by step:

    number=42
    factor_list=[]
    for num in range(1,number+1):      #for num in [1, 2, 3, 4, 5... 42] (Let's take 28 for example)
        num=number/num                 # num = 42 / 28; num = 1.5 now
        if (number%num)==0:            # 42 mod 1.5 == 0.0
            factor_list.append(num)    # so we add 1.5 to the list.
    print(factor_list)

See the problem in logic now?

Comments

0

In Python, the division operator / will result in a float data type even if the values either side of it are int. For integer division you should use //

Having said that, what you need here is the modulo operator.

Here's a concise (but not necessarily most efficient) way of achieving your objective:

def factors(n):
    return [1] + [x for x in range(2, n + 1) if n % x == 0]

print(factors(4203))

Output:

[1, 3, 9, 467, 1401, 4203]

Fun fact:

If the length of the returned list from this function is 2 then n must be a prime number. Don't go using this function to test for primes though as it would be woefully inefficient

2 Comments

Ah certainly, that makes sense. In the 2nd line of your code though, I see that you have defined a list- 1. without using a variable or labeling the list 2. also used '+' sign to further add the 'for' loop (which I think is similar to list comprehension) but you have also used an 'if' condition within it, all of which helps define the list So out of curiosity my question is- can I add multiple such conditions using the '+' sign similar to what you have done to define the list above? Also, if it is possible to label the list to permanently store list elements for further usage?
@CrimsonNinja Instead of printing the output from factors(n) just assign it to some variable. Yes, it's a list comprehension. Remember that all positive integers have a factor of 1. So that's just a constant that we can add to the output of the comprehension

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.