Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Find the prime numbers in the given range that ends with digit 1 and print them

Input format:

Get the range

Output format:

Print prime numbers from between the given range that ends with 1

Sample input/output:

1

100

11 31 41 61 71

What error did I make?

What I have tried:

l=int(input('enter the lowest'))
h=int(input('enter the highest'))
for i in range(l,h+1):
    if i>1:
        for e in range(2,i):
            if(i%e)==0:
                break
            else:
                if i%10==1:
                    print(i)
Posted
Updated 2-Nov-22 18:24pm

Quote:
How do I find the prime numbers in a given interval ending with 1

Advice: Separate concerns , it makes things easier!
A number being prime or not is pretty well defined and do not depend on anything other that the number itself.
Python
def IsPrime(n)
  ...
  return ...


Look at your code !
You know that you only want to display primes with a 1 in unit digit, checking the unit digit is rather easy and fast, checking if a number is a primes is harder.
Look for easiest path:
Python
l=int(input('enter the lowest'))
h=int(input('enter the highest'))
unit= l%10
if unit !=1:
    # adjust unit to next number with unit 1
    l= ...
while l < h:
    # check if l is prime and do stuff
    l= l+10
# end

Quote:
What error did I make?

You can find by yourself by using the tool of choice: the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
CPallini 3-Nov-22 3:20am    
5.
Patrice T 3-Nov-22 4:39am    
Thank you.
All prime numbers are, by definition, odd so you only need to divide by odd numbers. Also, you only need to divide by the numbers up to its square root, as all values above that will already have been tested. And finally you need to ignore dividing by itself. So the algorithm is:
// given any number
div = 3
max = sqrt(number)
while div < max
{
    if div != number AND number % div == 0
        return // not a prime
    div = div + 2 // try next divisor
}
print "number is a prime"
 
Share this answer
 
To add to what Richard has said, to work out if a prime number ends with a specific digit, use The Python Modulo Operator[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900