Smallest python script to print even numbers 0 to 100

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?

Benedict Lewis

Posted 2014-08-11T14:31:19.917

Reputation: 283

4i=2;exec"print i;i+=2;"*50 – Vectorized – 2014-08-11T14:37:40.563

bitpwner'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.643

Ah! 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.587

4Is 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. – Geobits – 2014-08-11T16:03:34.913

20print "0 10 100" (is binary OK?) – r3mainer – 2014-08-11T19:13:40.733

1@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.480

Cannot 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 0 is even.

– ajp15243 – 2014-08-12T17:04:15.640

Answers

37

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.

feersum

Posted 2014-08-11T14:31:19.917

Reputation: 29 566

Only problem: It prints the odd numbers and a lot more too. – seequ – 2014-08-13T16:33:28.213

1@Sieg, even and odd... kinda got all the bases covered, not sure how you find "a lot more" in that. ;) – paqogomez – 2014-08-13T17:16:50.337

Isn't the first time I have a different opinion than other users. I like your answer anyways. – seequ – 2014-08-13T17:24:31.800

@paqogomez even numbers 0-100, the odd numbers, and a lot more even numbers >100 – OJFord – 2014-08-14T10:33:35.900

Too curious to avoid asking a dumb Q, why specifically 8*999? – 0xc0de – 2014-08-14T12:06:04.493

@0xc0de It's one of the shortest way to obtain a big number that satisfy the property requested. Other examples are 2**952, 2**964, 2**975, 3**553, 3**612 and other 1231 with the base < 10 and exponent < 1000. – Bakuriu – 2014-08-14T12:12:29.470

@OllieFord, thank you for correcting someone that was wrong on the internet

– paqogomez – 2014-08-14T13:47:15.777

@paqogomez Isn't that exactly what you did in the first place..? No need to be so defensive, I was just suggesting that may have been what Sieg meant. – OJFord – 2014-08-14T14:01:05.460

@OllieFord No need to spoil feersum's excellent answer with a drawn out misunderstanding. I am just having fun. (notice the :)) ;-) – paqogomez – 2014-08-14T14:12:39.073

31

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.

isaacg

Posted 2014-08-11T14:31:19.917

Reputation: 39 268

1Wouldn't OP's second answer in the edit make you think he wanted it newline separated? – Moop – 2014-08-11T17:31:38.977

10That'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 – 2014-08-11T17:33:19.267

22

Python2 26

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

independent discovery of @bitpwner's solution

Sparr

Posted 2014-08-11T14:31:19.917

Reputation: 5 758

I get a SyntaxError when I try to run that. Should this answer's top line say "Python 2, 26"? – None – 2014-08-12T00:53:45.893

+1, you appear to have been 58 seconds faster to post this solution. :) – Ilmari Karonen – 2014-08-17T14:47:56.587

19

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.

Vectorized

Posted 2014-08-11T14:31:19.917

Reputation: 3 486

Should include 0, see Sparr's solution. – isaacg – 2014-08-11T17:23:35.207

I get a SyntaxError when I try to run that. Should this answer's top line say "Python 2 - 26"? – None – 2014-08-12T02:57:25.307

13

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.

Wrzlprmft

Posted 2014-08-11T14:31:19.917

Reputation: 2 772

9

Python 2, 28

for i in range(51):print 2*i

fsfd1100

Posted 2014-08-11T14:31:19.917

Reputation: 849

9

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)

hacatu

Posted 2014-08-11T14:31:19.917

Reputation: 229

7

Python 2 in *NIX, 24

os.system('seq 0 2 100')

If you need to add

import os

Then the total is 33 characters.

Ken A

Posted 2014-08-11T14:31:19.917

Reputation: 585

Clever - This might well be superior in some cases. Is it OS specific? – isaacg – 2014-08-11T19:54:15.443

4@isaacg Very much so, as os.system calls a system program. – seequ – 2014-08-11T20:31:35.423

3If that's allowed, then os.system('a'), because I happen to have a program called a with the desired behaviour on my system.. – OJFord – 2014-08-13T12:33:29.387

5@OllieFord seq is part of gnu coreutils. You don't have quite as much clout. – Sparr – 2014-08-14T01:55:18.847

4

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)

IanH

Posted 2014-08-11T14:31:19.917

Reputation: 201

1

The Python 3 version can be shortened to *a,=map(print,range(0,101,2)) using Extended Iterable Unpacking.

– flornquake – 2014-08-13T23:12:11.667

@flornquake Thanks, I've updated it. – IanH – 2014-08-14T01:47:44.667

3

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

le_vine

Posted 2014-08-11T14:31:19.917

Reputation: 701

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. – Wrzlprmft – 2014-08-12T12:10:38.943

you're right, i forgot to mention that. (btw works in the standard Python console) – le_vine – 2014-08-12T12:13:19.560

1-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. – Bakuriu – 2014-08-13T11:41:13.640

1

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.

Ed Griebel

Posted 2014-08-11T14:31:19.917

Reputation: 111

1This 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 – 2014-08-11T19:51:41.433

1@isaacg Updated to answer the question, now much longer, needing to embed each value in str() for the join kills it. – Ed Griebel – 2014-08-11T21:11:19.493

@Wrzlprmft Thanks for the suggestion, didn't know I could do that! Code changed to reflect this new knowledge. – Ed Griebel – 2014-08-12T15:01:49.817

1

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?

user30687

Posted 2014-08-11T14:31:19.917

Reputation: 11