Archive for the ‘writing scripts’ tag
Primes Problem in Python
Lately I decided to give SPOJ website a try ..
And my first problem after the TEST problem was the “PRIME1” problem..
Which is basically finding the prime numbers between 2 given numbers ..
Yesterday, regardless the fact of being truly ill and I hardly could open my eyes ..
I decided to solve the problem ..
and after I think 1 hour It started to output relatively right solution ..
here is my code in python:
times = input()
while times > 0:
times -= 1
limits = raw_input()
limits = limits.split(" ")
downLimit = int(limits[0])
upLimit = int(limits[1])
numsList = []
numsList = [i for i in range(downLimit,upLimit) if (i%2 or i==2) and i != 1 and i!=0]
primesList = list(numsList)
for oddNumber in numsList:
primeNumber = True;
for number in range( 2 , oddNumber / 2):
if oddNumber % number == 0:
primeNumber = False;
break;
if not primeNumber:
primesList.remove( oddNumber )
for primeNumber in primesList:
print primeNumber
Notes:
1-in line 9, I used list() to assign the numsList value to the primesList list .. not the numsList as python-strangely- assigns the old list reference to the new list and that caused +1 hour of debug .. :@
2-As shown in page title after submitting the problem they replied that solution exceed the time limit .. so any help will be appreciated ..
3-Python version 2.6.2 on Mac
Me, Kamasheto, Books and Beans ..
My Dear Friend Kamasheto and I went to books and beans after my depiak session “programming introduction” we spent about 3-4 hours kidding and chatting and MAINLY coding and PHPing arguing with each other ..
NOTE: kamasheto enjoyed the free everything [specially water] till we had to pay 18 LE for very normal drinks
Then we went to Zizo … ana bas a5dt 2 kofta w wa7ed mshakel ..
I discussed with him the future of developers and he asked me about the reason I asked about that .
then he talked about who I’m looking forward to be ..
and finally we tried having rules for the kpoint game..
I and Kama used to play a game who can get more points by showing that the other person is wrong :p
or
is saying that he knows and he is actually IMPROVISING without prior knowledge “byefte” ![]()
AND I REALLY ENJOY THIS GAME
[conclusion]
We disagreed on many things and we agreed on many other things but on overall it was a very
1-Useful
2-Funny
3-Emotional sometimes
4-healthy , Except for Zizo part ..
and that what you can really summarize in a friendly hang out ..
Kamasheto is going to be out of the country for the following [Time interval] and We will not be able to see each other
And I’ll miss him