Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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?

share|improve this question
1  
i=2;exec"print i;i+=2;"*50 –  bitpwner 13 hours ago
    
bitpwner's thing has 26 bytes if anyone cares –  Kevin L 13 hours ago
2  
This tip is located in the python golfing tips page codegolf.stackexchange.com/questions/54/…. –  bitpwner 13 hours ago
2  
print "0 10 100" (is binary OK?) –  squeamish ossifrage 8 hours ago
1  
@squeamishossifrage don't forget to golf out the space ;) –  isaacg 8 hours ago

7 Answers 7

Python2 26

i=0;exec"print i;i+=2;"*51

independent discovery of @bitpwner's solution

share|improve this answer
    
I get a SyntaxError when I try to run that. Should this answer's top line say "Python 2, 26"? –  Ricky Demer 3 hours ago

Python 2 - 26

i=0;exec"print i;i+=2;"*51

Based on the tip on exec with string multiplication found at Tips for golfing in Python.

share|improve this answer
    
Should include 0, see Sparr's solution. –  isaacg 10 hours ago
    
I get a SyntaxError when I try to run that. Should this answer's top line say "Python 2 - 26"? –  Ricky Demer 1 hour ago

Python 3, 22 (Possibly not allowed)

If the challenge is "to create a python script which prints the even numbers from 0 to 100" and not "to create a python script which prints the even numbers from 0 to 100, newline separated", then the shortest solution is:

print(*range(0,101,2))

Remember, it's very important in code golf not to put any limitations on yourself you don't have to - do what the problem asks, and no more.

share|improve this answer
    
Wouldn't OP's second answer in the edit make you think he wanted it newline separated? –  Moop 10 hours ago
2  
That's entirely possibly, and is why possibly not allowed is in the title. On the other hand, it's also possible that he/she got caught up in matching the output of his initial program, and forgot the initial problem spec. Since I don't know which is the case, I gave this answer. –  isaacg 10 hours ago

Python 2, 28

for i in range(51):print 2*i
share|improve this answer

Python 2 in *NIX, 24

os.system('seq 0 2 100')

If you need to add

import os

Then the total is 33 characters.

share|improve this answer
    
Clever - This might well be superior in some cases. Is it OS specific? –  isaacg 8 hours ago
1  
@isaacg Very much so, as os.system calls a system program. –  Sieg 7 hours ago

Python List Comprehension - 42

This uses a list comprehension, one trick to make it shorter is to multiply the index by 2 rather than going from 0 to 100 and a trailing if x % 2 check for even.

print'\n'.join(str(2*x)for x in range(51))

Edit: After separating values by newline it is by far not the shortest.

share|improve this answer
1  
This doesn't answer the question - OP says prints the even numbers from 0 to 100, not just generate them. Also, if you just want the list, range(0,101,2) is shorter. –  isaacg 8 hours ago
1  
@isaacg Updated to answer the question, now much longer, needing to embed each value in str() for the join kills it. –  Ed Griebel 7 hours ago
    
In Python 2, you can shorten this by using two ` instead of str. (Sorry, cannot post the example due to code formatting being triggered.) –  Wrzlprmft 6 hours ago

Python 2, 26 (possibly not allowed)

i=102
while i:i-=2;print i

It wasn’t strictly specified, in which order the numbers were to be printed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.