1

I have obtained input from user and put its factors to a new list.How do i check for prime numbers in the list.

a=int(input())
b=[]
for x in range(2,a):
    if(a%x)==0:
        b.append(x)
print(b)
Blizard
  • 13
  • 1
  • 3
  • you can look [here](https://stackoverflow.com/questions/4114167/checking-if-a-number-is-a-prime-number-in-python) to help you – Ivam Jul 10 '18 at 20:55
  • 5
    Possible duplicate of [Checking if a number is a prime number in Python](https://stackoverflow.com/questions/4114167/checking-if-a-number-is-a-prime-number-in-python) – Patrick Artner Jul 10 '18 at 20:59
  • 1
    Hey guys thank you,But im looking for checking prime numbers in a **"list"**. – Blizard Jul 10 '18 at 21:10

2 Answers2

0

Here you can print the list of factors and then iterate through the list of factors and this program will print out the ones that are prime. Instead of printing it you could also append it to another list by replace the print(n) with something else.

import math
a=int(input())
b=[]
for x in range(2,a):
    if(a%x)==0:
        b.append(x)
print(b)



def is_prime(n): #calling a function 
    if n == 2:
        print(n) #if one of the factors is 2 it prints it because it is a prime number

    if n % 2 == 0 or n <= 1: # if it is less than one or is a factor of 2 it returns false and does nothing
        return False

    sqr = int(math.sqrt(n)) + 1

    for divisor in range(3, sqr, 2): #checks for other divisors
        if n % divisor == 0:
            return False
    print(n) #otherwise it prints out the number since it is a prime number



for n in b:  #iterates through the list of factors and checks if they are prime
    is_prime(n)

If we run this and I input 10 it returns this :

[2, 5]
2
5

EDIT : When you input a prime number it returns a blank array. So i edited the code to be :

import math
values = []
def is_prime(n): #calling a function 
    if n == 2:
        values.append(n)

        #print(n) #if one of the factors is 2 it prints it because it is a prime number
        return True
    if n % 2 == 0 or n <= 1: # if it is less than one or is a factor of 2 it returns false and does nothing
        return False

    sqr = int(math.sqrt(n)) + 1

    for divisor in range(3, sqr, 2): #checks for other divisors
        if n % divisor == 0:
            return False
    #print(n) #otherwise it prints out the number since it is a prime number
    values.append(n)
    return True


a=int(input())
b=[]
for x in range(2,a):
    if(a%x)==0:
        b.append(x)
    if is_prime(a)==True: #if the inputted number is prime it automatically appends that number to the list and breaks since prime numbers don't have any other factors
        b.append(a)
        break;

print(b)


for n in b:  #iterates through the list of factors and checks if they are prime
    is_prime(n)
def remove_duplicates(values):  #here it checks for duplicates
    output = []
    seen = set()
    for value in values:
        # If value has not been encountered yet,
        # ... add it to both list and set.
        if value not in seen:
            output.append(value)
            seen.add(value)
    return output

# Remove duplicates from this list.
values = remove_duplicates(values)

print("Here are the prime factors :")    
print(values) #prints out the values

Now if you input a prime number it returns :

[7]
Here are the prime factors :
[7]

And any other number such as 20 :

[2, 4, 5, 10]
Here are the prime factors :
[2, 5]

will still run. Note : I changed it from printing out just the numbers to appending the numbers to an array and then printing out the array.

Ivam
  • 379
  • 3
  • 12
0

Here is a one liner, you can change lower and upper limit by changing 1 and 150. Assign this to a variable. Not as fast as SoA though

[i for i in range(1,150) if all(i%j for j in range(2,int(i**(1/2))+1)) and i != 1]
nishu b
  • 66
  • 3