23
\$\begingroup\$

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 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.
\$\endgroup\$
15
  • 1
    \$\begingroup\$ Are functions allowed? \$\endgroup\$
    – Jonah
    Mar 9, 2021 at 18:05
  • 1
    \$\begingroup\$ Huh, I accidentally chose the same n and m as the example. Spooky! \$\endgroup\$
    – Neil
    Mar 9, 2021 at 21:17
  • 2
    \$\begingroup\$ @Kaddath If n is zero, then the program always outputs 0, no matter \$m\$, so no \$\endgroup\$ Mar 10, 2021 at 13:10
  • 1
    \$\begingroup\$ @OlivierGrégoire Yes (and I think a lot of answers already do so), so long as it still "works" as specified when repeated \$\endgroup\$ Mar 11, 2021 at 15:45
  • 1
    \$\begingroup\$ @user253751 Only for Quine challenges \$\endgroup\$ Mar 11, 2021 at 17:00

19 Answers 19

9
\$\begingroup\$

R, 16 bytes

T=a=1+!T;T=T-1;a

Try it online!

Works with \$n=1\$ and \$m=2\$.


Previous version:

R, 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!

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.

\$\endgroup\$
4
  • \$\begingroup\$ 21 bytes \$\endgroup\$ Mar 10, 2021 at 6:52
  • \$\begingroup\$ @DominicvanEssen Neat, thanks! \$\endgroup\$ Mar 10, 2021 at 7:15
  • \$\begingroup\$ 19 bytes \$\endgroup\$ Mar 10, 2021 at 15:12
  • \$\begingroup\$ @DominicvanEssen Thanks again! \$\endgroup\$ Mar 10, 2021 at 15:25
7
\$\begingroup\$

R, 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!

n=1, m=3 (or trivially for any m up to 9 for same bytes...)

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 25 bytes \$\endgroup\$ Mar 10, 2021 at 5:43
  • \$\begingroup\$ @RobinRyder - Thanks! That inspired me to get rid of 5 more, too...! \$\endgroup\$ Mar 10, 2021 at 6:25
  • \$\begingroup\$ Shouldn't this work for second approach? Try it online! \$\endgroup\$
    – pajonk
    Mar 10, 2021 at 7:27
  • 1
    \$\begingroup\$ @pajonk - Thanks! That's amazing - I never would have thought that the operator precedence could allow that! It looks so wrong, but works! Cool. \$\endgroup\$ Mar 10, 2021 at 8:04
6
\$\begingroup\$

Bash, 26

n=3, m=2

trap echo\ $[++a-2?3:6] 0

Try it online!

Try it online!Try it online!

Try it online!Try it online!Try it online!

\$\endgroup\$
6
\$\begingroup\$

Python 3, 77 50 46 43 39 37 bytes

input(1+(len([*open(__file__)])==2))

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.

\$\endgroup\$
6
  • \$\begingroup\$ 50 bytes using the same trick \$\endgroup\$ Mar 9, 2021 at 18:26
  • \$\begingroup\$ Changing from indexing to computing a power saves enough to get 47 bytes. \$\endgroup\$ Mar 9, 2021 at 19:04
  • 1
    \$\begingroup\$ 36 by outputting via exit code and counting lines: Try it online! \$\endgroup\$
    – pxeger
    Mar 9, 2021 at 20:53
  • \$\begingroup\$ @pxeger the counting lines is smart, but I believe by output the OP means standard output only so using exit code for that is probably not allowed. \$\endgroup\$ Mar 10, 2021 at 5:05
  • 1
    \$\begingroup\$ You can use input() to get 37 bytes. \$\endgroup\$ Mar 10, 2021 at 5:22
5
\$\begingroup\$

Stax, 5 bytes

|d1=^

Run and debug it

Run and debug itRun and debug it

Run and debug itRun and debug itRun and debug it

This works for m = 2, n = 1.

\$\endgroup\$
5
\$\begingroup\$

PowerShell, 22 bytes, \$n=1\$, \$m=2\$

+!$x+!(++$x-2)-!($x-3)

Try it online!

Try it online!Try it online!

Try it online!Try it online!Try it online!

PowerShell, 39 bytes, \$n=1\$, \$m=2\$

1+((gc $PSCommandPath).Count-eq2);exit

Try it online!

PowerShell 7, 39 bytes, \$n=1\$, \$m=2\$

(gc $PSCommandPath).Count-eq2?2:1;exit

\$\endgroup\$
5
\$\begingroup\$

Charcoal, 11 bytes

⎚⊞υω×1⁼³Lυ5

\$n=5\$, \$m=3\$. Try it online! Try it online!Try it online! Try it online!Try it online!Try it online! Try it online!Try it online!Try it online!Try it online! Explanation:

Clear the canvas of output from the previous copy.

⊞υω

Track the number of copies.

×1⁼³Lυ

If that's 3 then print a 1.

5

Always print a 5.

\$\endgroup\$
4
\$\begingroup\$

