starting to get used to asking more advanced programmers instead of wasting hours looking for a solution and coming up with nothing.
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 = int(input("How many prime numbers would you like to see? "))
for num in range(2, n_numbers):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num, end="-")
if __name__ == "__main__":
calculate_n_prime()
Current code returns = [2,3,5,7]
I want a code that returns = [2,3,5,7,11,13,17,19,23,29]
Note - I understand the range function is an issue since it iterates until that number. However without it my code wont work and I didnt really know how to explain my issue. I originally thought the question I was given was to ask for two inputs (first and last #) and return all prime numbers inbetween. Now I am trying to correct my code for the question at hand (Get N prime #'s) Btw, i have tried changing my code numerous times and have been searching and reading isnce yesterday, however since I am so new to the basics it is very difficult to really understand the logic of what i am reading if my code is not the same. (I am in a Trainee program where I am learning software development and am only 2 months in. I began not knowing what a string was. I hope everyone reading this understands that I have tried other solutions, however, I am just having beginner issues and hopefully will begin to progress by asking questions to code I made)
I know we all dont know each other, but I am very shy and hesitate to ask questions because they seem too basic.
CodePudding user response:
The size of the n-th largest prime pn is < n * (log(n) log(log(n))
, which implies pn <= int(n * (log(n) log(log(n)))
. Since this is an upper bound we must keep track of the number of primes we've generated and stop when we've reached n. Here is short python function based on yours illustrating this. It also has an improved upper bound for the inner loop. There are numerous other improvements that are possible.
The upper bound formula only works for n >= 6. You can special-case n < 6 by using [2, 3, 5, 7, 11][:n-1]
to get the correct list of primes.
from math import log, sqrt
def calculate_n_prime2(n: int):
"""Only works for n >= 6"""
upper_bound = int(n * (log(n) log(log(n))))
count = 0
for num in range(2, upper_bound 1):
for i in range(2, int(sqrt(num)) 1):
if num % i == 0:
break
else:
count = 1
print(num, end="-")
if count == n:
return
CodePudding user response:
You can get your desired result by counting the primes as you generate them. You should take advantage of the fact that 2 is the only even prime, so if you special case n_numbers==1
and return only 2, then for your iteration you can look at only the odd numbers. Also you only need to try and divide by odd numbers to see if the current number is prime. This function returns a list of primes, you can then print that however you like (or modify the function to include the print
instead):
from math import sqrt
def is_prime(n):
for i in range(3, int(sqrt(n)) 1, 2):
if n % i == 0:
return False
return True
def calculate_n_prime():
n_numbers = int(input("How many prime numbers would you like to see? "))
if n_numbers <= 0:
return []
elif n_numbers == 1:
return [2]
count = 1
num = 3
primes = [2]
while count < n_numbers:
if is_prime(num):
primes.append(num)
count = 1
num = 2
return primes
print(','.join(map(str, calculate_n_prime())))
# for an input of 10:
# 2,3,5,7,11,13,17,19,23,29