Multiplicity Test

21

3

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)

Monolica

Posted 2018-11-10T14:27:40.273

Reputation: 1 101

6Related – ETHproductions – 2018-11-10T14:48:53.530

3Can we output each entry on a new line, or must the output be all on one line? – ETHproductions – 2018-11-10T14:56:44.727

4Can the output end with a space. A few answers seem to assume so. – Dennis – 2018-11-11T16:14:14.143

Answers

7

05AB1E, 23 bytes

-1 byte thanks to Kevin Cruijssen

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

Try it online!

Okx

Posted 2018-11-10T14:27:40.273

Reputation: 15 025

1-1 byte by changing }?ð? to ]ðý (close both the if and loop, and join the entire stack by spaces) – Kevin Cruijssen – 2018-11-11T21:56:51.687

@KevinCruijssen Thanks, that was exactly what I was looking for! – Okx – 2018-11-12T08:36:46.113

-1 by using Å€ – Grimmy – 2019-11-20T14:03:46.183

6

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!

ovs

Posted 2018-11-10T14:27:40.273

Reputation: 21 408

1I was right :) +1 – ElPedro – 2018-11-10T16:42:15.303

5

R, 61 bytes

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

Try it online!

J.Doe

Posted 2018-11-10T14:27:40.273

Reputation: 2 379

2brilliant aliasing! – Giuseppe – 2018-11-12T20:08:50.137

How does this even work? That's amazing! +1 to you my friend – Sumner18 – 2018-12-20T20:29:22.100

4

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.

Dennis

Posted 2018-11-10T14:27:40.273

Reputation: 196 637

3

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?" ":"");}

HatsuPointerKun

Posted 2018-11-10T14:27:40.273

Reputation: 1 891

Does MSVC let you do the inf f() thing? Sorry 'bout deleting my comments, thought I had something shorter (I didn't) – Zacharý – 2018-11-10T19:18:59.887

@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 ) – HatsuPointerKun – 2018-11-10T19:22:14.963

I know, I thought that I had something better than what I suggested in the comments. – Zacharý – 2018-11-10T19:26:03.830

1Dang, dotally forgot printf wasa thing. Couldn't you use the C stdio then? – Zacharý – 2018-11-10T19:50:49.473

2Another 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;) – mriklojn – 2018-11-10T20:22:43.210

@mriklojn Good tricks ( with i++<99 ), but not declaring types makes it invalid in C++ ( at least not with MSVC ) – HatsuPointerKun – 2018-11-10T20:55:13.123

Yeah, not declaring types always makes it invalid in C++. – Zacharý – 2018-11-10T21:02:23.563

-1 C++ optimization: #include<cstdio> (I think that works, not sure about std and the like) – Zacharý – 2018-11-10T21:03:15.420

@Logern No, i get a "undefined reference to printf" linker error – HatsuPointerKun – 2018-11-10T21:06:47.823

1Suggest ")\0"+i%3 instead of i%3?"":")". Also, I think you need to add i=0 at the beginning of the loop. – ceilingcat – 2018-12-31T06:19:20.663

@ceilingcat i=0 is not necessary as long as the program is executed only once. the i; declares a global variable (“static storage duration” in C standard parlance) which is initialized to 0 by default. https://port70.net/~nsz/c/c11/n1570.html#6.7.9p10

– Pascal Cuoq – 2019-03-01T09:19:21.160

3

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.

Zacharý

Posted 2018-11-10T14:27:40.273

Reputation: 5 710

3

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

Neil

Posted 2018-11-10T14:27:40.273

Reputation: 95 035

3

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!

FrownyFrog

Posted 2018-11-10T14:27:40.273

Reputation: 3 112

Thanks for doing this one. Also, why do you have to do stdout here... I've never seen that before. @FrownyFrog – Jonah – 2018-11-11T05:58:12.577

@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) – FrownyFrog – 2018-11-11T06:09:51.850

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! – Jonah – 2018-11-11T06:10:19.043

one more question: any reason you used LF instead of _. the latter seems to work too. – Jonah – 2018-11-11T06:34:37.743

3

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)

Kevin Cruijssen

Posted 2018-11-10T14:27:40.273

Reputation: 67 575

@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. – Kevin Cruijssen – 2018-11-13T07:57:12.483

Well, it's more that I've traded the explicit output each iteration for implicit output at the end of the program instead – Jo King – 2018-11-13T07:59:16.880

@JoKing Yeah, but I didn't knew it would output the entire stack joined together, instead of just the top. :) – Kevin Cruijssen – 2018-11-13T08:01:58.953

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! – maxb – 2018-11-22T12:05:41.510

3

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)

mazzy

Posted 2018-11-10T14:27:40.273

Reputation: 4 832

2

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.

Laikoni

Posted 2018-11-10T14:27:40.273

Reputation: 23 676

2

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!

ElPedro

Posted 2018-11-10T14:27:40.273

Reputation: 5 301

2

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!

Galen Ivanov

Posted 2018-11-10T14:27:40.273

Reputation: 13 815

2

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();