Zsh -F, 21 bytes, \$n=1\$, \$m=4\$

bye `wc -l<$0`-5?1:4

Try it online!

Try it online!Try it online!Try it online!Try it online!

Note the trailing newline. Outputs via exit code.

\$\endgroup\$
4
\$\begingroup\$

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.

Saved 11 bytes thanks to user81655

Saved 1 byte thanks to Neil

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

\$\endgroup\$
7
  • 1
    \$\begingroup\$ this.i=-~this.i should save 3 bytes. \$\endgroup\$
    – Arnauld
    Mar 9, 2021 at 22:10
  • 1
    \$\begingroup\$ You should also be able to remove the this. at the start \$\endgroup\$
    – user81655
    Mar 10, 2021 at 1:17
  • 1
    \$\begingroup\$ b&&(b=alert(i^5?1:5)) can become b=b&&alert(i^5?1:5) \$\endgroup\$
    – user81655
    Mar 10, 2021 at 1:19
  • 1
    \$\begingroup\$ Also you could replace b with i so you don't have to declare ,b=1 \$\endgroup\$
    – user81655
    Mar 10, 2021 at 1:23
  • 1
    \$\begingroup\$ var i=-~i; is an alternative way to save a byte. \$\endgroup\$
    – Neil
    Mar 10, 2021 at 11:45
4
+50
\$\begingroup\$

Factor, 172 bytes, n = 2, m = 4

Let's see some love for the language of the month.

 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!

Try it online!Try it online!

Try it online!Try it online!Try it online!

Try it online!Try it online!Try it online!Try it online!

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

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.

\$\endgroup\$
14
  • 1
    \$\begingroup\$ I think usually if you assume a file name you need to include it in your byte score (also you can use a name of a I think) \$\endgroup\$ Mar 10, 2021 at 16:22
  • \$\begingroup\$ @CommandMaster Since the interface is named Main, it cannot be run unless the file name is Main.java. \$\endgroup\$ Mar 10, 2021 at 17:30
  • \$\begingroup\$ @iota I just tried that and it seems to work. \$\endgroup\$
    – user
    Mar 10, 2021 at 17:33
  • 1
    \$\begingroup\$ 1. Your program can be shortened for Java versions 5 & 6: codegolf.stackexchange.com/a/140181/16236 2. You can get rid of the public as Java allows running it if you call java Main.java (yes, without compiling). 3. You can name the class A. All in all, you can reduce to 92 bytes: enum A{A;{System.out.print(new java.io.File("A.java").length()==184?2:1);System.exit(0);}}// You don't need to have it running on TIO to have a valid submission. \$\endgroup\$ Mar 11, 2021 at 15:44
  • 1
    \$\begingroup\$ Also, usually, Codegolf rules allow you to write to stderr after you've output the correct value, so this 77 bytes solution would work: enum A{A;{System.out.print(new java.io.File("A.java").length()==154?2:1);}}//. Then, running java A.java on Java 5 or 6 will yield the correct output. \$\endgroup\$ Mar 11, 2021 at 15:47
3
\$\begingroup\$

05AB1E, 6 5 4 bytes, \$ n=1, m=2 \$

-1 thanks to @Kevin Cruijssen

.gΘ>

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Can't your second solution be 4 bytes by replacing the 1+ with >? \$\endgroup\$ Mar 25, 2021 at 15:45
  • \$\begingroup\$ It can, I'm not sure how I didn't notice that. Thanks! \$\endgroup\$ Mar 25, 2021 at 18:49
2
\$\begingroup\$

Japt, 8 6 bytes, \$n=1\$, \$m=2\$

Works because Javascript numbers don't overflow from positive to negative.
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.

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

JavaScript (V8), 40 bytes

0;i=-~this.i,{get x(){print(i-3?1:3)}}.x

Try it online!

Uses the same trick as my solution the other day. 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).

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

JavaScript (Node.js), 26 bytes, \$n=1\$, \$m=2\$

(x=>x?y=>y?g=z=>z?g:1:2:1)

Try it online!

Is this valid? I'm not quite sure...


JavaScript (SpiderMonkey), 28 bytes, \$n=1\$, \$m=2\$

f=x=>((f+'').length==54)+1//

Try it online!

Another similar answer (requires Firefox 86-)

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

Julia, 20 bytes, \$n=1\$, \$m=2\$

a=b=1;a+=1;b+=a==3;b

Try it online!

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

PHP, 38 34 bytes \$n=1\$ \$m=2\$

<?=count(file(__FILE__))!=3?:2;
//

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

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

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!

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.

\$\endgroup\$
2
  • \$\begingroup\$ Can you assume an env supporting \r or \b to erase last output? \$\endgroup\$
    – l4m2
    Apr 9, 2021 at 11:48
  • \$\begingroup\$ @l4m2 I don't really know if that works in bf \$\endgroup\$
    – Mthd
    Apr 9, 2021 at 13:28
2
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.