About 1,710,000 results
Open links in new tab
  1. 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): 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]

  2. Python Program to Check Prime Number

    In Python, we can also use the for...else statement to do this task without using an additional flag variable. print(num, "is not a prime number") elif num > 1: # check for factors for i in range(2,num): if (num % i) == 0: print(num,"is not a prime number") print(i,"times",num//i,"is",num) break else: print(num,"is a prime number")

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

    Write python programs to find prime numbers using for loop and while loop and also within a given range between 1 to 100.

  4. Python Program to Print Prime Numbers from 1 to 100 - Python

    Oct 20, 2024 · Here is a simple Python program to print prime numbers from 1 to 100 using for loop. if num < 2: return False. for i in range(2, int(num**0.5) + 1): if num % i == 0: return False. return True. for num in range(start, end + 1): if is_prime(num): print(num)

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

    Oct 8, 2024 · First, take the number N as input. Then use a for loop to iterate the numbers from 1 to N; 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 and n. In ...

  6. Python Program to Check Prime Number (4 Ways)

    In this program, we first prompt the user to input a number. We then declare a boolean variable is_prime and initialize it to True. We then use a for loop to iterate over the numbers 2 to num-1. For each number i, we check if num is divisible by i. If …

  7. Python Program For Prime Number (With Code) - Python Mania

    To print prime numbers from 1 to 100 in Python, you can use a loop to iterate through the numbers and check if each number is prime. You can utilize the previously defined prime-checking function. If the number is prime, print it.

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

    Oct 16, 2024 · 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): if num % i == 0: return False. return True. for num in range(2, n + 1): if is_prime(num): print(num)

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

    Source code to print all prime numbers between two numbers enterd by user in Python programming with output and explanation...

  10. Python Program to find Prime Number or Not using For Loop

    Dec 23, 2022 · Python Program to find Prime Number or Not is used to check whether the given number is Prime or Not using For-Each loop and printing it in the output screen.

  11. Some results have been removed
Refresh