Question

How to code this in python Write a program that computes and prints the first 1000...

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

Homework Answers

Answer #1

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)
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a python code that will ask the user to enter an integer number n. Construct...
Write a python code that will ask the user to enter an integer number n. Construct a recursive function that prints numbers 1 to n in the form “11223344..nn”.
Write python code for the queue class buuilt with a linked list using: First- returns first...
Write python code for the queue class buuilt with a linked list using: First- returns first value in queue, PrintQ-returns whole queue as list, RemoveValue-removes specific value from queue, RemoveIndex-removes value from queue by index
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
The following code to run as the described program on python. The extra test file isn't...
The following code to run as the described program on python. The extra test file isn't included here, assume it is a text file named "TXT" with a series of numbers for this purpose. In this lab you will need to take a test file Your program must include: Write a python program to open the test file provided. You will read in the numbers contained in the file and provide a total for the numbers as well as the...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program...
C LANGUAGE CODE WITH COMMAND-LINE ARGUMENTS (NO SCANF TO BE USED ) Q]. Write a program that displays all the prime numbers in the given array with the following constraint. Constraint: Only those prime numbers should be displayed whose location is a composite number. Although you may have several prime numbers in the array, only those prime numbers should be displayed which are stored at non-prime locations. Remember that the first position in an array corresponds to the location/index 0....
Language: Python Write a program to simulate an experiment of tossing a fair coin 16 times...
Language: Python Write a program to simulate an experiment of tossing a fair coin 16 times and counting the number of heads. Repeat this experiment 10**5 times to obtain the number of heads for every 16 tosses; save the number of heads in a vector of size 10**5 (call it headCounts). You should be able to do this in 1-3 lines of numpy code. (Use np.random.uniform1 to generate a 2d array of 10**5 x 16 random numbers between 0 and...
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
write a python program that include a function named activity_selection() and take in two arguments, first...
write a python program that include a function named activity_selection() and take in two arguments, first one would be the number of tasks and the second argument would be a list of activities. Each activity would have an activity number, start time and finish time. Example activity_selection input and output: activity_selection (11, [[1, 1, 4 ], [2, 3, 5], [3, 0, 6], [4, 5, 7], [5, 3, 9], [6, 5, 9],[7, 6, 10], [ 8, 8, 11], [ 9, 8,...
Problem 2: Python 3 Implement a function called gee_whiz that does the following: given argument n,...
Problem 2: Python 3 Implement a function called gee_whiz that does the following: given argument n, a positive integer, it returns a list of n tuples corresponding to the numbers 1 through n (both inclusive): the tuple for the number k consists of k as the first component, and exactly one of the following strings as the second: • the string 'two!' if k is divisible by 2 • the string 'three!' if k is divisible by 3 • the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT