22
\$\begingroup\$

Use any programming language to display numbers between 1 and 99 (including both) in such a way, so that:

  • the numbers are separated by single space,
  • if a number is divisible by 3, it should be in parentheses,
  • if a number is divisible by 4, it should be in square brackets,
  • if a number is divisible by both 3 and 4, it should be in both parentheses and square brackets (with square brackets closer to the number).

Your program should display exactly:

1 2 (3) [4] 5 (6) 7 [8] (9) 10 11 ([12]) 13 14 (15) [16] 17 (18) 19 [20] (21) 22 23 ([24]) 25 26 (27) [28] 29 (30) 31 [32] (33) 34 35 ([36]) 37 38 (39) [40] 41 (42) 43 [44] (45) 46 47 ([48]) 49 50 (51) [52] 53 (54) 55 [56] (57) 58 59 ([60]) 61 62 (63) [64] 65 (66) 67 [68] (69) 70 71 ([72]) 73 74 (75) [76] 77 (78) 79 [80] (81) 82 83 ([84]) 85 86 (87) [88] 89 (90) 91 [92] (93) 94 95 ([96]) 97 98 (99)
\$\endgroup\$
3
  • 7
    \$\begingroup\$ Related \$\endgroup\$ Nov 10, 2018 at 14:48
  • 3
    \$\begingroup\$ Can we output each entry on a new line, or must the output be all on one line? \$\endgroup\$ Nov 10, 2018 at 14:56
  • 4
    \$\begingroup\$ Can the output end with a space. A few answers seem to assume so. \$\endgroup\$
    – Dennis
    Nov 11, 2018 at 16:14

48 Answers 48

7
\$\begingroup\$

05AB1E, 23 bytes

-1 byte thanks to Kevin Cruijssen

тGND4Öi…[ÿ]}N3Öi…(ÿ)]ðý

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ -1 byte by changing }?ð? to ]ðý (close both the if and loop, and join the entire stack by spaces) \$\endgroup\$ Nov 11, 2018 at 21:56
  • \$\begingroup\$ @KevinCruijssen Thanks, that was exactly what I was looking for! \$\endgroup\$
    – Okx
    Nov 12, 2018 at 8:36
  • \$\begingroup\$ -1 by using Å€ \$\endgroup\$
    – Grimmy
    Nov 20, 2019 at 14:03
6
\$\begingroup\$

Python 2, 68 65 60 bytes

i=0
exec"i+=1;print'('[i%3:]+`[i][i%4:]or i`+')'[i%3:],;"*99

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I was right :) +1 \$\endgroup\$
    – ElPedro
    Nov 10, 2018 at 16:42
5
\$\begingroup\$

R, 61 bytes

"+"=c
r=""+""
cat(paste0(r+"(",r+""+"[",1:99,r+""+"]",r+")"))

Try it online!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ brilliant aliasing! \$\endgroup\$
    – Giuseppe
    Nov 12, 2018 at 20:08
  • \$\begingroup\$ How does this even work? That's amazing! +1 to you my friend \$\endgroup\$
    – Sumner18
    Dec 20, 2018 at 20:29
4
\$\begingroup\$

Jelly, 21 20 bytes

³Ṗµ3,4ṚƬḍד([“])”j)K

Try it online!

How it works

³Ṗµ3,4ṚƬḍד([“])”j)K  Main link. No arguments.

³                     Set the return value to 100.
 Ṗ                    Pop; yield [1, ..., 99].
  µ               )   Map the chain in between over [1, ..., 9]; for each integer k:
   3,4                    Set the return value to [3, 4].
      ṚƬ                  Reverse until a loop is reached. Yields [[3, 4], [4, 3]].
        ḍ                 Test k for divisibility by [[3, 4], [4, 3]], yielding a
                          matrix of Booleans.
         ד([“])”         Repeat the characters of [['(', '['], [']', ')']] as many
                          times as the Booleans indicate.
                 j        Join the resulting pair of strings, separated by k.
                   K  Join the resulting array of strings, separated by spaces.
