# What's the odd one out?

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

# R, 16 bytes

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


Try it online!

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

Previous version:

## R, 242321 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.

• 21 bytes Mar 10 at 6:52
• @DominicvanEssen Neat, thanks! Mar 10 at 7:15
• 19 bytes Mar 10 at 15:12
• @DominicvanEssen Thanks again! Mar 10 at 15:25

# R, 2720 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...)

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

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! # Python 3, 7750464339 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. • 50 bytes using the same trick Mar 9 at 18:26 • Changing from indexing to computing a power saves enough to get 47 bytes. Mar 9 at 19:04 • 36 by outputting via exit code and counting lines: Try it online! Mar 9 at 20:53 • @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. Mar 10 at 5:05 • You can use input() to get 37 bytes. Mar 10 at 5:22 # 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. # 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!

1+((gc $PSCommandPath).Count-eq2);exit  Try it online! # PowerShell 7, 39 bytes, $$\n=1\$$, $$\m=2\$$ (gc$PSCommandPath).Count-eq2?2:1;exit



# Charcoal, 11 bytes

⎚⊞υω×1⁼³Ｌυ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⁼³Ｌυ


If that's 3 then print a 1.

5


Always print a 5.

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

# JavaScript, 65605746 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));

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

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

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

• 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) Mar 10 at 16:22
• @CommandMaster Since the interface is named Main, it cannot be run unless the file name is Main.java. Mar 10 at 17:30
• @iota I just tried that and it seems to work.
– user
Mar 10 at 17:33
• 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. Mar 11 at 15:44
• 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. Mar 11 at 15:47

# 05AB1E, 65 4 bytes, $$\ n=1, m=2 \$$

-1 thanks to @Kevin Cruijssen

.gΘ>


Try it online!

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

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

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

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

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

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


Try it online!

# 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

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

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

# Grok, 31 bytes, n=1, m=2

  j
ljq
11z
+=1
{k
j2h
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.