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
  • 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...

  • 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...

  • 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...

  • *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...

  • 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...

  • Write a class named DartSector. The constructor should accept a sector number and position {Single, Double,...

    Write a class named DartSector. The constructor should accept a sector number and position {Single, Double, Treble, Outer & Inner} represented with the integers 1 – 5 respectively. The class should have 3 methods: • getSectorColor – method should return the pocket’s color (as a String) • singleThrow – method should generate 2 random numbers; one in the range (1..20) and another (1..5); and return the score for the throw. • throwThree – method should generate 3 sets of random...

  • 15. Define a class called Point. It has two private data members: x and y with int type; and three public member functions: a constructor, setPoint(int, int) and printPoint0. The constructor init...

    15. Define a class called Point. It has two private data members: x and y with int type; and three public member functions: a constructor, setPoint(int, int) and printPoint0. The constructor initializes the data members to 0. You need to use the operator?: to test whether x and y are positive. If not, assign them to 0. The function printPoint prints the message "(X, Y)" where X and Y are two integers. In the main program, first input the number...

  • Write a class called Student. The specification for a Student is: Three Instance fields name -...

    Write a class called Student. The specification for a Student is: Three Instance fields name - a String of the student's full name totalQuizScore - double numQuizesTaken - int Constructor Default construtor that sets the instance fields to a default value Parameterized constructor that sets the name instance field to a parameter value and set the other instance fields to a default value. Methods setName - sets or changes the student name by taking in a parameter getName - returns...

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
ADVERTISEMENT