-1
\$\begingroup\$

I challenge you to generate the following sequence till 100 terms through any programming language in shortest characters possible.
The sequence is like this :-

1 2 3 4 5 10 20 30 40 50 100 200 300 400 500 ........ 

The output needs to be like this (above) only, nothing extra.

Remember, You can use + and - only and for mathematical calculations only (++ and -- are also allowed) and no other mathematical function like tan(), log(), pow(), ......etc.

Also, You cannot use any character variables in your code.

\$\endgroup\$
15
  • \$\begingroup\$ "You can use + and - only for mathematical calculations" So is this a [restricted-source] challenge? \$\endgroup\$ Commented Apr 16, 2014 at 2:35
  • \$\begingroup\$ Eh, seems easier to just treat them as strings and concat '0' each loop. \$\endgroup\$ Commented Apr 16, 2014 at 2:36
  • \$\begingroup\$ @algorithmshark can there be [restricted-source] + [code-golf]? \$\endgroup\$ Commented Apr 16, 2014 at 2:37
  • 1
    \$\begingroup\$ Wait a minute, if we can't use characters or strings, how do we output the numbers separated with spaces? Can they be newline separated instead? \$\endgroup\$ Commented Apr 16, 2014 at 2:44
  • 1
    \$\begingroup\$ “You can use + and - only for mathematical calculations” – Does this mean that I must not use + and - for something other than mathematical calculations or does this mean that I must not use any other mathematical operators than + and -? \$\endgroup\$ Commented Apr 16, 2014 at 14:48

9 Answers 9

6
\$\begingroup\$

GolfScript (21 chars)

If

You can use + and - only for mathematical calculations

means what it says then:

[6,1>~{4$10*}95*]' '* 

If it is intended to say

The only mathematical operators you can use are + and -

then the multiplications can be replaced with string manipulation at the same character count:

