Knowee
Questions
Features
Study Tools

Youwrite a program to find first 50 prime numbers

Question

Youwrite a program to find first 50 prime numbers

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple Python program that prints the first 50 prime numbers:

def is_prime(n):
    if n <= 1:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False
    i = 3
    while i * i <= n:
        if n % i == 0:
            return False
        i += 2
    return True

count = 0
num = 2
while count < 50:
    if is_prime(num):
        print(num)
        count += 1
    num += 1

Here's how it works:

  1. The is_prime(n) function checks if a number n is prime. It first checks if n is less than or equal to 1, in which case it returns False because 1 is not a prime number. It then checks if n is 2, in which case it returns True because 2 is a prime number. It then checks if n is divisible by 2, in which case it returns False because no even number greater than 2 can be prime. Finally, it checks if n is divisible by any odd number up to the square root of n. If n is divisible by any such number, it returns False; otherwise, it returns True.

  2. The main part of the program initializes a counter count to 0 and a number num to 2. It then enters a loop that continues until count reaches 50. In each iteration of the loop, it checks if num is prime. If num is prime, it prints num and increments count by 1. Whether or not num is prime, it increments num by 1.

This problem has been solved

Similar Questions

Write a program primes.py that reads a positive integer from standard input, and determines whether or not the number is prime.

Complete the code to find if a given number is a prime number? The program will take a positive integer greater than 1 as input and indicate if it is a prime number by saying "prime", and if it is not a prime number saying "not a prime". Note there are 3 places in the given code that you need to fix for this code to work properly and give the expected output.

Write a single line Python code to print all the prime numbers between 1 and 200.

write easies code for me : Write a program in C to find if a number is prime or not.

A number is chosen at random from 1 to 50. Find the probability of not selecting odd or prime numbers.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.