# Multiplicity Test

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

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)

• Related Nov 10, 2018 at 14:48
• Can we output each entry on a new line, or must the output be all on one line? Nov 10, 2018 at 14:56
• Can the output end with a space. A few answers seem to assume so. Nov 11, 2018 at 16:14

# Python 2, 68 bytes

i=0
exec"i+=1;t,f=i%3<1,i%4<1;print'('*t+'['*f+i+']'*f+')'*t,;"*99


Try it online!

# Pyth, 25 bytes

jdmj]W!%d4dc2*"()"!%d3S99


Try it online here.

jdmj]W!%d4dc2*"()"!%d3S99
m                   S99   Map [1-99], as d, using:
%d3        d mod 3
!           Logical not, effectively maps 0 to 1, all else to 0
*"()"            Repeat "()" the above number of times
c2                 Chop into two equal parts - maps "()" to ["(",")"] , and "" to ["",""]
The above is result {1}
%d4                    d mod 4
!                       Logical not the above
]W    d                   If the above is truthy, [d], else d
The above is result {2}
j                          Join {1} on string representation of {2}
jd                          Join the result of the map on spaces, implicit print


# Python 3, 95 bytes

print(*[(i if i%4else f"[{i}]")if i%3else(f"({i})"if i%4else f"([{i}])")for i in range(1,100)])


Try it online!

# APL(NARS), 46 chars, 92 bytes

{{0=3∣w:1⌽')(',⍵⋄⍵}{0=4∣w:1⌽'][',⍵⋄⍵}⍕w←⍵}¨⍳99


litte test:

  {{0=3∣w:1⌽')(',⍵⋄⍵}{0=4∣w:1⌽'][',⍵⋄⍵}⍕w←⍵}¨⍳14
1 2 (3) [4] 5 (6) 7 [8] (9) 10 11 ([12]) 13 14


# Python 2, 68 bytes

for i in range(1,100):print("(%s)","%s")[i%3>0]%("[%s]"%i,i)[i%4>0],


Try it online!

Ends with a trailing space.

# Stax, 19 17 bytes

ä┐r►U^îⁿ_╤▌,│☻£╡▬


Run and debug it

# C (gcc), 76 bytes

Surprisingly, this straight-forward solution was shorter than the "clever" ones I could come up with.

f(i){for(i=0;i++<99;)printf(i%3?i%4?"%d ":"[%d] ":i%4?"(%d) ":"([%d]) ",i);}


Try it online!

# Perl 5, 51 bytes

say map{$o=$_%4?$_:"[$_]";($_%3?$o:"($o)").$"}1..99


Try it online!

# Clojure, 104 bytes

(apply print(map #(do(def s(if(=(rem % 4)0)(str"["%"]")%))(if(=(rem % 3)0)(str"("s")")s))(range 1 100)))


Try it online!

# CJam, 70 bytes

99,:){X4%0={'[Xs']++}{X}?}fX]{Xs'[/_,(=']/0=i3%0={'(Xs')++}{X}?}fX]S*


### Explanation

99                                                                    Push 99 to stack
,                                                                   Make a 0..98 array
:)                                                                 Increment every value in array
{                   }fX                                          For X in array at top of stack
?                                             If...
X4%0=                                                           Condition: X mod 4 = 0
{        }                                                 Is true, then:
'[                                                        Push "["
Xs                                                      Push X as a string
']                                                    Push "]"
++                                                  Concatenate to "[X]"
{ }                                              Is false, then:
X                                               Push X
]                                         Capture all in an array
{                                 }fX    For X in array at top of stack
?       If...
Xs                                      X as a string
'[/                                   Split by "["
_,(=                               Take last element after split
']/                            Split by "]"
0=                          Take first element after split
i                         Top element as an integer
Xs'[/_,(=']/0=i                         The number X minus any brackets
3%0=                     Condition: X mod 3 = 0
{        }           Is true, then:
'(                  Push "("
Xs                Push the original X as a string
')              Push ")"
++            Concatenate to "(X)"
{ }        If false, then:
X         Push X
]   Capture all in an array
S* Separate by a space


This does not output a trailing space.

# Japt, 30 bytes

Lo1@=X%4?X:"[{X}]"X%3?U:"({U})


Try it online!

# PHP, 59 bytes

## Javascript, 114 96 bytes

[...Array(99)].map(_=>['[4]','(3)'].reduce((a,r)=>n%r[1]<1?r.replace(r[1],a):a,++n),n=0).join


Try it Online!

Thanks to Conor O'Brien for helping me improve it :-)

• If you haven't heard it yet, welcome to PPCG! You can squeeze out a few more bytes: n%r[1]==0 can become n%r[1]<1 (-1 bytes); .join(' ') to .join   (-2 bytes); you can use [...Array(99)].map(_=>...,n=0) to make the iteration section a bit more compact. You can overall golf around 20 bytes from this answer. Try it online!. Feb 22, 2019 at 17:20
• Some good tips, thanks @ConorO'Brien! Will add the improvements Feb 23, 2019 at 21:00
• Damn, both .join  and adding ,n=0 to the map parameters are tricks I've never seen. Love learning new things :-) Feb 23, 2019 at 21:24
• Anytime! Glad I could help! :D Feb 24, 2019 at 4:38

# ink, 61 bytes

VAR j=0
-(i)~j="{i%4:{i}|[{i}]}"
{i%3:{j}|({j})} <>{i<99:->i}


Try it online!

### Explanation

VAR j=0                       // Declare a variable called j.
-(i)~j="{i%4:{i}|[{i}]}"      // The i label keeps track of how many times it's been visited. Set j to that value, or that value surrounded by brackets, depending o nwhat it is.
{i%3:{j}|({j})} <>{i<99:->i}  // Print j or (j), depending on i. Use glue (<>) to not print a newline. Go back to the i label if it's been visited fewer than 99 times.