6
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ By shorter do you mean Javascript only or any language? \$\endgroup\$ – Sp3000 Dec 25 '14 at 12:09
  • 17
    \$\begingroup\$ -1 for needlessly restriction on the language \$\endgroup\$ – Johannes Kuhn Dec 25 '14 at 17:52
  • 7
    \$\begingroup\$ @JohannesKuhn language-specific questions asking for golfing advice are perfectly on-topic \$\endgroup\$ – Martin Ender Dec 25 '14 at 18:16
  • 3
    \$\begingroup\$ -1 for asking us to do your homework better. \$\endgroup\$ – R-D Dec 27 '14 at 1:36
  • 1
    \$\begingroup\$ Did you teacher specify that only the odd numbers were to be printed? \$\endgroup\$ – Flygenring Jan 9 '15 at 2:16
13
\$\begingroup\$

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)

| improve this answer | |
\$\endgroup\$
  • 13
    \$\begingroup\$ Just edit your existing answer(s) \$\endgroup\$ – proud haskeller Dec 25 '14 at 17:53
  • 3
    \$\begingroup\$ @proudhaskeller more rep this way ;) \$\endgroup\$ – hobbs Dec 26 '14 at 4:10
  • \$\begingroup\$ Or the alternative for(i=0;++i<1e5;)document.writeln(i++). I've found plenty, but none beats 38. Good job. \$\endgroup\$ – Domino Nov 2 '15 at 19:05
7
\$\begingroup\$

Javascript, 36 bytes

If order doesn't matter:

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

Writes the numbers in reverse order.

| improve this answer | |
\$\endgroup\$
6
\$\begingroup\$

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

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

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.

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

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.
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Using while 42

i=-1;while((i+=2)<10e5)document.writeln(i)
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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