Create a program that generates prime numbers between two integer boundareis

You first have to think about the properties of a prime number, the key characteristic is that prime numbers can only be divided by itself and 1, so If we prove that any other number can divide the number, then it is not prime.We have to then think about how many iterations we need (how we can use for loops to return the numbers that we need). We need a loop that returns every number between our two boundaries, and then we must check every number from 2, to the current number we are checking. % is a operator that returns the remainder, if the output is not 1, the number is not prime.the key in this task is making the method work for all numbers, by checking every number is greater than 1, we can start the method, otherwise we ignore it. This prevents an incorrect range to be used later.finally indentation is key, if we do not print our prime number after our division checks, then we will output a number everytime it passes a test.class Prime:   def method(self, lower, upper):       for num in range(lower, upper + 1):           if num > 1:               for i in range(2, num):                   if(num % i) == 0:                        break               else:                   print(num)

BN
Answered by Ben N. Python tutor

933 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Write a program that computes the sum of all numbers up to a input number n


Make a program that asks the user for a series of numbers until they either want to output the average or quit the program.


Write a function that takes a string, and outputs that string formatted in camelcase. (alternating upper and lower case for each character, e.g. cAmElCaSe)


Write a program that takes a value x and then outputs x Fibonnaci numbers. E.g. if x=6 output would be 1 1 2 3 5 8


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences