primes

What is wrong with this isPrime function?

What is wrong with this isPrime function? Question: I’m making an isPrime function. Any odd number that I put in (unless it’s 1, 2 or 3, which break it) says that it is prime even when they clearly aren’t. from even import * num = input("What number? ") def isPrime(n): n = int(n) if isEven(n): …

Total answers: 1

Validating given input and printing every prime number =< given input. (Python)

Validating given input and printing every prime number =< given input. (Python) Question: So I have read tons of solutions to this type of questions, but they all seem to be way too complicated, or I can’t find any useful solutions in them. I have written the first part where I have to ask for …

Total answers: 2

How to get N prime numbers instead of prime numbers until user input in Python

How to get N prime numbers instead of prime numbers until user input in Python Question: I have a working code to find prime numbers. Code asks for user to select a number and returns the prime numbers untill user input. However I am trying to reutrn the user input as N. def calculate_n_prime(): n_numbers …

Total answers: 1

Why does using for loop for finding prime number runs only runs once?

Why does using for loop for finding prime number runs only runs once? Question: I want to find the prime numbers up to the given number but my loop only runs once and my output is only 2 and 3, where did I went wrong? def primer_check(n): prime_state = True for i in range(2,n+1): if …

Total answers: 1

prime numbers in python same programs different outputs

prime numbers in python same programs different outputs Question: I have this assignment where I have to write a code to determine if a number is a prime number or not and I couldn’t come up with a solution so I searched the net and found the following code( the first one) and then I …

Total answers: 2

Python program to find all prime numbers in the range 1 – n

Python program to find all prime numbers in the range 1 – n Question: Hi I know there are lots of solutions to this problem but I wanted some help finding out why my answer is wrong. Here’s my answer: number = int(input("enter a number: ")) for n in range(2, number + 1): for i …

Total answers: 2

Next Prime Number in Python

Next Prime Number in Python Question: I’m a beginner in Python and I’m practicing to code this problem I saw. Next prime is needed, but there are limitations on the input. I have searched for similar questions, but my code is still not working. Hope you can help. Thank you! The problem I get is …

Total answers: 5

How to find prime numbers in python

How to find prime numbers in python Question: I’m new to Python. I am trying to count the prime numbers in a given range. Some of the answers that developers have shared are like this: import math def count_primes(num): out = [] for i in range(3,num,2): if all(i%j!=0 for j in range(3,int(math.sqrt(i))+1,2)): out.append(i) print(out) I …

Total answers: 7

Leetcode Ex. 204: Count Primes

Leetcode Ex. 204: Count Primes Question: I am learning Python using LeetCode problems and came across the Count Primes problem. I have created a solution, however, the program returned ‘Time Limit Exceeded’ when submitted. I am not sure why this happened. Why did this happen and how could I improve my solution to make it …

Total answers: 2

How do you find the first N prime numbers in python?

How do you find the first N prime numbers in python? Question: I am pretty new to python, so I don’t fully understand how to use loops. I am currently working on a piece of code that I have to find the first N prime numbers. The result that is desired is if you input …

Total answers: 6