11
\$\begingroup\$

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?

\$\endgroup\$
11
  • 6
    \$\begingroup\$ i=2;exec"print i;i+=2;"*50 \$\endgroup\$
    – Vectorized
    Aug 11, 2014 at 14:37
  • 2
    \$\begingroup\$ This tip is located in the python golfing tips page codegolf.stackexchange.com/questions/54/…. \$\endgroup\$
    – Vectorized
    Aug 11, 2014 at 14:52
  • 4
    \$\begingroup\$ Is this supposed to start at 0 or 2? I don't python much, but it looks to me like your two examples do different things. Please correct me if not. \$\endgroup\$
    – Geobits
    Aug 11, 2014 at 16:03
  • 23
    \$\begingroup\$ print "0 10 100" (is binary OK?) \$\endgroup\$
    – r3mainer
    Aug 11, 2014 at 19:13
  • 2
    \$\begingroup\$ @squeamishossifrage don't forget to golf out the space ;) \$\endgroup\$
    – isaacg
    Aug 11, 2014 at 19:25

13 Answers 13

40
\$\begingroup\$

Python 2 - 12 characters

print 8**999

The decimal representation of all even numbers from 0 to 100 can be found in the output:

153778990270139647116444851659594064330089236967104214470764753645007350076834118596920008479824182447803706156756475613564110522612279602948135310258168541404369918794480627176627915013920083365328091029969610052054309789461709376676636344651086297099162082351332867728061686056465813162964114500668343488577962834185114919242101638217077550294093097112980059735456387540301162747936045475366317560310988720435512281742591085641505551107966844283901574058972330493685836063965131445246304097593431852972101058022587137885482726523043570690342524474585327775688980689010069001288756281975198668705741000141718184277589376710426738442847382969979234512669279398030637083755270090078676447687796406001053805898105262326290072552249025832780916090265261064205460488458795026145331708830141367124625271312584437671840499845750728447412590406684361326531266896486146862384988911439049971734022314877278748672

As a bonus, so can the odd numbers.

\$\endgroup\$
9
  • \$\begingroup\$ Only problem: It prints the odd numbers and a lot more too. \$\endgroup\$
    – seequ
    Aug 13, 2014 at 16:33
  • 1
    \$\begingroup\$ @Sieg, even and odd... kinda got all the bases covered, not sure how you find "a lot more" in that. ;) \$\endgroup\$
    – crthompson
    Aug 13, 2014 at 17:16
  • \$\begingroup\$ Isn't the first time I have a different opinion than other users. I like your answer anyways. \$\endgroup\$
    – seequ
    Aug 13, 2014 at 17:24
  • \$\begingroup\$ @paqogomez even numbers 0-100, the odd numbers, and a lot more even numbers >100 \$\endgroup\$
    – OJFord
    Aug 14, 2014 at 10:33
  • \$\begingroup\$ Too curious to avoid asking a dumb Q, why specifically 8*999? \$\endgroup\$
    – 0xc0de
    Aug 14, 2014 at 12:06
31
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Wouldn't OP's second answer in the edit make you think he wanted it newline separated? \$\endgroup\$
    – Moop
    Aug 11, 2014 at 17:31
  • 10
    \$\begingroup\$ 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. \$\endgroup\$
    – isaacg
    Aug 11, 2014 at 17:33
23
\$\begingroup\$

Python2 26

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

independent discovery of @bitpwner's solution

\$\endgroup\$
2
  • \$\begingroup\$ I get a SyntaxError when I try to run that. Should this answer's top line say "Python 2, 26"? \$\endgroup\$
    – user30594
    Aug 12, 2014 at 0:53
  • \$\begingroup\$ +1, you appear to have been 58 seconds faster to post this solution. :) \$\endgroup\$ Aug 17, 2014 at 14:47
20
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Should include 0, see Sparr's solution. \$\endgroup\$
    – isaacg
    Aug 11, 2014 at 17:23
  • \$\begingroup\$ I get a SyntaxError when I try to run that. Should this answer's top line say "Python 2 - 26"? \$\endgroup\$
    – user30594
    Aug 12, 2014 at 2:57
13
\$\begingroup\$

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.

\$\endgroup\$
10
\$\begingroup\$

Python 2 - 20 (Possibly not allowed)

This is python 2 specific and probably cheating since it prints the list, but since all numbers end up on the screen:

