Python Forum
Return prime numbers from range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return prime numbers from range
#1
Hi Guys,

Let me begin by confesing I am new to coding.
I understand my problem might seem basic to some but I am also sure you're eager to help one learn.

Here we go. I came up with a problem, I wanted to write a program that will return all prime numbers from a range set by user. I end up not being able to write it and found below code online. It works fine but it also puzzles me why. Especially this line - if (number % i) == 0:

This will break if a number in range "i" is divisible by any number in range "number".
This makes sense to me except for the fact that eventually number will be divided by itself which would also exclude all prime numbers. I wanted to write a line that would run modulo between "I" and "number" excluding I from number, but it doesn't seem necessary.

Help, can you please unconfused me? Why does it work?


print ("all primer numbers in a selected range")

a = int(input("enter bottom value of the range: "))
b = int(input("enter top value of the range: "))

for number in range(a, b +1):
if number > 1:
for i in range(2, number):
if (number % i) == 0:
break
else:
print(number)
Reply
#2
Make sure you use the python tags for your code, so that it properly shows indentation.

I think your confusion is on the range() function. Lets look at the docs for it (which you can do by typing help(range) at an interactive prompt.

>>> help(range)
Help on class range in module builtins:

class range(object)
 |  range(stop) -> range object
 |  range(start, stop[, step]) -> range object
 |
 |  Return an object that produces a sequence of integers from start (inclusive)
 |  to stop (exclusive) by step.  range(i, j) produces i, i+1, i+2, ..., j-1.
 |  start defaults to 0, and stop is omitted! ...
So the important thing is that the stop element of the range is not returned. (This turns out to be very handy for properly treating ranges that line up with each other). But in your case since number is given as the stop for the range, it will not be returned. So the number % i expression will never be evaluated with i == number.
Reply
#3
Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,636 Jan-05-2024, 08:30 PM
Last Post: sgrey
  prime numbers with iterator and generator cametan_001 8 1,862 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  prime numbers astral_travel 28 3,631 Nov-08-2022, 09:23 PM
Last Post: astral_travel
  matplotlib x axis range goes over the set range Pedroski55 5 3,175 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 7,017 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  Is 2 a prime number? for loop & range fuction in python docs says yes, mine says no. allusernametaken 4 2,893 Nov-17-2019, 02:56 AM
Last Post: allusernametaken
  Prime numbers Anderi02 1 1,965 Oct-13-2019, 04:49 PM
Last Post: ichabod801
  first k non-prime numbers arycloud 11 7,226 Jul-09-2019, 02:19 PM
Last Post: abhi19935
  first k non prime numbers print bsrohith 7 7,501 Jun-20-2019, 10:48 AM
Last Post: arycloud
  count occurrence of numbers in a sequence and return corresponding value python_newbie09 6 3,462 May-20-2019, 06:33 PM
Last Post: python_newbie09

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020