[Question]
  [
You are to write a program which outputs a single positive integer \$n\$. The integer may have leading zeros and whitespace, and/or trailing whitespace.
Then, choose a positive integer \$m > 1\$. When the program is repeated exactly \$m\$ times, it should output \$m \times n\$ (and only \$m\times n\$). When repeated any number of times other than \$m\$, it should output \$n\$ (and only \$n\$).
You may choose the value of \$m\$, so long as it is a positive integer greater than 1.
In this context, "repeated \$m\$ times" means that the whole program is repeated \$m\$ times, rather than each character. For example, repeating `abcde` \$4\$ times yields `abcdeabcdeabcdeabcde`.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so the shortest code (not repeated \$m\$ times) in bytes wins.
---
For example, if your program is `print 5` which outputs 5, and you choose \$m = 3\$, then:
* `print 5` should output 5
* `print 5print 5` should output 5
* `print 5print 5print 5` should output 15
* `print 5print 5print 5print 5` should output 5
* `print 5print 5print 5print 5print 5` should output 5
* etc.
  
[Answer]
# [R](https://www.r-project.org/), 16 bytes
```
T=a=1+!T;T=T-1;a
```
[Try it online!](https://tio.run/##K/r/P8Q20dZQWzHEOsQ2RNfQOvF/Ua5GiCaXskJQakFqYklqikJJeWZyKhe6OnQ@F4Y@Y4WSzNzUYoI6CZtkQqZJGH4DAA "R – Try It Online")
Works with \$n=1\$ and \$m=2\$.
---
Previous version:
## [R](https://www.r-project.org/), ~~24~~ ~~23~~ ~~21~~ 19 bytes
-4 bytes thanks to Dominic van Essen
```
T=a=1+0^T^2;T=T-1;a
```
[Try it online!](https://tio.run/##K/r/P8Q20dZQ2yAuJM7IOsQ2RNfQOvF/Ua5GiCaXskJQakFqYklqikJJeWZyKhcWpViEuDB0GyuUZOamFhOpnygjTSg1Epu3AQ "R – Try It Online")
Works with \$n=1\$ and \$m=2\$. This uses the fact that
\$0^k = \begin{cases} 1 & \text{ if } k=0\\0& \text { otherwise.}\end{cases}\$
Therefore, when the code is repeated \$k\$ times, we output \$1+0^{(2-k)^2}\$, which is worth \$2\$ when \$k=2\$, and \$1\$ otherwise.
[Answer]
# [R](https://www.r-project.org/), ~~27~~ ~~20~~ 18 bytes
*Edit: -2 bytes thanks to Robin Ryder, and -2 bytes thanks to pajonk*
```
b=a=3^!(F=F+1)-3;a
```
[Try it online!](https://tio.run/##K/qvrJCfl6qQnF9QqZBfWlJQWlKsYPg/yTbR1jhOUcPN1k3bUFPX2DoRqK6kPB@kLjO1GKpSwZCrKFfDTZMLUzmmCBcX0ISMotRUNDOMSTADu6lp@aVF6A5TgJiqQKaxOCzKLEuluUXYrc7LzBsIq6kl8h8A "R – Try It Online")
n=1, m=3 (or trivially for any m up to 9 for same bytes...)
[Answer]
# [Bash](https://www.gnu.org/software/bash/), 26
n=3, m=2
```
trap echo\ $[++a-2?3:6] 0
```
[Try it online!](https://tio.run/##S0oszvj/v6QosUAhNTkjP0ZBJVpbO1HXyN7YyixWwYDr/38A "Bash – Try It Online")
[Try it online!Try it online!](https://tio.run/##S0oszvj/v6QosUAhNTkjP0ZBJVpbO1HXyN7YyixWwYALt8z//wA "Bash – Try It Online")
[Try it online!Try it online!Try it online!](https://tio.run/##S0oszvj/v6QosUAhNTkjP0ZBJVpbO1HXyN7YyixWwYCLHJn//wE "Bash – Try It Online")
[Answer]
# [Python 3](https://docs.python.org/3/), ~~77~~ ~~50~~ ~~46~~ ~~43~~ ~~39~~ 37 bytes
```
input(1+(len([*open(__file__)])==2))
```
[Try it online!](https://tio.run/##K6gsycjPM/7/PzOvoLREw1BbIyc1TyNaK78ASMXHp2XmpMbHa8Zq2toaaWpy/f8PAA "Python 3 – Try It Online")
Simply counts the number of lines of the code and returns 2 if it is 2 and one otherwise. The `x` is not defined and causes a runtime error immediately after and ends the program.
$$n=1, m=2$$
Thanks to *ChartZ Belatedly* for -27 bytes, *FryAmTheEggman* for -3 bytes, *pxeger* for -4 bytes and *dingledooper* for -2 bytes.
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 5 bytes
```
|d1=^
```
[Run and debug it](https://staxlang.xyz/#c=%7Cd1%3D%5E&i=)
[Run and debug itRun and debug it](https://staxlang.xyz/#c=%7Cd1%3D%5E%7Cd1%3D%5E&i=)
[Run and debug itRun and debug itRun and debug it](https://staxlang.xyz/#c=%7Cd1%3D%5E%7Cd1%3D%5E%7Cd1%3D%5E&i=)
This works for `m = 2, n = 1`.
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 22 bytes, \$n=1\$, \$m=2\$
```
+!$x+!(++$x-2)-!($x-3)
```
[Try it online!](https://tio.run/##K8gvTy0qzkjNyfn/X1tRpUJbUUNbW6VC10hTV1EDSBtr/v8PAA "PowerShell – Try It Online")
[Try it online!Try it online!](https://tio.run/##K8gvTy0qzkjNyfn/X1tRpUJbUUNbW6VC10hTV1EDSBtrYhf9/x8A "PowerShell – Try It Online")
[Try it online!Try it online!Try it online!](https://tio.run/##K8gvTy0qzkjNyfn/X1tRpUJbUUNbW6VC10hTV1EDSBtrkiL6/z8A "PowerShell – Try It Online")
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 39 bytes, \$n=1\$, \$m=2\$
```
1+((gc $PSCommandPath).Count-eq2);exit
```
[Try it online!](https://tio.run/##K8gvTy0qzkjNyfn/31BbQyM9WUElINg5Pzc3MS8lILEkQ1PPOb80r0Q3tdBI0zq1IrOE6/9/AA "PowerShell – Try It Online")
# PowerShell 7, 39 bytes, \$n=1\$, \$m=2\$
```
(gc $PSCommandPath).Count-eq2?2:1;exit
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 11 bytes
```
⎚⊞υω×1⁼³Ｌυ5
```
\$n=5\$, \$m=3\$. [Try it online!](https://tio.run/##ASQA2/9jaGFyY29hbP//4o6a4oqez4XPicOXMeKBvMKz77ysz4U1//8 "Charcoal – Try It Online") [Try it online!Try it online!](https://tio.run/##ASQA2/9jaGFyY29hbP//4o6a4oqez4XPicOXMeKBvMKz77ysz4U1//8 "Charcoal – Try It Online") [Try it online!Try it online!Try it online!](https://tio.run/##S85ILErOT8z5//9R36xHXfPOt57vPDzd8FHjnkOb3@9Zc77VlFTx//8B "Charcoal – Try It Online") [Try it online!Try it online!Try it online!Try it online!](https://tio.run/##S85ILErOT8z5//9R36xHXfPOt57vPDzd8FHjnkOb3@9Zc77VlFri//8DAA "Charcoal – Try It Online") Explanation:
```
⎚
```
Clear the canvas of output from the previous copy.
```
⊞υω
```
Track the number of copies.
```
×1⁼³Ｌυ
```
If that's 3 then print a `1`.
```
5
```
Always print a `5`.
[Answer]
# [Zsh](https://www.zsh.org/) `-F`, 21 bytes, \$n=1\$, \$m=4\$
```
bye `wc -l<$0`-5?1:4
```
[Try it online!](https://tio.run/##qyrO@G9jo@6qbpf2P6kyVSGhPFlBN8dGxSBB19Te0MqE678rV1VxhoKum0Ial42NjYr9fwA "Zsh – Try It Online")
[Try it online!Try it online!Try it online!Try it online!](https://tio.run/##qyrO@G9jo@6qbpf2P6kyVSGhPFlBN8dGxSBB19Te0MqEi0LB/65cVcUZCrpuCmlcNjY2Kvb/AQ "Zsh – Try It Online")
Note the trailing newline. Outputs via exit code.
[Answer]
# JavaScript, ~~65~~ ~~60~~ ~~57~~ ~~46~~ 45 bytes, \$n = 1, m = 5\$
```
var i=-~i;setTimeout(_=>i=i&&alert(i^5?1:5));
```
*Saved 3 bytes thanks to [Arnauld](https://codegolf.stackexchange.com/users/58563).*
*Saved 11 bytes thanks to [user81655](https://codegolf.stackexchange.com/users/46855)*
*Saved 1 byte thanks to [Neil](https://codegolf.stackexchange.com/users/17602)*
By itself:
```
var i=-~i;setTimeout(_=>i=i&&alert(i^5?1:5));
```
Repeated 5 times:
```
var i=-~i;setTimeout(_=>i=i&&alert(i^5?1:5));var i=-~i;setTimeout(_=>i=i&&alert(i^5?1:5));var i=-~i;setTimeout(_=>i=i&&alert(i^5?1:5));var i=-~i;setTimeout(_=>i=i&&alert(i^5?1:5));var i=-~i;setTimeout(_=>i=i&&alert(i^5?1:5));
```
[Answer]
# Factor, 172 bytes, n = 2, m = 4
Let's see some love for the [language of the month](https://codegolf.meta.stackexchange.com/questions/20824/language-of-the-month-for-march-2021-factor?cb=1).
```
 USING: prettyprint namespaces init math io kernel ; IN: main 0 1 set-global
1 get-global 1 + 1 set-global [ 1 get-global 4 = [ 8 ] [ 2 ] if . flush ] 2 add-shutdown-hook !
```
(The leading space before `USING:` is significant, and so is the lack of newline at the end)
We store a simple counter in a global variable (global variables in Factor are, by convention, symbols, but there's no actual requirement, so our global variable is named the integer `1`). Then the part of our code that "repeats" simply increments this global. We register a shutdown hook that does the final checks for us. The fact that we register the hook several times is irrelevant, because we give it the same name each time (this name, incidentally, is the integer `2`; once again, Factor is happy to accept a very loose definition of "name").
[Try it online!](https://tio.run/##VYmxCsIwGAZ3n@JzlootDlJxli5dipM4/DZJE5omIfmL@PQxk9Dl4O4UjexjzngMXX9vEaJk/oZoHMPRIlOgUSYYZxgLsYbxmGV00uKKrm9LNA4n1EiSq8n6N9ldjekv5Rw2F09s/hm3ki54FTaFRuEIZdekizQgIaqkVxb@4yrt/Yx9zj8 "Factor – Try It Online")
[Try it online!Try it online!](https://tio.run/##1Y6xCsIwFEV3v@I6S8QWB6k4S5cu4iQOsUmb0PQlJK@IXx8zCf0ElwP3nOUOsmcfc8b91nbXBiFq5k@IlhgkZ52C7HWCJcuYJRtYj0lH0g5ntF1TpCUcUCFpFqPzL@k2FcbfKGW3qnhg1Y@4FHXCs7AutAP2GNySTBk1pFIimYWVf5Mw3k/Y/tPXnL8 "Factor – Try It Online")
[Try it online!Try it online!Try it online!](https://tio.run/##7Y6xCsIwFEV3v@I6S8QWB6k4S5cuxUkcYpM0oWkSklfEr4@ZhH6Am8uFc85yFR/Ix5xx69vu2iBESfQO0TiC47NMgQ8ywThDmDlpGI9JRictzmi7pkjjcECFJImN1j@53VQYv1DKblVxx6ofcSnqhEfZuqxR2EPZJekCNbgQLOmFhH85pr2fsP1//c3XnD8 "Factor – Try It Online")
[Try it online!Try it online!Try it online!Try it online!](https://tio.run/##7Y6xCsIwFEV3v@I6S8QWB6k4S5cuxUkcYpM0oWkSklfEr4@ZhP5DlwvnnOUqPpCPOePRt929QYiS6BuicQTHZ5kCH2SCcYYwc9IwHpOMTlpc0XZNkcbhhApJEhutf3O7qzD@oZTDquKJVT/jVtQFr7J1WaNwhLJL0gVqcCFY0gsJ/3FMez9hv33dvub8Aw "Factor – Try It Online")
[Answer]
# Java 6, ~~120~~ 77 bytes, \$n=1, m=2\$
```
enum A{A;{System.out.print(new java.io.File("A.java").length()==154?2:1);}}//
```
*Saved 43 bytes thanks to [Olivier Grégoire](https://codegolf.stackexchange.com/users/16236).*
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~6~~ ~~5~~ 4 bytes, \$ n=1, m=2 \$
-1 thanks to @Kevin Cruijssen
```
.gΘ>
```
[Try it online!](https://tio.run/##yy9OTMpM/f9fL/3cDLv//wE "05AB1E – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt), ~~8~~ 6 bytes, \$n=1\$, \$m=2\$
Works because Javascript numbers [don't overflow from positive to negative](https://stackoverflow.com/questions/19054891/does-javascript-handle-integer-overflow-and-underflow-if-yes-how).  
Essentially J is incremented to infinity, 2 is output only on the case of two repetitions.
```
J°?1:2 // J is a builtin variable that equals -1 at first.
J°     // If J++ is truthy (-1, 1, 2, etc)
  ?1:2 // output 1, otherwise 2.
```
[Try it here.](https://ethproductions.github.io/japt/?v=1.4.6&code=SrA/MToy&input=)
[Answer]
# [JavaScript (V8)](https://v8.dev/), 40 bytes
```
0;i=-~this.i,{get x(){print(i-3?1:3)}}.x
```
[Try it online!](https://tio.run/##FY3BCoMwEETvfsUeE6xB8VKaBn/FELftitUQlyAE@@tpPM2DGd7MNtrdBfLcxHt227ozuG1CMDDmVpNpfvyhXdEtvZHhEDL5QCsLavqhe/TyPNWRR8WBvkLq6rUFEEtZzsXQ6hJPA90FdS0hVQATlhqBdGGMdhHXnQro0bKYZXGc@Q8 "JavaScript (V8) – Try It Online")
Uses the same trick as [my solution the other day](https://codegolf.stackexchange.com/a/220447/46855). The core idea is putting the `print()` behind a getter function. If the `.x` property is accessed it will print the result. This is placed at the end of the generated code so that it accesses `.x` on the last iteration but when concatenated it becomes `.x0` (does not access `.x`).
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 26 bytes, \$n=1\$, \$m=2\$
```
(x=>x?y=>y?g=z=>z?g:1:2:1)
```
[Try it online!](https://tio.run/##JcxBDsIgEEDRPaeY5UwaibhEgauUIBIMlgawaWs8OzZx9Tcv/2kXW12JcztN@e57ze/iPCgYO65Kr2ZTejNB7UrvJkghL1JQH3kr8YXE2CMXwHh4cYUINxDno8NA8GEALk81J89TDugXm/B/58XP3jaMREjEvqz/AA "JavaScript (Node.js) – Try It Online")
Is this valid? I'm not quite sure...
---
# [JavaScript (SpiderMonkey)](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Releases/45), 28 bytes, \$n=1\$, \$m=2\$
```
f=x=>((f+'').length==54)+1//
```
[Try it online!](https://tio.run/##JcxBDsIgEEDRPaeYXWdCpJLoSvEsJTitKC0EsNEYz45NXP3V@3e72uKyT3VXkr9ynuPy4Hcr8Zkdg4GhjeZlLoij7DpSgZep3ow5Hkjqvm@DqtnPSEKMMQP6TegTeDiD3m@VkuAjAFxcSgysQpyQVxvw/1eZE9uKngiJxLf9AA "JavaScript (SpiderMonkey) – Try It Online")
Another similar answer [(requires Firefox 86**-**)](https://bugzilla.mozilla.org/show_bug.cgi?id=1579792)
[Answer]
# [Julia](http://julialang.org/), 20 bytes, \$n=1\$, \$m=2\$
```
a=b=1;a+=1;b+=a==3;b
```
[Try it online!](https://tio.run/##yyrNyUw0rPhfrGCroKSk9D/RNsnW0DpRG0gkadsm2toaWyf9B0pEG1ql5qXoGsZycaXlFylkKmTmKRhamXIpgEBBUWZeSU6eRqYmKr84Dl0ktSwxR8M3tSRRryCxqDgVrEITTY0mF9Cm/wA "Julia 1.0 – Try It Online")
[Answer]
# [PHP](https://php.net/), ~~38~~ 34 bytes \$n=1\$ \$m=2\$
```
<?=count(file(__FILE__))!=3?:2;
//
```
[Try it online!](https://tio.run/##K8go@P/fxt42Ob80r0QjLTMnVSM@3s3TxzU@XlNT0dbY3srImktf//9/AA "PHP – Try It Online")
This hacky solution relies on getting the current code's first line and testing the length is 76 (38 \* 2). Works as a standalone but needs the `//` to comment any repetitions of the program so that only one output is displayed. Also in ternary conditions `a?b:c`, if `b` is missing, the value of `a` is used, and in PHP `true` is displayed as `1`.
EDIT: saved 4 bytes by counting the lines of code instead of length of the first line. Notice the newline *before* the comment
[Answer]
# Brainfuck, 268264 characters / 10199 bytes, n=1, m=2
```
    ++>+ Init
    [->+>+<<]>>[-<<+>>]< Copy second into third
    [-<<->>]<< Check if equal
        [ If not equal
            [-] Reset first
            +++++++++++++++++++++++++++++++++++++++++++++++++
            .
            ------------------------------------------------- Output 1
            >>>-<<< Set a flag saying that this cycle was passed
        ]
        >>>+ Change the flag
        [ If the flag is nonzero (meaning the previous cycle was not passed)
            [-] Reset the flag
            ++++++++++++++++++++++++++++++++++++++++++++++++++
            .
            -------------------------------------------------- Output 2
        ]
    <[-]<<[-] Reset first and third
```
[Try it online!](https://fatiherikli.github.io/brainfuck-visualizer/#Kys+KyBJbml0ClstPis+Kzw8XT4+Wy08PCs+Pl08IENvcHkgc2Vjb25kIGludG8gdGhpcmQKWy08PC0+Pl08PCBDaGVjayBpZiBlcXVhbAogICAgWyBJZiBub3QgZXF1YWwKICAgICAgICBbLV0gUmVzZXQgZmlyc3QKICAgICAgICArKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrCiAgICAgICAgLgogICAgICAgIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0gT3V0cHV0IDEKICAgICAgICA+Pj4tPDw8IFNldCBhIGZsYWcgc2F5aW5nIHRoYXQgdGhpcyBjeWNsZSB3YXMgcGFzc2VkCiAgICBdCiAgICA+Pj4rIENoYW5nZSB0aGUgZmxhZwogICAgWyBJZiB0aGUgZmxhZyBpcyBub256ZXJvIChtZWFuaW5nIHRoZSBwcmV2aW91cyBjeWNsZSB3YXMgbm90IHBhc3NlZCkKICAgICAgICBbLV0gUmVzZXQgdGhlIGZsYWcKICAgICAgICArKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKwogICAgICAgIC4KICAgICAgICAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSBPdXRwdXQgMgogICAgXQo8PDwgUmV0dXJuIHRvIGZpcnN0ClstXSBSZXNldCBmaXJzdAo+PlstXTw8IFJlc2V0IHRoaXJkCg==)
This can take a while to run, so set the delay to minimum if you want to test it.
Definitely not a good golfing language, but it was interesting to write just for the sake of it.
(This challenge is impossible to complete in BF as it's stated because repeated code will always produce a multiple-char output, so in this solution the answer is the last output char)
Edit: I'm completely brain dead and forgot this is code golf, so I wrote the last lines in a really weird way. Corrected, saved 4 chars.
[Answer]
# [Grok](https://github.com/AMiller42/Grok-Language), 31 bytes, n=1, m=2
```
`  j
ljq
11z
+=1
 {k
j2`h
`lzq
```
Note the single trailing newline. Program was multiplied with `cat odd1out >> odd2out`. At first I thought I would have to rework my solution because of how programs need to be multiplied, but then I realized I could just turn it on its side.
]