About 21,800,000 results
Open links in new tab
  1. How to let prime number print once in Python - Stack Overflow

    Dec 1, 2021 · def prime_number_generator(num): for n in range (1,num): for prime in range (2,n): if (n%prime) ==0: break else: yield n You are yielding n if it is not exactly divisible by prime. Here is an example for what happens when n = 5: First, prime = 2.

  2. python - Prime numbers iterator - Stack Overflow

    May 7, 2011 · I wrote a piece of code to print n prime numbers: def __init__(self): self.current = 2. def genPrime(self, num): for i in range(num): while 1: for j in range(2, self.current/2 + 1): if self.current % j == 0: self.current = self.current + 1. break. else: break. print self.current, self.current = self.current + 1. p = PrimeGen() p.genPrime(5)

  3. Python Programs to Print Alternate Prime Numbers till N - PYnative

    Apr 8, 2025 · Explanation. n = 20, meaning we want to find and print prime numbers up to 20.; We introduce a counter variable that increments every time a prime number is found.; Only when the counter is even the prime number is printed (if count % 2 == 0).This ensures that every second prime number is skipped (i.e., we print alternate primes). The counter is incremented by 1 …

  4. How to Print Prime Numbers from 1 to N in Python? - Python

    Oct 16, 2024 · The simplest way to find and print prime numbers from 1 to N in Python is by using basic iteration and checking for each number’s divisibility. Let me show you an example and the complete code. Here is the complete Python code to print prime numbers from 1 to n in Python. if num <= 1: return False. for i in range(2, int(num**0.5) + 1):

  5. Check Prime Number in Python - GeeksforGeeks

    Apr 10, 2025 · isprime () function from the SymPy library checks if a number is prime or not. It prints False for 30, True for 13 and True for 2 because 30 is not prime, while 13 and 2 are prime numbers.

  6. Python Program to Print all Prime Numbers in an Interval

    Here, we store the interval as lower for lower interval and upper for upper interval using Python range(), and printed prime numbers in that range. Visit this page to learn how to check whether a number is prime or not .

  7. Prime Number in Python: From Theory to Real Applications

    Feb 14, 2025 · A straightforward way to check if a number is prime is by using a loop. Here’s a straightforward Python function to accomplish that: if n <= 1: return False. for i in range(2, n):

  8. How to Write a Prime Number Program in Python - Edureka

    Dec 5, 2024 · To find a prime number in Python, you have to iterate the value from start to end using a for loop and for every number, if it is greater than 1, check if it divides n. If we find any other number which divides, print that value.

  9. Python Programs to Check Prime Number - PYnative

    Mar 31, 2025 · 2. Using math.sqrt function. In this section, we’ll use Python math module to check if number is Prime number. The math.sqrt() function in is used to calculate the square root of a given number. By calculating the square root of the given number we only need to check for divisors up to the square root of the number.

  10. Python Programs to Find Prime Numbers within a Range - PYnative

    Mar 31, 2025 · In this Python method, instead of checking divisibility up to the number itself, we only need to check up to the square root of the number. This optimization reduces the number of unnecessary checks. The math.sqrt() function in Python is used to calculate the square root of a given number. For instance, consider n = 49: The square root of 49 is 7.

  11. Some results have been removed
Refresh