1

Here is what I have. The function works perfectly fine at filtering out composite numbers, but I can not figure out why it is printing duplicates of certain numbers:

def prime_filter(list):
    prime_list = []
    for i in list:
        if i == 2:
            prime_list.append(i)
        if i > 1:
            for n in range(2, i):
                if(i % n == 0):
                    break
                else:
                    prime_list.append(i)
    print("The total number of prime numbers is: ", len(prime_list))
    print(prime_list)

Here is my test of the function and the output it returns:

# test
test_list = [2, 3, 6, 8, 11]
prime_filter(test_list)

The total number of prime numbers is:  11
[2, 3, 11, 11, 11, 11, 11, 11, 11, 11, 11]

Anyone know how I can fix this? Any help is greatly appreciated.

livgup
  • 11
  • 1

6 Answers6

2

You should check whether prime_list is divided by all the numbers or not.

def prime_filter(list):
    prime_list = []
    for i in list:
        if i == 2:
            prime_list.append(i)
        if i > 2:
            for n in range(2, i):
                if(i % n == 0):
                    break
            else:
                prime_list.append(i)
                
    print("The total number of prime numbers is: ", len(prime_list))
    print(prime_list)
1

This might work fine for the solution.i should be added to prime_list when all the check is done.

def prime_filter(list):
    prime_list = []
    for i in list:
        if i == 2:
            prime_list.append(i)
        if i > 1:
            for n in range(2, i):
                if(i % n == 0):
                    break
                if n**2>i:
                    prime_list.append(i)
                    break
    print("The total number of prime numbers is: ", len(prime_list))
    print(prime_list)
Jaden
  • 192
  • 4
  • 15
1

in your code for i>1 for every value of i which is odd is getting appended as odd no%2==1 which makes the program go in else part.

also, you only need to add else part when using for loop no value divides the i.

here is improved working code

def prime_filter(_list):
    prime_list = []
    for i in _list:
        if i == 2:
            prime_list.append(i)
        elif i==1:
            continue
        elif i > 2:
            for n in range(2, i):
                if(i % n == 0):
                    break
            else:
                prime_list.append(i)
    print("The total number of prime numbers is: ", len(prime_list))
    print(prime_list)

test_list = [2, 3, 6, 8, 11]
prime_filter(test_list)
# output [2,3,11]

how to check a no is prime or not

sahasrara62
  • 10,069
  • 3
  • 29
  • 44
1

sahasrara62 and Abhinav answers are, of course, correct, but I would like to point out the bizzare inefficiency here. The biggest factor of composite n is at most sqrt(n), which allows to save many iterations at the inner loop.

Another optimization is to avoid checking even factors - we already know the value is odd

def prime_filter(lst):
    prime_list = [value for value in lst
                  if (not value % 2 
                      or any(not value % n for n in range(3, value**.5 + 1, 2))
                     )]

It can be further improved by adding quick primality test before checking factors

Marat
  • 15,215
  • 2
  • 39
  • 48
1

Your code won't get prime correctly, it will add all numbers not divided by 2, e.g. if there is 9 in the list, it will be included in prime_list. I would like to suggest the following:

def prime_filter(list):
prime_list = []
for i in list:
    if i == 2:
        prime_list.append(i)
    if i > 2:
        prime_flag = True
        for n in range(2, i):
            if i % n == 0:
                prime_flag = False
                break

        if prime_flag:
            prime_list.append(i)
print("The total number of prime numbers is: ", len(prime_list))
print(prime_list)

test_list = [2, 3, 6, 8, 9, 11] prime_filter(test_list)

The total number of prime numbers is: 3

[2, 3, 11]

Zhongbo Chen
  • 493
  • 4
  • 12
0
else:
    prime_list.append(i)

For every factor that doesn't divide i, it will be appended to the list, which is incorrect. Just change the function to

def prime_filter(lst):
    prime_list = []
    for i in lst:
        isprime = 1
        if i == 2:
            prime_list.append(i)
        else if i > 1:
            for n in range(2, i):
                if(i % n == 0):
                    isprime = 0
                    break
            if isprime:
                prime_list.append(i)
    print("The total number of prime numbers is: ", len(prime_list))
    print(prime_list)            
Abhinav Mathur
  • 7,791
  • 3
  • 10
  • 24