# Print all odd numbers from 0 to 100000

My teacher gave me a simple problem which I should solve with a code tiny as possible. I should print all odd numbers from 0 to 100000 into a HTML document using javascript.

for(var y = 0; y < 100000; y++){
if(!(y % 2 == 0)){
document.write(y + " ");
}
};


Is it possible to do that shorter?

• By shorter do you mean Javascript only or any language? – Sp3000 Dec 25 '14 at 12:09
• -1 for needlessly restriction on the language – Johannes Kuhn Dec 25 '14 at 17:52
• – Martin Ender Dec 25 '14 at 18:16
• -1 for asking us to do your homework better. – R-D Dec 27 '14 at 1:36
• Did you teacher specify that only the odd numbers were to be printed? – Flygenring Jan 9 '15 at 2:16

# JavaScript, 38 bytes

Yet another way to do this, shortest of all.

This supposedly prints each number on a new line using \n. Now as the data is being written to an HTML document, \n is not of any use and the numbers appear to be separated by space only.

for(i=1;i<1e5;i+=2)document.writeln(i)

• @proudhaskeller more rep this way ;) – hobbs Dec 26 '14 at 4:10
• Or the alternative for(i=0;++i<1e5;)document.writeln(i++). I've found plenty, but none beats 38. Good job. – Domino Nov 2 '15 at 19:05

# Javascript, 36 bytes

If order doesn't matter:

for(i=1e5;i--;)document.writeln(i--)


Writes the numbers in reverse order.

another way to do it in 38 bytes:

for(i=0;i<5e4;)document.writeln(i+++i)


explanation:i+++i means the same as (i++)+i, with the second i resolving to its new value (as effected by the ++) so it goes like this for the results:

• 0+1=1
• 1+2=3
• 2+3=5

etc...

• Can you explain the i+++i condition? Thanks – NiCk Newman Nov 1 '15 at 12:21
• @NiCkNewman see edit. – Jasen Nov 2 '15 at 4:12
• Learn something new everyday, thanks Jasen. – NiCk Newman Nov 2 '15 at 13:32

# JavaScript, 40 bytes

for(i=1;i<1e5;i+=2)document.write(i+" ")

The code is fairly straight forward.

• I iterate from i=1 to i=1e5. 1e5 is nothing but 1 followed by 5 0, so 100000.
• In each iteration step, I print the value of i and increment i by 2. Thus printing only every other number starting from 1 and till 100000.

This prints all the odd numbers from 1 to 100000.

# JavaScript, 40 bytes

Another way to do it!

for(i=0;i<5e4;)document.writeln(2*++i-1)

The code is fairly straight forward.

• Iterate from i = 0 to i = 5e4. 5e4 is nothing but 5 followed by 4 0, so 50000.
• In each iteration step, print the value of 2*i - 1 after incrementing i by 1. Thus printing only odd numbers from 1 to 99999.

Using while 42

i=-1;while((i+=2)<10e5)document.writeln(i)