Question

Write a class called Primes. This class should contain a constructor and a method called next_prime....

Write a class called Primes. This class should contain a constructor and a method called next_prime. The next_prime method returns the next prime number. For example:

>>> n = int(input('How many prime numbers?\n'))
>>> p = primes()
>>> prime_numbers = [ ]
>>> for i in range(n):
>>>      prime = p.next_prime()
>>>      primes_numbers.append(prime)
>>> print(prime_numbers)

If the user types 5 in response to the prompt, then the output should be [2, 3, 5, 7, 11].

python 2.7.13

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM CODE:

#Python program to print primes
import math

class primes:
   #constructor for the class
   def __init__(self):
       self.prime = 2
      
   #method to return the prime number
   def next_prime(self):
       nprime = 0
       while True:
           isPrime = True;
           for i in range(2, int(math.sqrt(self.prime) + 1)):
               if self.prime % i == 0:
                   isPrime = False;
                   break
           nprime = self.prime
           self.prime += 1
           if isPrime:
               return nprime
      
#testing code
n = int(input('How many prime numbers?\n'))
p = primes()
prime_numbers = [ ]
for i in range(n):
   prime_num = p.next_prime();
   prime_numbers.append(prime_num);

print(prime_numbers)

OUTPUT:

How many prime numbers?
7
[2, 3, 5, 7, 11, 13, 17]
Add a comment
Know the answer?
Add Answer to:
Write a class called Primes. This class should contain a constructor and a method called next_prime....
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • UseMath2 Write a program called UseMath2. It should contain a class called UseMath2 that contains the...

    UseMath2 Write a program called UseMath2. It should contain a class called UseMath2 that contains the method main. The program should ask for a number from the user and then print the information shown in the examples below. Look at the examples below and write your program so it produces the same input/output. Examples (the input from the user is in bold face) % java UseMath2 enter a number: 0 enter a second number: 0 the maximum of the two...

  • Twin primes are primes that are exactly two apart. For example, 41 and 43, are twin...

    Twin primes are primes that are exactly two apart. For example, 41 and 43, are twin primes. (The two Bushes were twin prime presidents — I’m surprised nobody ever pointed that out!) Write a function twinprimes(primes) that takes as input a list of prime numbers and returns another list twins containing all the twin primes. Be sure to write your code in such a way that the list primes remains unaltered. (It is known that there are infinitely many prime...

  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

  • Python 3> Make a class called BMW: Parameterized constructor with three instance variables (attributes): Hint: def...

    Python 3> Make a class called BMW: Parameterized constructor with three instance variables (attributes): Hint: def __init__(self, make, model, year) Methods in BMW parent class (can leave as method stubs for now): startEngine() -must print "The engine is now on." stopEngine() -must print "The engine is now off." Make another class called ThreeSeries -Must inherit BMW Hint: class ThreeSeries(BMW): -Also passes the three attributes of the parent class and invoke parent Hint: BMW.__init__(self, cruiseControlEnabled, make, model, year) -Constructor must have...

  • Create a LIFO class with following methods in java - public LifoList() The default constructor for...

    Create a LIFO class with following methods in java - public LifoList() The default constructor for LifoList class which will initialize your class variables maxSize to 0 and the int array to null. public LifoList(int maxSize) The constructor for LifoList class which takes the int input maxSize to initialize the size of the array. You should NOT reinitialize the array elsewhere. For example, creating an object as new LifoList(5) should create a LifoList whose maximum size is 5. If the...

  • *Please write in code in C* First, write a function called prime_check(int x) that takes an...

    *Please write in code in C* First, write a function called prime_check(int x) that takes an integer number as an input then return 1 if it is a prime number nd returns 0 if it is a composite number Then, Write a program that prompt the user to input two numbers: Print the multiplication of these two numbers if both of them are prime. Or Print " Sorry at least one of your number is composite" if otherwise

  • in java the program must contain the main method and example( the number 50 should be...

    in java the program must contain the main method and example( the number 50 should be the input) must be included in the main method to check the correctness of your programs. Create a GUI program to let user enter a positive integer n through the first window, and then your program prints out all the prime integers within the range [1, n) through the second window. Your program should contain two windows in total. Below are two example pictures:...

  • Use C programming Make sure everything works well only upload Write a program that takes an...

    Use C programming Make sure everything works well only upload Write a program that takes an integer from standard input and prints all prime numbers that are smaller than it to standard output. Recall that a prime number is a natural number greater than 1 whose only positive divisors are 1 and itself. At the start of the program, prompt the user to input a number by printing "Input a number greater than 2:\n". If the user's input is less...

  • Write a class named TestScores. The class constructor should accept an array of test scores as...

    Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program (create a Driver class in the same file). The program should ask the user to input the number of test scores to be...

  • PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a...

    PYTHON 3 - please show format Question 2. ) Use the Design Recipe to write a function yesOrNo which has no parameters. When called, it gets input from the user until the user types either 'yes' or 'no', at which point the function should return True if the user typed 'yes' and False if the user typed 'no'. Any other entries by the user are ignored and another value must be input. For example: Test Input Result print(yesOrNo()) hello blank...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT