Search
Latest topics
Who is online?
In total there are 5 users online :: 0 Registered, 0 Hidden and 5 Guests :: 1 BotNone
Most users ever online was 38 on Sun Mar 19, 2023 10:07 pm
Most Viewed Topics
[Python] Prime Numbers Script
Page 1 of 1
[Python] Prime Numbers Script
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:
[You must be registered and logged in to see this link.] 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
[You must be registered and logged in to see this link.] 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"
[You must be registered and logged in to see this link.] lists to iterate next
num_ran = []
for i in lines:
r = range(i[0], (i[1] + 1))
num_ran.append(r)
[You must be registered and logged in to see this link.] 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
[You must be registered and logged in to see this link.] 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()
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:
[You must be registered and logged in to see this link.] 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
[You must be registered and logged in to see this link.] 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"
[You must be registered and logged in to see this link.] lists to iterate next
num_ran = []
for i in lines:
r = range(i[0], (i[1] + 1))
num_ran.append(r)
[You must be registered and logged in to see this link.] 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
[You must be registered and logged in to see this link.] 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()
cloud9- Moderator
- Posts : 38
Join date : 2014-04-09
Age : 34
Similar topics
» Creating a new Python script
» Basics of writing a python script
» Simple Numbers Mind reading game.
» Python to exe
» Multiencoder (Python)
» Basics of writing a python script
» Simple Numbers Mind reading game.
» Python to exe
» Multiencoder (Python)
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Tue Feb 02, 2021 7:12 am by manas41
» SQL injection and Quote escaping
Sun Jun 28, 2015 11:42 am by ADS1
» [TUT] Chmod: Files & Permissions [TUT]
Thu Jun 04, 2015 12:45 pm by Guest
» Reaver pixiewps
Thu Jun 04, 2015 12:23 pm by voidfletcher
» How To Crash Someone's Skype in 10 SECONDS
Thu Jun 04, 2015 12:20 pm by voidfletcher
» Internet Security & IP Security (IPSec)
Mon May 18, 2015 9:00 pm by voidfletcher
» [Python] Infinite / Definite File Generator
Mon May 18, 2015 8:58 pm by ADS1
» [C#] String Case-Inversion
Mon May 18, 2015 8:57 pm by ADS1
» Rekall Memory Forensic Framework
Sat May 16, 2015 8:55 pm by ADS1