How to code this in python
Write a program that computes and prints the first 1000 prime numbers - simply write out each prime number on a new line. In implementing this solution I want you to define 2 functions: is_prime which can be used to test whether a number is a prime (e.g. is_prime(17) returns True but is_prime(9) returns False) and compute_primes which will return an array (or Python list) of the first n primes where n is passed as an argument (e.g. compute_primes(10) returns a list of the 1st 10 prime numbers). Note: you can check your answer here: https://primes.utm.edu/lists/small/1000.txt
Hey here is answer to your question.
In case of any doubt comment below. Please UPVOTE if you Liked the answer.
def isPrime(n):
if n <= 1:
return 0
for i in range(2, n):
if n % i == 0:
return 0;
return 1
def compute(n):
answer=[]
counter = 0
# first prime number
k = 2
while counter !=n:
if isPrime(k) == 1:
counter+=1
answer.append(k)
k+=1
return answer
final_answer = compute(1000)
for i in final_answer:
print(i)
Get Answers For Free
Most questions answered within 1 hours.