Answer to Question #296152 in Python for Sudheer

Question #296152

write a python function Separate_List(A) (Refer RollNo_W8B_2.py ) which take list A as input and return five lists, one contains all prime numbers in A, second one contains composite numbers in A, the third list contains all numbers in A which are neither divisible by 2, fourth and fifth list contains number divisible by 3 and 5

1
Expert's answer
2022-02-10T15:23:37-0500
import math
primes = [0 for i in range(100000)]
def sieve(N):
    for i in range(2,int(math.sqrt(N))+1):
        if primes[i] == 0:
            j = i * i
            while j <= N:
                primes[j] = 1
                j += i
                
def Seperate_list(A):
    l1,l2,l3,l4,l5 = [],[],[],[],[]
    for item in A:
        if primes[item] == 0 and (item != 0 or item!= 1):
            l1.append(item)
        if primes[item]:
            l2.append(item)
        if item % 2 == 1:
            l3.append(item)
        if item % 3 == 0:
            l4.append(item)
        if item % 5 == 0:
            l5.append(item)
    return [l1,l2,l3,l4,l5]




A = [2,3,4,5,6]
sieve(max(A))
print(Seperate_list(A))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS