Author Topic: [Python] Prime Numbers Script  (Read 372 times)

0 Members and 1 Guest are viewing this topic.

Offline MadJ0ker

  • /dev/null
  • *
  • Posts: 13
  • Cookies: -15
    • View Profile
[Python] Prime Numbers Script
« on: December 31, 2014, 02:23:19 am »
Hello :)

I was starting to code again in python so to practice my skills I was solving some of the problems of SPOJ.
I'd like to hear your opinion about this code. I know python has some libraries to do this faster and easier, but the point was to practice, so I just used built-in stuff.

What this piece of code does is asking user for t lines. Each t line has m and n (separated by space). Then it outputs the prime numbers between m and n. There's some limitations on m and n tho'.

Any kind of suggestions, tips, recommendations are welcome!
Thank you in advance.
Code: [Select]
#Asks for t
while True:
    t = int(raw_input("Input number of lines: "))
    if t > 10 or t <= 0:
        print "This value can't be greater than ten, equal or less than zero!"
    else:
        break

#Ask for m and n
lines = []
count = 0
while count < t:
    for i in range(1, (t + 1)):
        line = raw_input("Input m y n: ")
        line = line.split()
        line = [int(i) for i in line]             
        m, n = line[0], line[1]
        if (m >= 1) and (n <= 1000000000):
            if (n - m <= 100000) and (m < n):
                lines.append(line)
                count += 1
            else:
                print "Invalid values"
        else:
            print "Invalid values"

#Gets lists to iterate next
num_ran = []
for i in lines:
    r = range(i[0], (i[1] + 1))
    num_ran.append(r)

#Search the prime numbers
for i in range(0, len(num_ran)):
    for j in num_ran[i][:]:
        if j == 1 or j == 4:
            num_ran[i].remove(j)
        for k in range(2, (j/2)):
            if j%k == 0:
                num_ran[i].remove(j)
                break

#Just a little bit of decent output
c = 0
for i in lines:
    print "Prime numbers between %s and %s are:"%(i[0], i[1])
    print num_ran[c]
    c = c + 1
raw_input()