print range(0,101,2)
\$\endgroup\$
9
\$\begingroup\$

Python 2, 28

for i in range(51):print 2*i
\$\endgroup\$
7
\$\begingroup\$

Python 2 in *NIX, 24

os.system('seq 0 2 100')

If you need to add

import os

Then the total is 33 characters.

\$\endgroup\$
4
  • \$\begingroup\$ Clever - This might well be superior in some cases. Is it OS specific? \$\endgroup\$
    – isaacg
    Aug 11, 2014 at 19:54
  • 4
    \$\begingroup\$ @isaacg Very much so, as os.system calls a system program. \$\endgroup\$
    – seequ
    Aug 11, 2014 at 20:31
  • 3
    \$\begingroup\$ If that's allowed, then os.system('a'), because I happen to have a program called a with the desired behaviour on my system.. \$\endgroup\$
    – OJFord
    Aug 13, 2014 at 12:33
  • 5
    \$\begingroup\$ @OllieFord seq is part of gnu coreutils. You don't have quite as much clout. \$\endgroup\$
    – Sparr
    Aug 14, 2014 at 1:55
5
\$\begingroup\$

Python 3, 29

*a,=map(print,range(0,101,2))

If you're in Python 2 and happen to have already imported the print function, you don't have to make the iterator object into a list and it becomes 25 characters:

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

I don't know that that's entirely fair though.

Here's another fun idea that works in python 2 or 3. It's a tad longer.

def p(i):
    if i+2:p(i-2);print i
p(100)
\$\endgroup\$
2
  • 1
    \$\begingroup\$ The Python 3 version can be shortened to *a,=map(print,range(0,101,2)) using Extended Iterable Unpacking. \$\endgroup\$
    – flornquake
    Aug 13, 2014 at 23:12
  • \$\begingroup\$ @flornquake Thanks, I've updated it. \$\endgroup\$
    – IanH
    Aug 14, 2014 at 1:47
4
\$\begingroup\$

Python 2 - 24

0;exec"_+=2;print _;"*50

(based on bitpwner and Sparr solution)

In the shell, "_" contains the value of the previously evaluated expression

\$\endgroup\$
3
  • \$\begingroup\$ You should note that this only works, if pasted into the interactive mode of some Python interpreters (e.g., it does not work in IPython) and not as a script. \$\endgroup\$
    – Wrzlprmft
    Aug 12, 2014 at 12:10
  • \$\begingroup\$ you're right, i forgot to mention that. (btw works in the standard Python console) \$\endgroup\$
    – 6160
    Aug 12, 2014 at 12:13
  • 1
    \$\begingroup\$ -1 -- The question explicitly mentions a python script, which means the code should be written to a file and executed. Otherwise a better solution would simply be:range(0,102,2) since this would display all the even numbers (plus some commas and two brackets) on the screen. \$\endgroup\$
    – Bakuriu
    Aug 13, 2014 at 11:41
2
\$\begingroup\$

Python 2 - 20 (questionable)

If isaacg's space-separated solution is OK, then presumably the normal list formatting is as well:

print range(0,101,2)

For further rule-twisting, apply SirBraneDamuj's suggestion from the comments:

print range(101)

and you are at 16. So what if there's some extra garbage separating them?

\$\endgroup\$
1
\$\begingroup\$

Python List Comprehension - 39

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(`2*x`for x in range(51))

Using a map and @isaacg's suggestion it ends up being the same 39 characters:

print'\n'.join(map(str,range(0,101,2)))

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

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – isaacg
    Aug 11, 2014 at 19:51
  • 1
    \$\begingroup\$ @isaacg Updated to answer the question, now much longer, needing to embed each value in str() for the join kills it. \$\endgroup\$
    – Ed Griebel
    Aug 11, 2014 at 21:11
  • \$\begingroup\$ @Wrzlprmft Thanks for the suggestion, didn't know I could do that! Code changed to reflect this new knowledge. \$\endgroup\$
    – Ed Griebel
    Aug 12, 2014 at 15:01
0
\$\begingroup\$

In my case, I use list comprehension (in Python 3.9). < Sorry, I came late but I hope it makes sense.>

even_numbers = [x for x in range(100) if x % 2 == 0]
print(even_numbers)
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Welcome to Code Golf! This site is for competitive programming, so we require answers to make a serious attempt at golfing. Make sure to read our tips questions if you want some hints! \$\endgroup\$ Apr 20, 2021 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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