About 1,950,000 results
Open links in new tab
  1. How to Print Prime Numbers from 1 to N in Python? - Python

    Oct 16, 2024 · Print the First 10 Prime Numbers in Python Using a For Loop. Let me now show you two methods to print the first 10 prime numbers using a for loop in Python. Method 1: Basic For Loop with Prime Check Function. This method uses a for loop to iterate through numbers and a helper function to check if a number is prime in Python. Example:

  2. python - how to use for loop to calculate a prime number - Stack Overflow

    Nov 11, 2018 · Here's a Python implementation of a well-known algorithm (the Sieve of Eratosthenes) for generating the first n primes (credit to tech.io for the code): def sieve(n): primes = 2*[False] + (n-1)*[True] for i in range(2, int(n**0.5+1.5)): for j in range(i*i, n+1, i): primes[j] = False return [prime for prime, checked in enumerate(primes) if checked]

  3. Program to print first 10 prime numbers - GeeksforGeeks

    Jul 23, 2024 · Looping until first 10 primes are not found: We can use a while loop to continue our prime check on the numbers until we print the first 10 prime numbers. Step-by-step algorithm: Maintain a variable cnt = 0 to keep track of number of primes printed so far. Maintain a variable num = 2 to keep track of the number to be checked for prime.

  4. To find first N prime numbers in python - Stack Overflow

    for prime in primes: if not (i == prime or i % prime): return False. primes.add(i) return i. primes = set([2]) i, p = 2, 0.

  5. Program to print prime numbers from 1 to N. - GeeksforGeeks

    Oct 8, 2024 · First, take the number N as input. Then check for each number to be a prime number. If it is a prime number, print it. Approach 1: Print prime numbers using loop. Now, according to formal definition, a number ‘n’ is prime if it is not divisible by any number other than 1 …

  6. Python Program to print Prime Numbers from 1 to 100

    Write a Python Program to print Prime numbers from 1 to 100, or 1 to n, or minimum to maximum with examples and also calculate the sum of them. This program prints the prime numbers from 1 to 100 using for loop and break. First, we used For Loop to iterate a …

  7. Sum of Prime Numbers in Python - Scaler Topics

    Apr 27, 2022 · Here we started with the input from the user, where we take the number till which the sum of prime numbers in python needs to be known. Then we use for loop in python to iterate for each number starting from 2. As known, the first …

  8. Check for prime number using for and while loop in Python

    In this article, we will learn how to take a number as user input and check if the number is a prime number or not using for loop and while loop in Python. Before starting, let's learn a bit about prime numbers. What is a Prime Number?

  9. Python Programs to Find Sum of First n Prime Numbers - PYnative

    Apr 8, 2025 · 1. Using loop. We can use for loop to calculate the sum of the first n prime numbers by iterating through numbers from 1 to n, checking if they are prime, and summing them.. Also, see:. Python Programs to Check if Number is Prime Number; Python Programs to Find Prime Numbers within a Range; Code Example: Calculate the sum of first 5 prime numbers.

  10. Python Program to Print Prime Number From 1 to N - Tuts Make

    Nov 3, 2022 · Let’s see the following program to print prime numbers from 1 to N (10, 100, 500, 1000, etc) using for loop, while loop: No = int(input(" Please Enter any Nober: ")) flag = 0. for i in range(2, (No//2 + 1)): if(No % i == 0): flag = flag + 1. break. if (flag == 0 and No != 1): print(" %d is a Prime Nober" %No) else:

Refresh