[Solved] Prime numbers in given range Python [closed]


lower = int(input("Give lower limit: "))               # Get lower bound, must be an int
upper = int(input("Give upper limit: "))               # Get upper bound, must be an int
print()                                                # Print an empty line
primes = []                                            # An array to store the primes in
for i in range(lower, upper+1):                        # Go through the range
    if i == 1:
        print("1 is not a valid start number")
    else:
        primeFlag = True                               # Will only remain as true if the number is prime
        for div in range(2, i):                        # Go from 2 to the current number
            if i % div == 0:
                primeFlag = False                      # If this happens, it isnt prime
        if(primeFlag):                                 # If it is a prime
            primes.append(i)                           # Add to primes array
            print(i," is a prime number")
        else:
            print(i," is not a valid prime")

print((upper-lower+1), " numbers were examined, ", len(primes), " were prime numbers")
print("The last found prime number is ", primes[-1:])

Will produce:

Give lower limit: 1
Give upper limit: 10

1 is not a valid start number
2  is a prime number
3  is a prime number
4  is not a valid prime
5  is a prime number
6  is not a valid prime
7  is a prime number
8  is not a valid prime
9  is not a valid prime
10  is not a valid prime
10  numbers were examined,  4  were prime numbers
The last found prime number is  [7]

I hope this is helpful, its gets you almost to the output you want, please take the time to understand and learn from this.

2

solved Prime numbers in given range Python [closed]