\$\endgroup\$
3
\$\begingroup\$

D, 110 bytes

import std.stdio;void f(){for(int i;i<99;)write(++i%3?"":"(",i%4?"":"[",i,i%4?"":"]",i%3?"":")",i%99?" ":"");}

Try it online!

Ported from @HatsuPointerKun's C++ answer.

\$\endgroup\$
3
\$\begingroup\$

Charcoal, 30 bytes

⪫EEE⁹⁹I⊕ι⎇﹪⊕κ⁴ι⪫[]ι⎇﹪⊕κ³ι⪫()ι 

Try it online! Link is to verbose version of code. Explanation:

    ⁹⁹                          Literal 99
   E                            Map over implicit range
        ι                       Current value
       ⊕                        Incrementd
      I                         Cast to string
  E                             Map over list of strings
            κ                   Current index
           ⊕                    Incremented
             ⁴                  Literal 4
          ﹪                     Modulo
              ι                 Current value
                []              Literal string `[]`
                  ι             Current value
               ⪫                Join i.e wrap value in `[]`
         ⎇                      Ternary
 E                              Map over list of strings
                      κ         Current index
                     ⊕          Incremented
                       ³        Literal 3
                    ﹪           Modulo
                        ι       Current value
                          ()    Literal string `()`
                            ι   Current value
                         ⪫      Join i.e wrap value in `()`
                   ⎇            Ternary
                                Literal space
⪫                               Join list
                                Implicitly print
\$\endgroup\$
3
\$\begingroup\$

J, 54 53 bytes

1 byte less thanks to @Jonah

(*stdout(3|.0=4 3 1 3 4&|,1=":)#3|.']) ([',":)@>i.100

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Thanks for doing this one. Also, why do you have to do stdout here... I've never seen that before. @FrownyFrog \$\endgroup\$
    – Jonah
    Nov 11, 2018 at 5:58
  • \$\begingroup\$ @Jonah I can’t output it as a complete string, it gets cut off (. . .) stdout doesn’t do that, and it doesn’t print a newline either, so I can also print each number separately. For some reason though it makes trailing spaces appear (there are 4, and only 1 is intentionally there) \$\endgroup\$
    – FrownyFrog
    Nov 11, 2018 at 6:09
  • \$\begingroup\$ This approach is really clever, both the rotation and the choice to use #. I had introduced an auxiliary verb to surround with () and []: g=. {.@[ , ":@] , {:@[. ugh the verboseness! \$\endgroup\$
    – Jonah
    Nov 11, 2018 at 6:10
  • \$\begingroup\$ one more question: any reason you used LF instead of _. the latter seems to work too. \$\endgroup\$
    – Jonah
    Nov 11, 2018 at 6:34
3
\$\begingroup\$

C, C++, 136 133 131 129 128 124 bytes

-5 bytes thanks to Zacharý and inspired by write() function in D language ( see Zacharý answer )

-2 bytes thanks to mriklojn

-12 bytes for the C version thanks to mriklojn

-4 bytes thanks to ceilingcat

#include<cstdio>
void f(){for(int i=0;i++<99;)printf("(%s%d%s%s%s"+!!(i%3),i%4?"":"[",i,i%4?"":"]",i%3?"":")",i%99?" ":"");}

C Specific Optimization : 115 bytes

#include<stdio.h>
i;f(){for(;i++<99;)printf("(%s%d%s%s%s"+!!(i%3),i%4?"":"[",i,i%4?"":"]",i%3?"":")",i%99?" ":"");}
\$\endgroup\$
11
  • \$\begingroup\$ Does MSVC let you do the inf f() thing? Sorry 'bout deleting my comments, thought I had something shorter (I didn't) \$\endgroup\$
    – Adalynn
    Nov 10, 2018 at 19:18
  • \$\begingroup\$ @Zacharý No, i think the function is too simple and it generates a "f must return an int". BTW, your solution was 3 bytes shorter ( include compression paired with moving the incrementation of i ) \$\endgroup\$ Nov 10, 2018 at 19:22
  • 1
    \$\begingroup\$ Dang, dotally forgot printf wasa thing. Couldn't you use the C stdio then? \$\endgroup\$
    – Adalynn
    Nov 10, 2018 at 19:50
  • 2
    \$\begingroup\$ Another thing you could use/exploit is the fact that, at least with gcc 5.3.1, you don't need the #include, and you can also remove the function return type. Additionally, if you declare the int i outside the function (in global scope) then its value defaults to 0 and data type defaults to int. This would result in your loop starting at 0, and to fix this you could move the increment into the condition expression in your for loop, making it look like i;f(){for(;++i<=99;) \$\endgroup\$
    – mriklojn
    Nov 10, 2018 at 20:22
  • 1
    \$\begingroup\$ Suggest ")\0"+i%3 instead of i%3?"":")". Also, I think you need to add i=0 at the beginning of the loop. \$\endgroup\$
    – ceilingcat
    Dec 31, 2018 at 6:19
