12
\$\begingroup\$

I wrote a code that separates the numbers from 1 to 9999 by comma

void p(n){printf("%d,",n);if(n<9999){p(n+1);}}main(){p(1);}

Is there a way to make this short code without using a repeat statement?

\$\endgroup\$
1
  • 21
    \$\begingroup\$ Welcome to Code Golf! Just a word of caution: we'll do anything we can on this site to save a single byte, which usually leads to terrible code that should not be actually used for anything serious. \$\endgroup\$
    – Arnauld
    Commented Oct 24, 2023 at 10:17

7 Answers 7

16
\$\begingroup\$

C (gcc), 40 bytes by Sisyphus, exit by stack overflow

i;main(){main(dprintf(i++<9999,"%d,"));}

Try it online!

C (gcc), 41 bytes by dingledooper

i;main(){++i<1e4&&main(printf("%d,",i));}

Try it online!

C (gcc), 42 bytes, original

main(i){i<1e4&&main(i+1,printf("%d,",i));}

Try it online!

main can be used to recurse

\$\endgroup\$
7
  • 4
    \$\begingroup\$ 41: n;main(){++n<1e4&&main(printf("%d,",n));} \$\endgroup\$ Commented Oct 24, 2023 at 6:33
  • 3
    \$\begingroup\$ 39; main(i){for(;i<1e4;)printf("%d,",i++);} \$\endgroup\$ Commented Oct 24, 2023 at 19:10
  • 1
    \$\begingroup\$ @HudsonGraeme I follow rule without using a repeat statement? \$\endgroup\$
    – l4m2
    Commented Oct 25, 2023 at 0:29
  • 1
    \$\begingroup\$ 40: i;main(){main(dprintf(i++<9999,"%d,"));} \$\endgroup\$
    – Sisyphus
    Commented Oct 25, 2023 at 6:25
  • 2
    \$\begingroup\$ "exit by stack overflow" That phrase just embodies everything I love about this site so well. \$\endgroup\$ Commented Oct 27, 2023 at 1:02
14
\$\begingroup\$

C (gcc) (Linux only), 31 bytes

main(){system("seq -s, 9999");}

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ 31; main(){system("seq -s, 9999");} \$\endgroup\$ Commented Oct 24, 2023 at 20:26
  • 1
    \$\begingroup\$ That's what I started with, then for some reason, I thought it needed a space after the commas. \$\endgroup\$
    – AShelly
    Commented Oct 24, 2023 at 20:47
  • \$\begingroup\$ Hm, surprised seq is not POSIX \$\endgroup\$
    – pipe
    Commented Oct 24, 2023 at 23:06
  • \$\begingroup\$ @pipe BSDs ship jot instead, neither is standardized by POSIX \$\endgroup\$
    – noname
    Commented Oct 25, 2023 at 5:19
  • \$\begingroup\$ OpenBSD and supposedly NetBSD have a seq command also, and it supports the s option. \$\endgroup\$
    – Oskar Skog
    Commented Oct 25, 2023 at 8:08
2
\$\begingroup\$

Python 3, 30 bytes by Karl Knechtel

print(*range(1,10000),sep=',')

Try it online!

Python 3, 36 bytes by onetoinfinity

print(str(list(range(10000)))[4:-1])

Try it online!

Python 3, 39 bytes by UndoneStudios

i=0
while i<1e5:print(end='%d,'%i);i+=1

Try it online!

Python 3, 40 bytes by Lemon

print(",".join(map(str,range(1,10000))))

Try it online!

\$\endgroup\$
7
  • \$\begingroup\$ Feel free to make any suggestions :) \$\endgroup\$
    – Lemon
    Commented Oct 26, 2023 at 3:53
  • \$\begingroup\$ range end is exclusive so you need 10000 or it will only print up to 9998 \$\endgroup\$
    – rbntd
    Commented Oct 26, 2023 at 7:41
  • \$\begingroup\$ Whops, I forgot about that one. Thanks :) \$\endgroup\$
    – Lemon
    Commented Oct 26, 2023 at 9:39
  • \$\begingroup\$ -1 byte: tio.run/##K6gsycjPM/7/… \$\endgroup\$ Commented Oct 26, 2023 at 14:02
  • 1
    \$\begingroup\$ Welcome to Code Golf! \$\endgroup\$ Commented Oct 26, 2023 at 14:13
1
\$\begingroup\$

C# 71 bytes

Console.WriteLine(string.Join(",",Enumerable.Range(1, 999).ToList()));

Saved 14 bytes thanks to Jupotters comment

Console.Write(string.Join(",",Enumerable.Range(1,9999)));
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 57: Console.Write(string.Join(",",Enumerable.Range(1,9999))); \$\endgroup\$
    – Jupotter
    Commented Oct 25, 2023 at 14:11
1
\$\begingroup\$

PowerShell, 15 bytes

1..9999-join","

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Haskell, 36 35 bytes

main=putStr.init.tail$show[1..9999]

Try it online!

Old version

main=putStr.init.tail.show$[1..9999]

Try it online!

\$\endgroup\$
-2
\$\begingroup\$

Wolfram Language (Mathematica), 27 bytes

Range@9999~StringRiffle~","

Try it online!

Range@9999 (10 bytes) almost does it...

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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