Question

PYTHON 3 PROGRAM PLEASE 18.19* (Prime number iterator) Write an iterator class for prime numbers. Invoking...

PYTHON 3 PROGRAM PLEASE

18.19* (Prime number iterator) Write an iterator class for prime numbers. Invoking the __next__() method returns the next prime number. Write a test program that displays all prime numbers less than 10000.

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

Prime Number iterator (Python3):

class iterator: #Iterator class
def __init__(self,num): #Constructor for our class
self.maximum = num #Setting our maximum value attribute

def __iter__(self): #iter method
self.number = 1
return self

def __next__(self): #__next__() method which returns next element in an object
if self.maximum > self.number:
self.number += 1
start = 2
while start < (self.number//2 + 1): #Prime number program starts...
if self.number % start == 0:
self.number += 1

if self.number > self.maximum: #If the self.number is greater than our maximum
raise StopIteration #Raise StopIteration
start = 1   
start += 1 #Incrementing start
else:
return self.number #If the number in the series is a prime number return that number
else:
raise StopIteration

itr = iterator(10000) #Creating "itr" object of the class "iterator"
obj = itr.__iter__() #Setting "obj" as "itr.__iter__()"

try:
while True:
print (obj.__next__(), end = " ") #Printing the prime number using "__next__()" method
except: #Except block is, if the "__next__()" method raises an exception..
pass

#End of the code..

CODE implementation:

File Edit Format Run Options Window Help class iterator: definit (self, num): self.maximum = num #Iterator class #Constructor

Part of the output:

2 3 5711 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 1 07 109 113 127 131 137 139 149 151 157 163 167

NOTE:

# Be aware of the indentation while copying the code into your editor by observing the above uploaded CODE pictures...

# Here when the "__next__()" method is invoked it returns the next element in the object that we have created.

# Any questions or clarifications regarding this question can be asked in the comments section...

Add a comment
Know the answer?
Add Answer to:
PYTHON 3 PROGRAM PLEASE 18.19* (Prime number iterator) Write an iterator class for prime numbers. Invoking...
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
  • Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs...

    Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the user’s choice of the following functions the program exits should also be a user’s choice. Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(“user input”)...

  • Write a Python function isPrime(number) that determines if the integer argument number is prime or not....

    Write a Python function isPrime(number) that determines if the integer argument number is prime or not. The function will return a boolean True or False. Next, write a function HowManyPrimes(P), that takes an integer P as argument and returns the number of prime numbers whose value is less than P. And then write a function HighestPrime(K) that takes integer K as an argument and returns the highest prime that is less than or equal to K. USE THE WHILE LOOP...

  • Write a python 3 program to generate a list of the prime numbers less than 50000...

    Write a python 3 program to generate a list of the prime numbers less than 50000 using the Sieve of Eratosthenes.

  • Please use public class for java. Thank you. 1. (10 points) Write a method that computes...

    Please use public class for java. Thank you. 1. (10 points) Write a method that computes future investment value at a given interest rate for a specified number of years. The future investment is determined using the formula futurelnvestmentValue numberOfYears 12 investmentAmount X ( monthlyInterestRate) Use the following method header: public static double futurelnvestmentValue( double investmentAmount, double monthlyInterestRate, int years) For example, futureInvestmentValue 10000, 0.05/12, 5) returns 12833.59. Write a test program that prompts the user to enter the investment...

  • USING PYTHON 1. Write a small program that asks for an integer number from the user...

    USING PYTHON 1. Write a small program that asks for an integer number from the user and print all the prime numbers (2,3,5,7,etc) less than the input number. 2. How long it takes for your program to print the prime numbers less than 100. (Use magic functions)

  • Write a multithreaded C program that outputs prime numbers. This program should work as follows: the...

    Write a multithreaded C program that outputs prime numbers. This program should work as follows: the user will run the program and will enter a number on the command line. The program will then create a separate thread that outputs all the prime numbers less than or equal to the number entered by the user. ADD  "comments" to the source code please and a screenshot of the output with steps on how to compile

  • In this assignment you are asked to write a Python program to determine all the prime...

    In this assignment you are asked to write a Python program to determine all the prime numbers in between a range and store them in a list that will be printed when the range is searched. The user is prompted for two positive integer values as input, one is the start of the range and the other is the end of the range. If the user gives a negative value or enters a second number that is lower than the...

  • Write a test program that prompt the user to enter seven numbers, stores them in an...

    Write a test program that prompt the user to enter seven numbers, stores them in an array list and do the following: 1. Displays its elements (numbers) in increasing order by invoking a method with the following header that sorts the array: public static void sort(ArrayList<Integer>list) 2. Write a method that returns and displays the sum of all numbers (elements) of the ArrayList. Using the following header: public static double sum(ArrayList<Integer>list) 3. Write a method that removes the duplicate numbers...

  • Write a multithreaded a Java program that outputs prime numbers. This program should work as follows:...

    Write a multithreaded a Java program that outputs prime numbers. This program should work as follows: The user will run the program and will enter a number on the command line, The program will then create a separate thread that outputs all the prime numbers less than or equal to the number entered by the user.

  • Write a program to print all the prime numbers below a certain given number. A prime...

    Write a program to print all the prime numbers below a certain given number. A prime number is defined as a number that can only be divided by 1 and itself. Requirements: 1. Accept the upper limit from the user as an integer. 2. You can assume the user input will be positive and smaller than INT MAX 3. Go from 1 to the number. If you happen to find a number that is prime, print it. The input and...

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