HatsuPointerKun

Posted 2018-11-10T14:27:40.273

Reputation: 1 891

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? – Zacharý – 2018-11-10T22:39:59.030

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). – Zacharý – 2018-11-10T22:41:26.600

@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 ? – HatsuPointerKun – 2018-11-10T22:49:04.547

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. – Zacharý – 2018-11-10T23:22:35.917

@Zacharý Yes, under one condition, that t is declared as Action<int>, and not Action<Whatever_other_type_but_int>. My answer works whatever generic specialization – HatsuPointerKun – 2018-11-11T01:10:32.353

1All five ==0 can be <1. – Kevin Cruijssen – 2018-11-11T22:00:14.253

Ah, the <int> ... okay, that makes more sense. And nice one @KevinCruijssen, totally forgot we weren't dealing with negative integers. – Zacharý – 2018-11-11T22:35:57.940

Does (i%4<1?"[":"")+i+(i%4<1?"]":"") => (i%4<1?"["+i+"]":""+i) work? – Zacharý – 2018-11-12T15:21:18.323

Don't you either have to include using System; or qualify it like System.Console.Write? – LiefdeWen – 2018-11-12T15:32:16.053

(That System. adds to the byte count, by the way) – Zacharý – 2018-11-12T16:30:02.297

@Zacharý I forgot to complete the edit before saving x) – HatsuPointerKun – 2018-11-12T16:32:28.840

2

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)

David Wheatley

Posted 2018-11-10T14:27:40.273

Reputation: 123

2

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!

Dennis

Posted 2018-11-10T14:27:40.273

Reputation: 196 637

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}? – RosLuP – 2018-11-12T08:59:17.203

It works with gcc, which is good enough, as PPCG defines languages by their implementations. – Dennis – 2018-11-12T12:59:54.890

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) – RosLuP – 2018-11-12T16:51:26.390

@RosLuP gcc does work the same way on most computers though, at least on anything with the same architecture – ASCII-only – 2018-11-13T06:29:20.710

@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... – RosLuP – 2018-11-13T08:10:48.113

@RosLuP makes sense, but pretty sure they're normally compiled without flags unless otherwise specified – ASCII-only – 2018-11-14T02:21:48.307

@RosLuP For code golfing purposes, gcc with flags is a different language.

– Dennis – 2018-11-14T02:24:14.470

This program absolutely does not “work with GCC”. It happens to fly only tiny demons out of the author's nose when he tries it out on his machine so that he does not notice. GCC has never guaranteed that "("+2 is in a valid page. The language description should be “GCC on my machine”. – Pascal Cuoq – 2019-03-01T08:31:14.777

2

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.

Gabriel Mills

Posted 2018-11-10T14:27:40.273

Reputation: 778

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

– Veskah – 2018-11-12T21:43:34.830

67 bytes if you use list indexing as well. – Veskah – 2018-11-12T21:53:15.980

also 67 – ASCII-only – 2019-02-22T13:30:32.860

63? – ASCII-only – 2019-02-22T13:34:13.447

62? – ASCII-only – 2019-02-22T13:43:41.917

2

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.

AdmBorkBork

Posted 2018-11-10T14:27:40.273

Reputation: 41 581

2

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.

Will Cross

Posted 2018-11-10T14:27:40.273

Reputation: 19

Hello and welcome to PPCG! 70 bytes.

– Jonathan Frech – 2019-02-23T22:21:05.177

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

– Conor O'Brien – 2019-02-24T04:41:26.943

1

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

l4m2

Posted 2018-11-10T14:27:40.273

Reputation: 5 985

You could shorten significantly by doing ${n%4?n:`[${n}]`} – ETHproductions – 2018-11-10T15:12:52.713

