Martin Eden
Martin Eden

Reputation: 1

Counting divisors to detect prime number in python 3.7.4

I tried to write a code to detect prime numbers myself and accidentally, I found this which is working just fine:

def prime_number_check(inp):
    setp = [ ]
    res = [ ]
    count = 0
    for x in range(1, inp):
        setp.append(x)
    print(setp) #just to control the set
    for y in setp: #set of dividers
        if inp % setp[y-1] == 0:
            res.append(setp[y-1])
        else:
            continue
    print(res) #just to control the set
    for z in res:
        count += 1
    if count > 1:
        print(inp, "is not a prime number!")
    else:
        print(inp, "is a prime number!")

I tried to divide the input number, which will be checked whether it is prime or not, to the numbers smaller than itself and wanted to detect the result set whether it has float results or int results, then remove float results with res.remove(), then if there is int result other than 1, print not a prime, otherwise print prime, but I failed; instead found this code which counts divisors.

Could you help me to find out how can I make float/int check in a list and return a result? I tried bool(), and isinstance() but always got an error about iterability.

Upvotes: 0

Views: 108

Answers (1)

PAWAN PARACKAL
PAWAN PARACKAL

Reputation: 1

def prime(*arg):
    if num<2:
      return 0
    else:
      t=0
      for x in range(2,num+1):
         k=0
         for y in range(1,x+1):
           if x%y == 0:
             k += 1
         if k == 2:
           t += 1
      return t

Here I actually not considered 0 and 1 as prime . I hope you got my simple code in python.

Upvotes: 0

Related Questions