3
\$\begingroup\$

Powershell, 60 bytes

"$(1..99|%{($_,"($_)","[$_]","([$_])")[!($_%3)+2*!($_%4)]})"

Explanation:

  • the array with 4 elements: $_, "($_)", "[$_]", "([$_])"
  • and the index: [!($_%3)+2*!($_%4)]
  • repeat for each number
  • convert the result to a string

Less golfed Test script:

$f = {

$r = 1..99|%{
    ($_, "($_)", "[$_]", "([$_])") [!($_%3)+2*!($_%4)]
}
"$($r)"

}

$expected = '1 2 (3) [4] 5 (6) 7 [8] (9) 10 11 ([12]) 13 14 (15) [16] 17 (18) 19 [20] (21) 22 23 ([24]) 25 26 (27) [28] 29 (30) 31 [32] (33) 34 35 ([36]) 37 38 (39) [40] 41 (42) 43 [44] (45) 46 47 ([48]) 49 50 (51) [52] 53 (54) 55 [56] (57) 58 59 ([60]) 61 62 (63) [64] 65 (66) 67 [68] (69) 70 71 ([72]) 73 74 (75) [76] 77 (78) 79 [80] (81) 82 83 ([84]) 85 86 (87) [88] 89 (90) 91 [92] (93) 94 95 ([96]) 97 98 (99)'
$result = &$f
$result-eq$expected
$result

Output:

True
1 2 (3) [4] 5 (6) 7 [8] (9) 10 11 ([12]) 13 14 (15) [16] 17 (18) 19 [20] (21) 22 23 ([24]) 25 26 (27) [28] 29 (30) 31 [32] (33) 34 35 ([36]) 37 38 (39) [40] 41 (42) 43 [44] (45) 46 47 ([48]) 49 50 (51) [52] 53 (54) 55 [56] (57) 58 59 ([60]) 61 62 (63) [64] 65 (66) 67 [68] (69) 70 71 ([72]) 73 74 (75) [76] 77 (78) 79 [80] (81) 82 83 ([84]) 85 86 (87) [88] 89 (90) 91 [92] (93) 94 95 ([96]) 97 98 (99)
\$\endgroup\$
3
\$\begingroup\$

MathGolf, 41 40 34 29 bytes

