11
2
I'm work on a problem which I set myself for fun, which is to create a python script which prints the even numbers from 0 to 100. The challenge is to make the script as small as possible. This is what I have so far:
for x in range(0, 101):
if (x % 2 == 0):
print x
Currently it is 60 bytes. Can anyone think of a way to make it smaller?
Edit: print(*range(2,101,2),sep='\n')
which is 30 bytes. Any smaller?
4
i=2;exec"print i;i+=2;"*50
– Vectorized – 2014-08-11T14:37:40.563bitpwner's thing has 26 bytes if anyone cares – Kevin L – 2014-08-11T14:39:37.997
@bitpwner Not all non-competition questions are off topic.
– Geobits – 2014-08-11T14:47:48.643Ah! My bad. Thanks for the link, was trying to hunt for it. – Vectorized – 2014-08-11T14:49:19.220
2
This tip is located in the python golfing tips page http://codegolf.stackexchange.com/questions/54/tips-for-golfing-in-python/1020#1020.
– Vectorized – 2014-08-11T14:52:49.5874Is this supposed to start at
0
or2
? I don't python much, but it looks to me like your two examples do different things. Please correct me if not. – Geobits – 2014-08-11T16:03:34.91320
print "0 10 100"
(is binary OK?) – r3mainer – 2014-08-11T19:13:40.7331@squeamishossifrage don't forget to golf out the space ;) – isaacg – 2014-08-11T19:25:07.037
print(*range(101),sep='\n')
dang, not even my pedantic "you didn't say I couldn't print the numbers in between!" solution isn't as short as the other one :( – Zach Thacker – 2014-08-11T20:48:26.480Cannot compete, but for completeness a recursive solution:
f=lambda i:i and"%s\n%i"%(f(i-2),i);print f(100)
– Wrzlprmft – 2014-08-11T22:09:21.000@Geobits raises a good point; it should be noted that
– ajp15243 – 2014-08-12T17:04:15.6400
is even.