Answer to Question #301689 in Python for sruthi

Question #301689

program to print the first prime numbers in the given input


Sample Input 2

4

2

3

5

7

Sample Output 2

2


Sample Input 1

5

1

10

4

3

2

Sample Output 1

3



can i get coode for this the above code is not working for both cases



1
Expert's answer
2022-02-24T04:24:07-0500
def is_prime(x):
    if x < 2:
        return False
    if x == 2:
        return True
    if x % 2 == 0:
        return False
    
    p = 3
    while p*p <= x:
        if x % p == 0:
            return False
        p += 2
    return True


def main():
    n = int(input())
    pn = None
    for i in range(n):
        x = int(input())
        if pn is None:
            if is_prime(x):
                pn = x
    
    if pn is not None:
        print(pn)
        

main()

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