Skip to main content
Commonmark migration
Source Link

#Pure bash, 43

Pure bash, 43

echo $[$1<2?1:$[$1<2?2:$1]#`printf 1%0$1d`]

Try it online.

Not sure if this is bending the rules too much - I'm not using any of the listed banned builtins, but I am using base conversion.

  • printf 1%0$1d outputs a 1 followed by n 0s
  • $[b#a] is an arithmetic expansion to treat a as a base b number, which gives the required result. Unfortunately base <2 does not work, so the extra ?: bits handle input n=1.

Maximum input is 15, because bash uses signed 64-bit integers (up to 231-1).

#Pure bash, 43

echo $[$1<2?1:$[$1<2?2:$1]#`printf 1%0$1d`]

Try it online.

Not sure if this is bending the rules too much - I'm not using any of the listed banned builtins, but I am using base conversion.

  • printf 1%0$1d outputs a 1 followed by n 0s
  • $[b#a] is an arithmetic expansion to treat a as a base b number, which gives the required result. Unfortunately base <2 does not work, so the extra ?: bits handle input n=1.

Maximum input is 15, because bash uses signed 64-bit integers (up to 231-1).

Pure bash, 43

echo $[$1<2?1:$[$1<2?2:$1]#`printf 1%0$1d`]

Try it online.

Not sure if this is bending the rules too much - I'm not using any of the listed banned builtins, but I am using base conversion.

  • printf 1%0$1d outputs a 1 followed by n 0s
  • $[b#a] is an arithmetic expansion to treat a as a base b number, which gives the required result. Unfortunately base <2 does not work, so the extra ?: bits handle input n=1.

Maximum input is 15, because bash uses signed 64-bit integers (up to 231-1).

added 112 characters in body
Source Link
Digital Trauma
  • 73k
  • 9
  • 112
  • 264

#Pure bash, 2643

echo $[$1#`printf$[$1<2?1:$[$1<2?2:$1]#`printf 1%0$1d`]

Try it onlineTry it online.

Not sure if this is bending the rules too much - I'm not using any of the listed banned builtins, but I am using base conversion.

  • printf 1%0$1d outputs a 1 followed by n 0s
  • $[b#a] is an arithmetic expansion to treat a as a base b number, which gives the required result. Unfortunately base <2 does not work, so the extra ?: bits handle input n=1.

Maximum input is 15, because bash uses signed 64-bit integers (up to 231-1).

#Pure bash, 26

echo $[$1#`printf 1%0$1d`]

Try it online.

Not sure if this is bending the rules too much - I'm not using any of the listed banned builtins, but I am using base conversion.

  • printf 1%0$1d outputs a 1 followed by n 0s
  • $[b#a] is an arithmetic expansion to treat a as a base b number, which gives the required result.

Maximum input is 15, because bash uses signed 64-bit integers (up to 231-1).

#Pure bash, 43

echo $[$1<2?1:$[$1<2?2:$1]#`printf 1%0$1d`]

Try it online.

Not sure if this is bending the rules too much - I'm not using any of the listed banned builtins, but I am using base conversion.

  • printf 1%0$1d outputs a 1 followed by n 0s
  • $[b#a] is an arithmetic expansion to treat a as a base b number, which gives the required result. Unfortunately base <2 does not work, so the extra ?: bits handle input n=1.

Maximum input is 15, because bash uses signed 64-bit integers (up to 231-1).

Source Link
Digital Trauma
  • 73k
  • 9
  • 112
  • 264

#Pure bash, 26

echo $[$1#`printf 1%0$1d`]

Try it online.

Not sure if this is bending the rules too much - I'm not using any of the listed banned builtins, but I am using base conversion.

  • printf 1%0$1d outputs a 1 followed by n 0s
  • $[b#a] is an arithmetic expansion to treat a as a base b number, which gives the required result.

Maximum input is 15, because bash uses signed 64-bit integers (up to 231-1).