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

  • 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

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

  • Number Comparison: Write a program in PYTHON that compares three numbers and displays the following: Check...

    Number Comparison: Write a program in PYTHON that compares three numbers and displays the following: Check if they are all the same, all different, or neither. Determine if 3 numbers are in increasing order, decreasing order, or neither. Find the highest and lowest number.

  • Consider the following class definition class FibSequence implements Iterablexnteger public Iterator<Integer> iterator) return new FibIterator); private class FibIterator implements Iterator&lt...

    Consider the following class definition class FibSequence implements Iterablexnteger public Iterator<Integer> iterator) return new FibIterator); private class FibIterator implements Iterator<Integer> f //complete this You are required to create an inner class in the FibSequence class. The inner class should be called FibIterator. It implements the Iterator<Integer> interface and implements the hasNext() and the next() methods. The iterator iterates through the Fibonacci sequence. The Fibonacci sequence is a series of numbers where a number is the sum of previous two numbers....

  • Your program  write a program to test whether a number is prime or not. Your program should...

    Your program  write a program to test whether a number is prime or not. Your program should ask user to input an integer and respond: "Prime" or "Not prime". Don't use any library function that tests for prime numbers. The program should continue to ask for input, till it sees a 0, when it exits. Please submit printed pseudocodeshould ask user to input an integer and respond: "Prime" or "Not prime". Don't use any library function that tests for prime numbers....

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