[6,1>~{4$0`+}95*]' '* 

Dissection:

[ # Start gathering values into an array 6,1>~ # Dump the numbers 1 to 5 onto the stack { # Beginning of loop body 4$ # Copy the 5th item on the stack 

Here the variants differ

 10* # Multiply it by 10 

or

 0`+ # Postpend '0' 

And then merge again

 }95* # Loop 95 times, so we end up with 100 items on the stack ] # Gather those 100 items into an array ' '* # Space-separate them 
\$\endgroup\$
2
  • \$\begingroup\$ I don't know GolfScript but 4$10* # Copy the 5th number on the stack and multiply by 10 -- Only addition subtraction and (in|de)crements are allowed \$\endgroup\$ Commented Apr 16, 2014 at 14:04
  • \$\begingroup\$ @user80551, that's not clear to me, but it's easily worked around at the same character count. \$\endgroup\$ Commented Apr 16, 2014 at 14:34
4
\$\begingroup\$

Perl, 38 bytes

Disclaimer: The restrictions of the changed question are not too clear to me.

for$i(0..19){print$_.0 x$i.$"for 1..5} 

The code uses variables with integer values except for $" which is used for the separation space. It can be easily replaced by ' ' (+1 byte).

Mathematical operations are not used, the code only contains string concatenations and the repetition operator.

The result are 100 numbers:

1 2 3 4 5 10 20 30 40 50 100 200 300 400 500 1000 2000 3000 4000 5000 10000 20000 30000 40000 50000 100000 200000 300000 400000 500000 1000000 2000000 3000000 4000000 5000000 10000000 20000000 30000000 40000000 50000000 100000000 200000000 300000000 400000000 500000000 1000000000 2000000000 3000000000 4000000000 5000000000 10000000000 20000000000 30000000000 40000000000 50000000000 100000000000 200000000000 300000000000 400000000000 500000000000 1000000000000 2000000000000 3000000000000 4000000000000 5000000000000 10000000000000 20000000000000 30000000000000 40000000000000 50000000000000 100000000000000 200000000000000 300000000000000 400000000000000 500000000000000 1000000000000000 2000000000000000 3000000000000000 4000000000000000 5000000000000000 10000000000000000 20000000000000000 30000000000000000 40000000000000000 50000000000000000 100000000000000000 200000000000000000 300000000000000000 400000000000000000 500000000000000000 1000000000000000000 2000000000000000000 3000000000000000000 4000000000000000000 5000000000000000000 10000000000000000000 20000000000000000000 30000000000000000000 40000000000000000000 50000000000000000000 

Ungolfed:

for $i (0..19) { # 20 * 5 = 100 numbers for $_ (1..5) { print $_ # first digit . 0 x $i # zeros . $" # separator (space) } } 
\$\endgroup\$
1
  • \$\begingroup\$ Not a bad solution. The shortest I could manage was 34: print 1..5,!($,="0$,"||$")for a..t \$\endgroup\$ Commented Apr 17, 2014 at 7:25
4
\$\begingroup\$

Python 60

r=range for i in r(20): for j in r(1,6):print str(j)+'0'*i, 

Thanks to @ace for pointing out that this was way too long.

Python 113

from itertools import* i=j=0 for x in cycle(range(1,6)): i+=1;print str(x)+'0'*j, if i%5==0:j+=1 if i>99:break 

\$\endgroup\$
6
  • 2
    \$\begingroup\$ It should be shorter if you use two loops instead of itertools \$\endgroup\$ Commented Apr 16, 2014 at 9:16
  • \$\begingroup\$ for j in'12345' lets you omit the str() command, which brings you down to 51 characters, if you remove the first line and spell out range \$\endgroup\$ Commented Apr 16, 2014 at 14:39
  • \$\begingroup\$ @MrLemon j will then become a character variable which is not allowed. \$\endgroup\$ Commented Apr 16, 2014 at 14:58
  • \$\begingroup\$ @user80551 I totally overlooked that part, you are correct. \$\endgroup\$ Commented Apr 16, 2014 at 15:12
  • \$\begingroup\$ Use list comprehension and this will reduce to 47 characters [i*10**j for j in range(21)for i in range(1,6)] \$\endgroup\$ Commented Apr 16, 2014 at 15:47
3
\$\begingroup\$

GolfScript, 30 25 characters (26 23 newline separated)

;6,1>{.{10*}%}20*][]*' '* 

Commented:

; # pop the input string (empty) 6,1> # generate [1 2 3 4 5] {.{10*}%} # duplicate the array and multiply everything by 10 20* # do this 20 times (5 * 20 = 100) ] # wrap the whole stack in an array []* # flatten ' '* # join by space 

I really don't like the repetition at the end, but GS doesn't provide a flat_map or flatten of any sort. Thanks @PeterTaylor for providing a way to flatten!

If we can separate by newlines instead of spaces, then 23 characters:

;6,1>{.{10*}%}20*][]*n* 
\$\endgroup\$
5
  • \$\begingroup\$ You should create a programming language called DoorknobScript. \$\endgroup\$ Commented Apr 16, 2014 at 5:50
  • 1
    \$\begingroup\$ To get an array of 1 to 5 it's better to do either 6,(; or 6,1>. GS does provide a couple of ways to do one-level flatten: []* or {~}%. But to use either you'll need to remove the initial '' from the stack (which is currently giving you output with a leading space) or avoid including it by using an explicit [. \$\endgroup\$ Commented Apr 16, 2014 at 7:34
  • \$\begingroup\$ {.{10*}%} # duplicate the array and multiply everything by 10 Multiplying isn't allowed. \$\endgroup\$ Commented Apr 16, 2014 at 14:08
  • \$\begingroup\$ @user Where does it say that? Most of the other answers use multiplication too. \$\endgroup\$ Commented Apr 16, 2014 at 15:04
  • \$\begingroup\$ It says "Remember, You can use + and - only and for mathematical calculations only (++ and -- are also allowed)". Doesn't "only" means ..... "only"? \$\endgroup\$ Commented Apr 16, 2014 at 17:02
3
\$\begingroup\$

APL (17 24)

,⍉10⊥¨(⍳5)∘.{⍺,1↓⍵/0}⍳20 
\$\endgroup\$
2
  • \$\begingroup\$ Multiply and power are not allowed. \$\endgroup\$ Commented Apr 16, 2014 at 14:05
  • \$\begingroup\$ @user80551: ok I changed it \$\endgroup\$ Commented Apr 16, 2014 at 14:16
2
\$\begingroup\$

Java,185

class g {public static void main(String[] args){for(int x=0;x<20;x++){for(int i=1;i<6;i++){System.out.print(""+i); for(int j=0;j<x;j++){System.out.print("0");}System.out.print(" ");}}}} 
\$\endgroup\$
5
  • 1
    \$\begingroup\$ You forgot a space between int and j so your code didn't compile. I edited your answer to fix that. \$\endgroup\$ Commented Apr 16, 2014 at 13:31
  • 1
    \$\begingroup\$ It's also an infinite loop rather than printing 100 terms. \$\endgroup\$ Commented Apr 16, 2014 at 13:36
  • \$\begingroup\$ Oh ok, sorry I'll edit it. \$\endgroup\$ Commented Apr 16, 2014 at 13:38
  • 1
    \$\begingroup\$ You can get it down to 169 by 1) renaming args 2) restructuring the loops and 3) declaring your ints up front : class G{public static void main(String[]a){int x=0,i,j;for(;x<20;x++)for(i=0;i++<5;System.out.print(" ")){System.out.print(""+i);for(j=0;j++<x;System.out.print("0"));}}} \$\endgroup\$ Commented Apr 16, 2014 at 13:51
  • \$\begingroup\$ Can't you remove the space just before the third for \$\endgroup\$ Commented Apr 16, 2014 at 14:19
2
\$\begingroup\$

Julia - 38

[print(i,"0"^j," ") for i=1:5,j=0:19]; 

Output:

1 2 3 4 5 10 20 30 40 50 100 200 300 400 500 1000 2000 3000 4000 5000 10000 20000 30000 40000 50000 100000 ... 
\$\endgroup\$
3
  • 1
    \$\begingroup\$ -1 , multiplication and power are not allowed. \$\endgroup\$ Commented Apr 16, 2014 at 14:06
  • \$\begingroup\$ Now they are strings, so i think it doesnt break the rules \$\endgroup\$ Commented Apr 16, 2014 at 14:30
  • 1
    \$\begingroup\$ I just wrote the exact same solution before seeing yours. \$\endgroup\$ Commented Apr 16, 2014 at 17:43
2
\$\begingroup\$

C, 93

i;main(j,k){for(;++i;)for(j=0;++j<6;){for(putchar(48+j),k=0;++k<i;putchar(48));putchar(32);}} 

// it draws nice patterns on the terminal...

\$\endgroup\$
1
  • \$\begingroup\$ 87 bytes \$\endgroup\$ Commented Apr 10, 2020 at 19:54
2
\$\begingroup\$

PHP - 61

for($i=0;$i++<21;)for($j=0;$j++<5;)echo str_pad($j,$i,0).' '; 
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.