Print all odd numbers from 0 to 100000

5

0

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?

Max Krause

Posted 2014-12-25T12:08:00.353

Reputation: 161

By shorter do you mean Javascript only or any language? – Sp3000 – 2014-12-25T12:09:35.310

The problem should be solved in JS only. – Max Krause – 2014-12-25T12:10:33.267

17-1 for needlessly restriction on the language – Johannes Kuhn – 2014-12-25T17:52:05.510

7

@JohannesKuhn language-specific questions asking for golfing advice are perfectly on-topic

– Martin Ender – 2014-12-25T18:16:26.313

3-1 for asking us to do your homework better. – R-D – 2014-12-27T01:36:58.210

The non-golfed Perl6 answer would be: say "{ 1, 3, 5, 7 ...^ *>100000 }" The golfed version would be: say "{1,3...9 x 5}" or say "{1,3...99999}" – Brad Gilbert b2gills – 2014-12-29T15:02:47.243

1Did you teacher specify that only the odd numbers were to be printed? – Flygenring – 2015-01-09T02:16:53.653

Answers

13

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)

Optimizer

Posted 2014-12-25T12:08:00.353

Reputation: 25 836

13Just edit your existing answer(s) – proud haskeller – 2014-12-25T17:53:24.933

3@proudhaskeller more rep this way ;) – hobbs – 2014-12-26T04:10:54.260

Or the alternative for(i=0;++i<1e5;)document.writeln(i++). I've found plenty, but none beats 38. Good job. – Domino – 2015-11-02T19:05:44.133

7

Javascript, 36 bytes

If order doesn't matter:

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

Writes the numbers in reverse order.

Noyo

Posted 2014-12-25T12:08:00.353

Reputation: 943

6

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...

Jasen

Posted 2014-12-25T12:08:00.353

Reputation: 413

Can you explain the i+++i condition? Thanks – NiCk Newman – 2015-11-01T12:21:03.150

1@NiCkNewman see edit. – Jasen – 2015-11-02T04:12:30.947

Learn something new everyday, thanks Jasen. – NiCk Newman – 2015-11-02T13:32:38.017

5

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.

Optimizer

Posted 2014-12-25T12:08:00.353

Reputation: 25 836

4

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.

Optimizer

Posted 2014-12-25T12:08:00.353

Reputation: 25 836

0

Using while 42

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

Yahor Zhylinski

Posted 2014-12-25T12:08:00.353

Reputation: 301