1
import requests
def repeat():

  x = int(input("Common divisors of: "))

  listrange = list(range(2,x))

  emptylist = []

  for number in listrange:
    if x % number == 0:
      emptylist.append(number)
      print (emptylist)
    elif x % number not in listrange:
      print ("Prime number")

while True:
  repeat()

Whenever I run this code it it prints prime number several times no matter what I type in.

What I want it to do instead is to give all common divisors for any integer except for 1 and the integer. If the integer is a prime number I want it to print prime number.

However as I previously mentioned this causes a problem for some reason resulting in that whenever the code is executed it prints prime number over and over again even though an elif statement is used.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

2 Answers2

0

Your current logic prints 'Prime number' every time it encounters an integer in the range which doesn't divide the number, irrespective of whether any other numbers divide it (i.e. it is not prime) or not (i.e. it is prime).

This logic corrects that:

def repeat():

  x = int(input("Common divisors of: "))
  listrange = list(range(2,x))
  emptylist = []

  for number in listrange:
    if x % number == 0:
      emptylist.append(number)

  if not emptylist: #Checks if emptylist is empty i.e. is prime
      print ("Prime number")

  else:
      print (emptylist)

e.g.

Common divisors of: 5
Prime number

Common divisors of: 28
[2, 4, 7, 14]
iacob
  • 20,084
  • 6
  • 92
  • 119
0

Your x % number not in listrange is going to be true a lot of the time, even for prime numbers. It is the wrong thing to test for prime numbers.

Say you start with 7, a prime number. The first number to be tested is 2:

>>> x = 7
>>> number = 2
>>> x % number
1

So the remainder is 1. 1 is not in the listrange() values (which go from 2 through to 6, inclusive). That's because for any given prime number larger than 2, division by 2 will always result in 1 as the remainder, and 1 is never in your list.

2 is not the only such value for which the remainder of the division is 1. For the prime number 7919, there are 7 such numbers:

>>> [i for i in range(2, 7919) if 7919 % i < 2]
[2, 37, 74, 107, 214, 3959, 7918]

so your code will print Prime number 7 times. And the test would be true for non-prime numbers too; 9 is not a prime number, but 9 % 2 is 1 too, so your code would claim 9 to be a prime number. For 1000, not a prime number, your code would print Prime number 32 times!

You can't state that a number is a prime number until you have tested all the values in listrange() and have determined that none of those numbers can divide the number without a remainder. So you need to test after you loop; you could test if emptylist is empty; that means there were no divisors, the very definition of a prime number:

for number in listrange:
    if x % number == 0:
        emptylist.append(number)

if not emptylist:
    print ("Prime number") 

As a side note: you don't need to turn a range object into a list of numbers, not only does testing if number in range(...): work, it's a lot faster than using the same test on a list.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343