66 bytes (with ETH's suggestion) – Alion – 2018-11-10T15:15:45.950

59 bytes – Arnauld – 2018-11-10T18:15:24.363

57 bytes – Neil – 2018-11-10T19:47:59.937

1

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.

user73921

Posted 2018-11-10T14:27:40.273

Reputation:

1Are you the Abigail? Inventor of /^1$|^(11+?)\1+$/? – msh210 – 2018-11-11T22:54:48.203

1Wow. What an honor to have you here! – msh210 – 2018-11-12T12:56:08.743

1

Perl 6, 51 48 bytes

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

Try it online!

nwellnhof

Posted 2018-11-10T14:27:40.273

Reputation: 10 037

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

– Jo King – 2018-11-11T01:27:32.640

@JoKing I thought about that, too, but I only came up with this 51-byter.

– nwellnhof – 2018-11-11T09:10:42.520

1

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.

Neil

Posted 2018-11-10T14:27:40.273

Reputation: 95 035

1

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

th3pirat3

Posted 2018-11-10T14:27:40.273

Reputation: 271

1

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!

Οurous

Posted 2018-11-10T14:27:40.273

Reputation: 7 916

1

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!

Οurous

Posted 2018-11-10T14:27:40.273

Reputation: 7 916

1

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.

NotBaal

Posted 2018-11-10T14:27:40.273

Reputation: 131

Save a byte by moving the space to the beginning of each loop iteration (i>1:" ":"") – dana – 2018-11-11T06:35:18.483

That would only work if I printed the result in reverse (see this) but would actually save 2 bytes instead of 1.

– NotBaal – 2018-11-11T16:23:08.680

Unfortunately that's not the same as the expected output as per the question, but thank you for the suggestion nevertheless! – NotBaal – 2018-11-11T16:24:09.180

1The "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);} ? – dana – 2018-11-11T16:37:59.480

1Ohhhh you're right that does work! Thanks for that! – NotBaal – 2018-11-11T16:55:13.397

1

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!

Jonah

Posted 2018-11-10T14:27:40.273

Reputation: 8 729

1

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.

Titus

Posted 2018-11-10T14:27:40.273

Reputation: 13 814

1

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.

Lynn

Posted 2018-11-10T14:27:40.273

Reputation: 55 648

1

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 

RosLuP

Posted 2018-11-10T14:27:40.273

Reputation: 3 036

1

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!

Vedant Kandoi

Posted 2018-11-10T14:27:40.273

Reputation: 1 955

1

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

Sok

Posted 2018-11-10T14:27:40.273

Reputation: 5 592

1

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!

glietz

Posted 2018-11-10T14:27:40.273

Reputation: 101

1

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.

Triggernometry

Posted 2018-11-10T14:27:40.273

Reputation: 765

1

Stax, 19 17 bytes

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

Run and debug it

recursive

Posted 2018-11-10T14:27:40.273

Reputation: 8 616

1

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!

gastropner

Posted 2018-11-10T14:27:40.273

Reputation: 3 264

1

Perl 5, 51 bytes

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

Try it online!

Xcali

Posted 2018-11-10T14:27:40.273

Reputation: 7 671

1

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!

TheGreatGeek

Posted 2018-11-10T14:27:40.273

Reputation: 111

1

CJam, 70 bytes

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

Try it out online! (Permalink)


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.

Helen

Posted 2018-11-10T14:27:40.273

Reputation: 125

1

C# (Visual C# Interactive Compiler), 71 bytes

int i;for(;i<99;)Write("{0:;;(}{1:;;[}{2}{1:;;]}{0:;;)} ",++i%3,i%4,i);

Try it online!

-9 bytes thanks to @ASCIIOnly!

Leveraging the semicolon (The ";" section separator) to create a custom format string.

dana

Posted 2018-11-10T14:27:40.273

Reputation: 2 541

:( close, also I think it'd be fine just adding a space after every term like most answers are doing – ASCII-only – 2019-02-22T14:04:49.257

Good call on adding a space after every number. Sometimes I take things too literally :) I'll update later. – dana – 2019-02-22T14:10:59.777

1

Japt, 30 bytes

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

Try it online!

Oliver

Posted 2018-11-10T14:27:40.273

Reputation: 7 160

1

PHP, 59 bytes

for(;$i<100;$s=++$i%4?$i:"[$i]")echo$i%3|!$s?$s:"($s)"," ";

Try it online!

Oliver

Posted 2018-11-10T14:27:40.273

Reputation: 7 160

1

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

Coert Grobbelaar

Posted 2018-11-10T14:27:40.273

Reputation: 131

1

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

– Conor O'Brien – 2019-02-22T17:20:01.917

Some good tips, thanks @ConorO'Brien! Will add the improvements – Coert Grobbelaar – 2019-02-23T21:00:39.067

Damn, both .join\ `` and adding ,n=0 to the map parameters are tricks I've never seen. Love learning new things :-) – Coert Grobbelaar – 2019-02-23T21:24:49.387

Anytime! Glad I could help! :D – Conor O'Brien – 2019-02-24T04:38:29.153

1

Attache, 56 bytes

Echo@@({~Join^^S@_'Mask[4'3|_,["["'"]","("'")"]]}=>1:99)

Try it online!

Explanation

Echo@@({~Join^^S@_'Mask[4'3|_,["["'"]","("'")"]]}=>1:99)
       {                                        }=>1:99  over each number _, 1 to 99:
                        4'3|_                                check if 4 or 3 divide _
                   Mask[     ,["["'"]","("'")"]]             keep the respective brackets
             ^^S@_'                                          fold over this list (seed = S[_]):
        ~Join                                                joining elements by the iterator
Echo@@(                                                ) print strings separated by spaces

Alternatives

58 bytes: Echo@@({~Join^^S@_'Mask[[4,3]|_,["["'"]","("'")"]]}=>1:99)

59 bytes: Print@@({~Join^^S@_'Mask[[4,3]|_,["["'"]","("'")"]]}=>1:99)

76 bytes: Print@@(&/S@{Mask[1'3'4'12|_,Split@$"${_} (${_}) [${_}] ([${_}])"] }=>1:99)

Conor O'Brien

Posted 2018-11-10T14:27:40.273

Reputation: 36 228

0

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.

Sara J

Posted 2018-11-10T14:27:40.273

Reputation: 2 576