Tuesday, 11 July 2017

A Python Script to Solve Prime Numbers

Update Prime_numbers
This is an algorithm that finds prime numbers in a given range of numbers. How it works is this:
1) Your number must be more than 1, it excludes 1 from its calculation because any number less than 2 is not a prime number.
2)Input your first number, which should be your starting point, and should be lower than the second number
3) Input your 3rd number, which is the end point and the higher number.  Now due to how range function behaves, the last number is always excluded, so i tried to catch the last number by doing max+1, since the last number in range is always short of 1
4)if the numbers entered are greater than 1, it jumps to the next code
5) Now do a range of the 2 numbers you entered by , adding extra 1 to the last number
6) Since 2 is a prime number, i will let it print, by printing num if num equals 2. This is so that while running the programming, it won't exclude 2 as a prime number as 2%2 == 0.
7)Apart from that, all other numbers that are divisible by 2 are not printed, same with other numbers which are divisible, starting by 2, to the num. Remember ending it at num excludes the num, and run only from 2 to the number before num
8) If it encounters any number in range of 2 and num that is divisible by num, it will break the loop, as the number is no longer a prime a number.
9)Else, it prints all the numbers that are only divisible by 1 and itself
10) For the code below the first one posted, it just prints the prime numbers starting from highest to lowest, the "-1" at the end makes it possible. And note this only works if your lower number was greater than you higher number.
11) The new update prints the numbers which are less than 2 and tell the user that such numbers are lesser than 2 or they are not greater than 1
Image result for python


print "Dont enter numbers less than 2. All numbers should be more than 1"

min = int(raw_input("Enter the lower number here>>>"))
max = int(raw_input("Enter the higher number here>>>"))

for num in range(min, max+1):
    if num > 1:
        for i in range(2, num):
            if num == 2:
                print num
            else:
                if num % i == 0:
                    break
        else:
            print num
    else:
        print " %d is not greater than 1" %min
            
           
#the below code prints the number inversely if your first input should be higher than your second input
print "Dont enter numbers less than 2. All numbers should be more than 1"

min = int(raw_input("Enter the lower number here>>>"))
max = int(raw_input("Enter the higher number here>>>"))

for num in range(min, max+1, -1):
    if num > 1:
        for i in range(2, num):
            if num == 2:
                print num
            else:
                if num % i == 0:
                    break
        else:
            print num
    else:
        print " %d is not greater than 1" % min        


1 comment:

  1. I enjoy your writing style genuinely enjoying this website.

    ReplyDelete