♀({îû)(î+╫îa_'(\')ßyΓî34α÷ä§ 

NOTE: It has a trailing space

Only my second MathGolf answer..
-5 bytes thanks to @JoKing.

Try it online.

Explanation:

♀(             # Push 99 (100 decreased by 1)
  {            # Start a loop, which implicitly loops down to (and excluding) 0
   û)(         #  Push string ")("
      î+       #  Append the 1-indexed index
        ╫      #  Rotate the string once towards the right
   îa          #  Push the 1-indexed index of the loop, wrap in a list
   _           #  Duplicate it
    '(        '#  Push string "("
      \        #  Swap the top two items of the stack
       ')     '#  Push string ")"
         ßy    #  Wrap all three into a list, and join them
   Γ           #  Wrap all four into a list
               #  (We now have a list [N, "(N)", "[N]", "([N])"], where N is the index)
   î           #  Push the 1-indexed index of the loop
    34         #  Push 3 and 4 to the stack
      α        #  Wrap all three into a list
       ÷       #  Check for each if the index is divisible by it
               #  (resulting in either [0,0], [0,1], [1,0], or [1,1]
        ä      #  Convert from binary to integer
               #  (resulting in either 0, 1, 2, or 3
         §     #  Push the string at that index from the array
               #  Push a space
               # (After the loop, output the entire stack joined together implicitly)
\$\endgroup\$
4
  • \$\begingroup\$ @JoKing Thanks! Didn't knew the q could be omitted and it's done implicitly in loops. Also, didn't knew there was a 2/3/4-string builtin. Too bad that rotate trick doesn't work with the wrapped array. \$\endgroup\$ Nov 13, 2018 at 7:57
  • \$\begingroup\$ Well, it's more that I've traded the explicit output each iteration for implicit output at the end of the program instead \$\endgroup\$
    – Jo King
    Nov 13, 2018 at 7:59
  • \$\begingroup\$ @JoKing Yeah, but I didn't knew it would output the entire stack joined together, instead of just the top. :) \$\endgroup\$ Nov 13, 2018 at 8:01
  • \$\begingroup\$ My solution was approaching 40 bytes, though I misread and thought that curly brackets should be used instead of square brackets. Good job on the solution! \$\endgroup\$
    – maxb
    Nov 22, 2018 at 12:05
2
\$\begingroup\$

Haskell, 77 bytes

unwords[f"()"3$f"[]"4$show n|n<-[1..99],let f(x:r)m s|mod n m<1=x:s++r|1<3=s]

Try it online!

I wonder if show[n] can be used to shorten things up, so far to no avail.

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

Lua, 161 123 bytes

b=""for i=1,99 do;c,d=i%3==0,i%4==0;b=b..(c and"("or"")..(d and"["or"")..i..(d and"]"or"")..(c and")"or"").." "end;print(b)

Try it online!

Ungolfed:

b = ""
for i = 1, 99 do
    c = 1 % 3 == 0
    d = 1 % 4 == 0
    a = ""
    if c then
        a = a .. "("
    end
    if d then
        a = a .. "["
    end
    a = a .. i
    if d then
        a = a .. "]"
    end
    if c then
        a = a .. ")"
    end
    b = b .. a .. " "
end
print(b)
\$\endgroup\$
2
\$\begingroup\$

Python 2, 105 97 88 86 85 84 bytes

x=1
while x<100:print((('%s','(%s)')[x%3<1],'[%s]')[x%4<1],'([%s])')[x%12<1]%x,;x+=1

Try it online!

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

C (gcc), 84 bytes

main(i){while(99/i++)printf("%s%s%d%s%s ","("+i%3,"["+i%4,i-1,"]"+i%4,")"+i%3);}

There's a null byte at the beginning of each "bracket string".

Try it online!

\$\endgroup\$
8
  • \$\begingroup\$ And in "("+i%3 how do you know that address for i=2 point to a zero char value? The same for "["+i%4 for i in {2,3}? \$\endgroup\$
    – user58988
    Nov 12, 2018 at 8:59
  • \$\begingroup\$ It works with gcc, which is good enough, as PPCG defines languages by their implementations. \$\endgroup\$
    – Dennis
    Nov 12, 2018 at 12:59
  • \$\begingroup\$ I think you can not say that code it is ok compiled in every implementation of gcc compiler, perhaps only the one run in your pc (but possible not too) \$\endgroup\$
    – user58988
    Nov 12, 2018 at 16:51
  • \$\begingroup\$ @RosLuP gcc does work the same way on most computers though, at least on anything with the same architecture \$\endgroup\$
    – ASCII-only
    Nov 13, 2018 at 6:29
  • \$\begingroup\$ @ASCII-only possible if is compiled optimized for space or for speed the result is different... I don't know if it is out the standard... \$\endgroup\$
    – user58988
    Nov 13, 2018 at 8:10
2
\$\begingroup\$

PowerShell, 67 62 bytes

"$(1..99|%{'('*!($x=$_%3)+'['*!($y=$_%4)+$_+']'*!$y+')'*!$x})"

Try it online!

Basically a FizzBuzz using string multiplication times Boolean variables (implicitly cast to 1 or 0). Those strings are left on the pipeline and gathered within a script block inside quotes. Since the default $OutputFieldSeparator for an array is spaces, this implicitly gives us space-delimited array elements.

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

C#, 124 117 123 bytes

-5 bytes thanks to Kevin Cruijssen

x=>{for(int i=0;i++<99;)System.Console.Write((i%3<1?"(":"")+(i%4<1?"[":"")+i+(i%4<1?"]":"")+(i%3<1?")":"")+(i>98?"":" "));}

Test with :

Action<int> t = x=>{for(int i=0;i++<99;)System.Console.Write((i%3<1?"(":"")+(i%4<1?"[":"")+i+(i%4<1?"]":"")+(i%3<1?")":"")+(i>98?"":" "));}
t.Invoke(0);
Console.ReadKey();
\$\endgroup\$
11
  • \$\begingroup\$ Into the foray of C#, I see. Does C# allow integers as the left argument to a ternary operator, or does it have to be a boolean? \$\endgroup\$
    – Adalynn
    Nov 10, 2018 at 22:39
  • \$\begingroup\$ I don't know much about C#, but could you use x instead of i,thus not having to worry about the int ? (You'd still have to set it, of course). \$\endgroup\$
    – Adalynn
    Nov 10, 2018 at 22:41
  • \$\begingroup\$ @Zacharý No, it generates a CS0029 error "Can't convert implicitly int to boolean". And yes, i could use i and the fact that i can initialize it at 0 when i Invoke. But wouldn't that mean that i would have to include the declaration of t ( Action<int> ) and the call ( t.Invoke(0) ) in the bytecount ? \$\endgroup\$ Nov 10, 2018 at 22:49
  • \$\begingroup\$ I'm asking if something like x=>{for(x=0;x++<99;)Console.Write((x%3==0?"(":"")+(x%4==0?"[":"")+x+(x%4==0?"]":"")+(x%3==0?")":"")+(x%99==0?"":" "));}; would work. \$\endgroup\$
    – Adalynn
    Nov 10, 2018 at 23:22
  • 1
    \$\begingroup\$ All five ==0 can be <1. \$\endgroup\$ Nov 11, 2018 at 22:00
2
\$\begingroup\$

Red, 99 97 bytes

repeat n 99[a: b: c: d:""if n% 3 = 0[a:"("c:")"]if n% 4 = 0[b:"["d:"]"]prin rejoin[a b n d c" "]]

Try it online!

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

Ruby, 72 66 bytes

p [*1..99].map{|x|a=x
a="[#{x}]"if x%4<1
a="(#{a})"if x%3<1
a}*' '

Thanks to @jonathan-frech and @conor-obrien for additional trimming.

\$\endgroup\$
2
  • \$\begingroup\$ Hello and welcome to PPCG! 70 bytes. \$\endgroup\$ Feb 23, 2019 at 22:21
  • \$\begingroup\$ Welcome to PPCG! Here's another 4 bytes off of @JonathanFrench 's suggestion, for 66 bytes, since a.join b for an array a and string b is equivalent to a*b \$\endgroup\$ Feb 24, 2019 at 4:41
2
\$\begingroup\$

PowerShell, 98 82 74 67 63 62 bytes

A whopping -31 bytes thanks to @Veskah -5 bytes thanks to @ASCII-only

(1..99|%{(($a=($_,"[$_]")[!($_%4)]),"($a)")[!($_%3)]})-join' '

Try it online!

I'm still not quite sure what I've done here.

\$\endgroup\$
5
  • \$\begingroup\$ Just some quick golfing for 70 bytes. You don't have to cast $a as a string and "$a" will still substitute in the value. (Note: Single-quotes do not replace $foo, only double-quotes). Another trick is ifs only care about 0 or 1 so you can use boolean logic to save a byte \$\endgroup\$
    – Veskah
    Nov 12, 2018 at 21:43
  • \$\begingroup\$ 67 bytes if you use list indexing as well. \$\endgroup\$
    – Veskah
    Nov 12, 2018 at 21:53
  • \$\begingroup\$ also 67 \$\endgroup\$
    – ASCII-only
    Feb 22, 2019 at 13:30
  • \$\begingroup\$ 63? \$\endgroup\$
    – ASCII-only
    Feb 22, 2019 at 13:34
  • \$\begingroup\$ 62? \$\endgroup\$
    – ASCII-only
    Feb 22, 2019 at 13:43
1
\$\begingroup\$

perl -E, 60 bytes

$,=$";say map$_%12?$_%3?$_%4?$_:"[$_]":"($_)":"([$_])",1..99

Some bytes can be saved if we can use newlines between the numbers: in that case, we can remove the $,=$";, change the map into a for loop, while moving the say into the loop.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Are you the Abigail? Inventor of /^1$|^(11+?)\1+$/? \$\endgroup\$
    – msh210
    Nov 11, 2018 at 22:54
  • 1
    \$\begingroup\$ Wow. What an honor to have you here! \$\endgroup\$
    – msh210
    Nov 12, 2018 at 12:56
1
\$\begingroup\$

Perl 6, 51 48 bytes

put {$_%3??$^a!!"($a)"}(++$_%4??$_!!"[$_]")xx 99

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ I was going to abuse the difference between lists and array representations, like this, but I'm not sure how to get rid of the enclosing brackets around the whole list... \$\endgroup\$
    – Jo King
    Nov 11, 2018 at 1:27
  • \$\begingroup\$ @JoKing I thought about that, too, but I only came up with this 51-byter. \$\endgroup\$
    – nwellnhof
    Nov 11, 2018 at 9:10
1
\$\begingroup\$

Batch, 145 bytes

@set s=
@for /l %%i in (1,1,99)do @set/an=%%i,b=n%%4,p=n%%3&call:c
@echo%s%
:c
@if %b%==0 set n=[%n%]
@if %p%==0 set n=(%n%)
@set s=%s% %n%

The code falls through into the subroutine but the string has already been printed by this point so the code executes harmlessly.

\$\endgroup\$
1
\$\begingroup\$

PHP 103

for(;$i<1e2;$i++)$a.=$i%12==0?"([$i]) ":($i%3==0?"($i) ":($i%4==0?"[$i] ":"$i "));echo substr($a,5,-1);

https://www.ideone.com/SBAuWp

\$\endgroup\$
1
\$\begingroup\$

Clean, 100 bytes

import StdEnv,Text

join" "[if(n/3*3<n)m("("+m+")")\\n<-[1..99],m<-[if(n/4*4<n)(""<+n)("["<+n<+"]")]]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

sfk, 225 bytes

for n from 1 to 99 +calc -var #(n)/3+1/3 -dig=0 +calc -var #text*3-#(n) +setvar t +calc -var #(n)/4 -dig=0 +calc -var #text*4-#(n) +xed -var _0_#(t)\[#(n)\]_ _*_#(t)#(n)_ +xed _0*_([part2])_ _?*_[part2]_ +xed "_\n_ _" +endfor

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Bash, 61 bytes

-14 bytes, thanks to Dennis

seq 99|awk '{ORS=" ";x=$1%4?$1:"["$1"]";print$1%3?x:"("x")"}'

explanation

Pretty straightforward:

  • seq produces 1..99
  • we pipe that into awk with the output record separator (ORS) set to space so the output is a single line.
  • the main awk body just adds "[]" when the number is divisible by 4, and then adds, on top of that, "()" when divisible by 3.

Try it online!

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

JavaScript (Node.js), 57 bytes

f=(n=99)=>n&&f(n-1)+(s=n%4?n:`[${n}]`,n%3?s:`(${s})`)+' '

Try it online!

Changed to community cuz the optimize rely too much on it

\$\endgroup\$
4
  • \$\begingroup\$ You could shorten significantly by doing ${n%4?n:`[${n}]`} \$\endgroup\$ Nov 10, 2018 at 15:12
  • \$\begingroup\$ 66 bytes (with ETH's suggestion) \$\endgroup\$
    – Alion
    Nov 10, 2018 at 15:15
  • \$\begingroup\$ 59 bytes \$\endgroup\$
    – Arnauld
    Nov 10, 2018 at 18:15
  • \$\begingroup\$ 57 bytes \$\endgroup\$
    – Neil
    Nov 10, 2018 at 19:47
1
\$\begingroup\$

PHP, 65 bytes

while($i++<99)echo$i%4?$i%3?$i:"($i)":($i%3?"[$i]":"([$i])")," ";

or

while($i++<99)echo"("[$i%3],"["[$i%4],$i,"]"[$i%4],")"[$i%3]," ";

(requires PHP 5.5 or later)

Run with -nr or try them online.

\$\endgroup\$
1
\$\begingroup\$

Python 2, 78 bytes

i=0
exec"i+=1;u=i%3/-2*(i%4/-3-1);print'([%0d])'[u:7-u:1+(i%3<1<=i%4)]%i,;"*99

Try it online!

I envisioned this cool approach slicing '([%0d])' but I can't get the expressions any shorter.

\$\endgroup\$
1
\$\begingroup\$

Java 8, 92 91 bytes

-1 byte thanks to @Dana

i->{for(;i++<99;)out.printf((i>1?" ":"")+(i%12<1?"([%d])":i%3<1?"(%d)":i%4<1?"[%d]":i),i);}

Try it online!

Alternative solution, 82 bytes (with trailing space in the output - not sure if that's allowed):

i->{for(;i++<99;)out.printf((i%12<1?"([%d])":i%3<1?"(%d)":i%4<1?"[%d]":i)+" ",i);}

Explanation:

for(;i++<99;) - a for loop that goes from the value of i (reused as input, taken to be 0 in this case) to 99

out.printf(<part1>+<part2>,i); - formats the string before immediately printing it to stdout with the value of i

where <part1> is (i>1?" ":"") - prints the space before printing the number unless that number is 1, in which case it omits the space

and <part2> is (i%12<1?"([%d])":i%3<1?"(%d)":i%4<1?"[%d]":i) - if i is divisible by both 3 and 4, i has both square and round brackets around it; else if i is divisible by 3, i has round brackets; else if i is divisible by 4, i has square brackets; else, i has no brackets.

\$\endgroup\$
5
  • \$\begingroup\$ Save a byte by moving the space to the beginning of each loop iteration (i>1:" ":"") \$\endgroup\$
    – dana
    Nov 11, 2018 at 6:35
  • \$\begingroup\$ That would only work if I printed the result in reverse (see this) but would actually save 2 bytes instead of 1. \$\endgroup\$
    – NotBaal
    Nov 11, 2018 at 16:23
  • \$\begingroup\$ Unfortunately that's not the same as the expected output as per the question, but thank you for the suggestion nevertheless! \$\endgroup\$
    – NotBaal
    Nov 11, 2018 at 16:24
  • 1
    \$\begingroup\$ The "try it online" links seem to be broken. I was thinking i->{for(;i++<99;)out.printf((i>1?" ":"")+(i%12<1?"([%d])":i%3<1?"(%d)":i%4<1?"[%d]":i),i);} ? \$\endgroup\$
    – dana
    Nov 11, 2018 at 16:37
  • 1
    \$\begingroup\$ Ohhhh you're right that does work! Thanks for that! \$\endgroup\$
    – NotBaal
    Nov 11, 2018 at 16:55

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.