# Question
Title: Polite Near-Sighted Drunk Bot on a Minefield
As the title may suggest, this problem is semi-inspired by the Polite Near-Sighted Drunk Bot by @N.P.

Our poor bot is placed on a cartesian grid at the origin, and after each minute, it moves 1 unit in one of four directions (Up, Down, Left, Right).

After *n* minutes, all of the latent mines on the grid activate, killing any poor bot that might find themselves over them. The mines are located at all integer coordinates satisfying the equation |y|=|x|.

# Challenge

You will be provided *n*, the number of minutes before the mines blast, as an input, and as an output, you must find the probability that the bot is **dead**.

**Input**: An natural number representing *n*.

**Output**: Let the probability the bot is **dead** be p/q, where p and q are relatively prime whole numbers (q can't be 0, but p can). Output p.

# Rules

* Your algorithm **must not** run in exponential or higher time. It ideally should run in polynomial time or less.
* Your algorithm must be able to handle inputs of `n`\<20 (can be adjusted if too hard) in a reasonable time.
* This is a challenge.
* Iterating over all possibilities for a given *n* will most definitely not be accepted as an answer.

# Test Cases

`1`-\>`0`

`2`-\>`3`

`4`-\>`39`

`6`-\>`135`

`8`-\>`7735`

`10`-\>`28287`

# Example Calculation for *n*=6

We have 4 possible moves: U, D, R, and L. The total number of paths that could be taken is 4^6, or 4096. There are 4 possible cases that land along the line y = x: x,y = ±1; x,y = ±2; x,y = ±3; or x = y = 0. We will count the number of ways to end up at (1,1), (2,2), and (3,3), multiply them by 4 to account for the other quadrants, and add this to the number of ways to end up at (0,0).

**Case 1:** The bot ends at (3, 3). In order for the bot to end up here, it must have had 3 right moves, and 3 up moves. In other words, the total number of ways to get here is the ways to rearrange the letters in the sequence RRRUUU, which is 6 choose 3 = 20.

**Case 2:** The bot ends at (2,2). In order for the bot to end up here, it could have had 2 up moves, 3 right moves, and 1 left move; or 2 right moves, 3 up moves, and 1 down move. Thus, the total number of ways to get here is sum of the ways to rearrange the letters in the sequences RRRLUU and UUUDRR, both of which are (6 choose 1) * (5 choose 2) = 60, for a total of 120 possibilities.

**Case 3:** The bot ends at (1,1). In order for the bot to end up here, it could have had: 1 right move, 3 up moves, and 2 down moves. In this case, the number of ways to rearrange the letters in the sequence RUUUDD is (6 choose 1)\*(5 choose 2) = 60.

1 up move, 3 right moves, and 2 left moves. In this case, the number of ways to rearrange the letters in the sequence URRRLL is (6 choose 1)\*(5 choose 2) = 60.

2 right moves, 1 left move, 2 up moves, and 1 down move. In this case, the number of ways to rearrange the letters in the sequence UUDRRL is (6 choose 1)\* (5 choose 1)\*(4 choose 2) = 180.

Thus, the total number of ways to end up at (1,1) is 300.

**Case 4:** The bot ends at (0,0). In order for the bot to end up here, it could have had:

3 right moves and 3 left moves. In this case, the number of ways to rearrange the letters in the sequence RRRLLL is (6 choose 3) = 20.

3 up moves and 3 down moves. In this case, the number of ways to rearrange the letters in the sequence UUUDDD is (6 choose 3) = 20.

1 right move, 1 left move, 2 up moves, and 2 down moves. In this case, the number of ways to rearrange the letters in the sequence RLUUDD is (6 choose 1)\* (5 choose 1)\*(4 choose 2) = 180.

1 up move, 1 down move, 2 right moves, and 2 left moves. In this case, the number of ways to rearrange the letters in the sequence RRLLUD is (6 choose 1)\* (5 choose 1)\*(4 choose 2) = 180.

Thus, the total number of ways to end up at (0,0) is 400.

Adding these cases together, we get that the total number of ways to end up on |y| = |x| is 4(20 + 120 + 300) + 400 = 2160. Thus, our probability is 2160/4096. When this fraction is fully reduced, it is 135/256, so our answer is **135**.

# Answer
# Python 2, 65 bytes

```
def p(n):b=2**n;r=b*b-((~b)**n/b**(n/2)%-b)**2;print~n%2*r/(r&-r)

```

Try it online!

The probability the bot is dead can be expressed as:

> $$ f(n) = 2s-s^2\text{, where } s=\frac{1}{2^n}\binom{n}{n/2}$$

and the binomial is understood to equal \\$0\\$ when \\$n/2\\$ isn't whole.

We can reason like this. What's the probability that the bot lands on the \\$y=x\\$ line? This happens if the total number of up and left moves equals the total number of down and right moves. This is the same probability that, say, you flip a coin \\$n\\$ times and get as many tails as heads. You must choose \\$n/2\\$ flips to be heads of \\$n\\$ flips, which can be done in \\$\binom{n}{n/2}\\$ ways, of \\$2^n\\$ possible sequences overall, giving probability

$$s=\frac{1}{2^n}\binom{n}{n/2}$$

The probability of landing the \\$y=-x\\$ line is also \\$s\\$. So, the probability of landing on either line is the sum of these or \\$2s\\$, except we're double-counting the probability of landing of \\$x=y=0\\$ in both lines, and must subtract it to compensate.

It turns out the probability of landing on \\$x=y=0\\$ is just \\$s^2\\$, the product of the probability of landing on each line. We can argue the events are independent as follows: if we pick a random sequence of equal numbers of "Up or Left" and "Down or Right" to land on \\$x=y\\$ and likewise with "Up or Right" and "Down or Left" for \\$x=-y\\$, we can uniquely combine them into a sequence of Up, Down, Left, Right by taking the intersection of the pairs of directions at each position.

So, the probability of landing on \\$x=y\\$ or \\$x=-y\\$ is \\$2s-s^2\\$.

The code computes the binomial \\$\binom{n}{n/2}\\$ using this expression as `(b+1)**n/b**(n/2)%b` with base `b=2**n`. To extract the numerator from the probability fraction, we note that the denominator is a power of 2, so we use the expression `r/(r&-r)` to divide out the maximal power of 2 is `r`, expressed as the classic bit trick `r&-r`.

The solution is golfed by writing \\$2s-s^2\\$ as \\$1-(1-s)^2\\$ so that \\$s\\$ is only referenced once, and working without the \\$1/2^n\\$ fractions to stay within the integers. The computation is polynomial-time in \\$n\\$ even with the funky way of computing binomials.

---

After writing the proof of the probability the bot dies, I found a possibly cleaner way to prove it and present it.

Let's keep track of the values of \\$a=x+y\\$ and \\$b=x-y\\$ after each move of the bot. Each of the four directions up, down, left, and right either increments or decrements each of \\$a\\$ and \\$b\\$, with the four directions corresponding to the four combinations.

So, a random move is equivalent to randomly adding \\$\pm 1\\$ to \\$a\\$ and independently \\$\pm 1\\$ to \\$b\\$. This is equivalent to doing separate random walks on \\$a\\$ and \\$b\\$.

Now, the robot ends on the line \\$x=y\\$ or \\$x=-y\\$ exactly when \\$a=0\\$ or \\$b=0\\$. So, the probability of ending with \\$a=0\\$ is \\$s=\frac{1}{2^n}\binom{n}{n/2}\\$ and likewise for \\$b=0\\$. Since the walks are independent, the probability that \\$a\neq 0\\$ and \\$b\neq 0\\$ is \\$(1-s)^2\\$, so the probability that at least one is zero is the complement \\$1-(1-s)^2\\$.
> 18 votes

---
Tags: code-golf, restricted-complexity
---


title: (A → B) → (¬B → ¬A)

text:
# Question
Title: (A → B) → (¬B → ¬A)
Well I think it is about time we have another question.

This time we are going to prove the well known logical truth

\\$(A \rightarrow B) \rightarrow (\neg B \rightarrow \neg A)\\$

To do this we will use Łukasiewicz's third Axiom Schema, an incredibly elegant set of three axioms that are complete over propositional logic.

Here is how it works:

# Axioms

The Łukasiewicz system has three axioms. They are:

\\$\phi\rightarrow(\psi\rightarrow\phi)\\$

\\$(\phi\rightarrow(\psi\rightarrow\chi))\rightarrow((\phi\rightarrow\psi)\rightarrow(\phi\rightarrow\chi))\\$

\\$(\neg\phi\rightarrow\neg\psi)\rightarrow(\psi\rightarrow\phi)\\$

The axioms are universal truths regardless of what we choose for \\$\phi\\$, \\$\psi\\$ and \\$\chi\\$. At any point in the proof we can introduce one of these axioms. When we introduce an axiom you replace each case of \\$\phi\\$, \\$\psi\\$ and \\$\chi\\$, with a "complex expression". A complex expression is any expression made from Atoms, (represented by the letters \\$A\\$-\\$Z\\$), and the operators implies (\\$\rightarrow\\$) and not (\\$\neg\\$).

For example if I wanted to introduce the first axiom (L.S.1) I could introduce

\\$A\rightarrow(B\rightarrow A)\\$

or

\\$(A\rightarrow A)\rightarrow(\neg D\rightarrow(A\rightarrow A))\\$

In the first case \\$\phi\\$ was \\$A\\$ and \\$\psi\\$ was \\$B\\$, while in the second case both were more involved expressions. \\$\phi\\$ was \\$(A\rightarrow A)\\$ and \\$\psi\\$ was \\$\neg D\\$.

What substitutions you choose to use will be dependent on what you need in the proof at the moment.

## Modus Ponens

Now that we can introduce statements we need to relate them together to make new statements. The way that this is done in Łukasiewicz's Axiom Schema (L.S) is with Modus Ponens. Modus Ponens allows us to take two statements of the form

\\$\phi\\$

\\$\phi\rightarrow \psi\\$

and instantiate a new statement

\\$\psi\\$

Just like with our Axioms \\$\phi\\$ and \\$\psi\\$ can stand in for any arbitrary statement.

The two statements can be anywhere in the proof, they don't have to be next to each other or any special order.

## Task

Your task will be to prove the law of contrapositives. This is the statement

\\$(A\rightarrow B)\rightarrow(\neg B\rightarrow\neg A)\\$

Now you might notice that this is rather familiar, it is an instantiation of the reverse of our third axiom

\\$(\neg\phi\rightarrow\neg\psi)\rightarrow(\psi\rightarrow\phi)\\$

However this is no trivial feat.

## Scoring

Scoring for this challenge is pretty simple, each time you instantiate an axiom counts as a point and each use of modus ponens counts as a point. This is essentially the number of lines in your proof. The goal should be to minimize your score (make it as low as possible).

## Example Proof

Ok now lets use this to construct a small proof. We will prove \\$A\rightarrow A\\$.

Sometimes it is best to work backwards since we know where we want to be we can figure how we might get there. In this case since we want to end with \\$A\rightarrow A\\$ and this is not one of our axioms we know the last step must be modus ponens. Thus the end of our proof will look like

```
φ
φ → (A → A)
A → A       M.P.

```

TeX

Where \\$\phi\\$ is an expression we don't yet know the value of. Now we will focus on \\$\phi\rightarrow(A\rightarrow A)\\$. This can be introduced either by modus ponens or L.S.3. L.S.3 requires us to prove \\$(\neg A\rightarrow\neg A)\\$ which seems just as hard as \\$(A\rightarrow A)\\$, so we will go with modus ponens. So now our proof looks like

```
φ
ψ
ψ → (φ → (A → A))
φ → (A → A)        M.P.
A → A              M.P.

```

TeX

Now \\$\psi\rightarrow(\phi\rightarrow(A\rightarrow A))\\$ looks a lot like our second axiom L.S.2 so we will fill it in as L.S.2

```
A → χ
A → (χ → A)
(A → (χ → A)) → ((A → χ) → (A → A)) L.S.2
(A → χ) → (A → A)                   M.P.
A → A                               M.P.

```

TeX

Now our second statement \\$(A\rightarrow(\chi\rightarrow A))\\$ can pretty clearly be constructed from L.S.1 so we will fill that in as such

```
A → χ
A → (χ → A)                         L.S.1
(A → (χ → A)) → ((A → χ) → (A → A)) L.S.2
(A → χ) → (A → A)                   M.P.
A → A                               M.P.

```

TeX

Now we just need to find a \\$\chi\\$ such that we can prove \\$A\rightarrow\chi\\$. This can very easily be done with L.S.1 so we will try that

```
A → (ω → A)                                     L.S.1
A → ((ω → A) → A)                               L.S.1
(A → ((ω → A) → A)) → ((A → (ω → A)) → (A → A)) L.S.2
(A → (ω → A)) → (A → A)                         M.P.
A → A                                           M.P.

```

TeX

Now since all of our steps our justified we can fill in \\$\omega\\$, as any statement we want and the proof will be valid. We could choose \\$A\\$ but I will choose \\$B\\$ so that it is clear that it doesn't need to be \\$A\\$.

```
A → (B → A)                                     L.S.1
A → ((B → A) → A)                               L.S.1
(A → ((B → A) → A)) → ((A → (B → A)) → (A → A)) L.S.2
(A → (B → A)) → (A → A)                         M.P.
A → A                                           M.P.

```

TeX

Try it online!

And that is a proof.

## Resources

### Verification program

Here is a Prolog program you can use to verify that your proof is in fact valid. Each step should be placed on its own line. `->` should be used for implies and `-` should be used for not, atoms can be represented by any string of alphabetic characters.

### Metamath

Metamath uses the Łukasiewicz system for its proofs in propositional calculus, so you may want to poke around there a bit. They also have a proof of the theorem this challenge asks for which can be found here. There is an explanation here of how to read the proofs.

### The Incredible Proof Machine

@Antony made me aware of a tool called The Incredible Proof machine which allows you to construct proofs in a number of systems using a nice graphical proof system. If you scroll down you will find they support the Łukasiewicz system. So if you are a more visual oriented person you can work on your proof there. Your score will be the number of blocks used minus 1.

# Answer
# ~~88~~ ~~82~~ ~~77~~ 72 steps

Thanks to H.PWiz for better combinator conversions that saved 10 steps!

### Explanation

You might be familiar with the Curry–Howard correspondence, in which theorems correspond to types and proofs correspond to programs of those types. The first two axioms in the Łukasiewicz system are actually the K and S combinators, and it’s well known that we can translate lambda calculus expressions into the SK combinatory expressions.

So let’s write down some expressions corresponding to our axioms (the following is valid Haskell syntax, which is convenient because we can quite literally check our proofs using the Haskell compiler):

```
data Not φ

k :: φ -> (ψ -> φ)
k x _ = x

s :: (φ -> (ψ -> χ)) -> ((φ -> ψ) -> (φ -> χ))
s x y z = x z (y z)

c :: (Not φ -> Not ψ) -> (ψ -> φ)
c = error "non-computational axiom"

```

Then we can write a proof of the desired statement as a program in terms of `c` (this part takes a bit of cleverness, but it’s much easier to write this than an 72-line axiomatic proof):

```
pf :: (a -> b) -> (Not b -> Not a)
pf x y = c (\z -> c (\_ -> y) (x (c (c (\_ -> z)) x))) k

```

and convert it into an SK combinatory expression:

```
pf' :: (a -> b) -> (Not b -> Not a)
pf' =
  s (k (s (k (s c (k k)))))
    (s (k (s (s (k s) (s (k k) (s (k c) k)))))
       (s (k k) (s (k (s s (s (s (k c) (s (k c) k))))) k)))

```

The 17 `k`, 16 `s`, and 4 `c` combinators above correspond to the 16 LS1, 16 LS2, and 4 LS3 invocations in the proof below, and the 38 applications of a function to a value above correspond to the 38 MP invocations below.

Why only 16 LS1 invocations? It turns out one of the `k` combinators above has a free type variable, and instantiating it carefully turns it into a duplicate of another one that has already been derived.

### The proof

1. (A → B) → (¬¬A → (A → B)) LS1
2. ¬¬A → (¬¬(A → B) → ¬¬A) LS1
3. (¬¬(A → B) → ¬¬A) → (¬A → ¬(A → B)) LS3
4. ((¬¬(A → B) → ¬¬A) → (¬A → ¬(A → B))) → (¬¬A → ((¬¬(A → B) → ¬¬A) → (¬A → ¬(A → B)))) LS1
5. ¬¬A → ((¬¬(A → B) → ¬¬A) → (¬A → ¬(A → B))) MP 4,3
6. (¬¬A → ((¬¬(A → B) → ¬¬A) → (¬A → ¬(A → B)))) → ((¬¬A → (¬¬(A → B) → ¬¬A)) → (¬¬A → (¬A → ¬(A → B)))) LS2
7. (¬¬A → (¬¬(A → B) → ¬¬A)) → (¬¬A → (¬A → ¬(A → B))) MP 6,5
8. ¬¬A → (¬A → ¬(A → B)) MP 7,2
9. (¬A → ¬(A → B)) → ((A → B) → A) LS3
10. ((¬A → ¬(A → B)) → ((A → B) → A)) → (¬¬A → ((¬A → ¬(A → B)) → ((A → B) → A))) LS1
11. ¬¬A → ((¬A → ¬(A → B)) → ((A → B) → A)) MP 10,9
12. (¬¬A → ((¬A → ¬(A → B)) → ((A → B) → A))) → ((¬¬A → (¬A → ¬(A → B))) → (¬¬A → ((A → B) → A))) LS2
13. (¬¬A → (¬A → ¬(A → B))) → (¬¬A → ((A → B) → A)) MP 12,11
14. ¬¬A → ((A → B) → A) MP 13,8
15. (¬¬A → ((A → B) → A)) → ((¬¬A → (A → B)) → (¬¬A → A)) LS2
16. (¬¬A → (A → B)) → (¬¬A → A) MP 15,14
17. (¬¬A → (A → B)) → ((¬¬A → A) → (¬¬A → B)) LS2
18. ((¬¬A → (A → B)) → ((¬¬A → A) → (¬¬A → B))) → (((¬¬A → (A → B)) → (¬¬A → A)) → ((¬¬A → (A → B)) → (¬¬A → B))) LS2
19. ((¬¬A → (A → B)) → (¬¬A → A)) → ((¬¬A → (A → B)) → (¬¬A → B)) MP 18,17
20. (¬¬A → (A → B)) → (¬¬A → B) MP 19,16
21. ((¬¬A → (A → B)) → (¬¬A → B)) → ((A → B) → ((¬¬A → (A → B)) → (¬¬A → B))) LS1
22. (A → B) → ((¬¬A → (A → B)) → (¬¬A → B)) MP 21,20
23. ((A → B) → ((¬¬A → (A → B)) → (¬¬A → B))) → (((A → B) → (¬¬A → (A → B))) → ((A → B) → (¬¬A → B))) LS2
24. ((A → B) → (¬¬A → (A → B))) → ((A → B) → (¬¬A → B)) MP 23,22
25. (A → B) → (¬¬A → B) MP 24,1
26. (¬¬A → B) → (¬B → (¬¬A → B)) LS1
27. ((¬¬A → B) → (¬B → (¬¬A → B))) → ((A → B) → ((¬¬A → B) → (¬B → (¬¬A → B)))) LS1
28. (A → B) → ((¬¬A → B) → (¬B → (¬¬A → B))) MP 27,26
29. ((A → B) → ((¬¬A → B) → (¬B → (¬¬A → B)))) → (((A → B) → (¬¬A → B)) → ((A → B) → (¬B → (¬¬A → B)))) LS2
30. ((A → B) → (¬¬A → B)) → ((A → B) → (¬B → (¬¬A → B))) MP 29,28
31. (A → B) → (¬B → (¬¬A → B)) MP 30,25
32. ¬B → (¬¬(¬¬A → (¬¬(A → B) → ¬¬A)) → ¬B) LS1
33. (¬¬(¬¬A → (¬¬(A → B) → ¬¬A)) → ¬B) → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) LS3
34. ((¬¬(¬¬A → (¬¬(A → B) → ¬¬A)) → ¬B) → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) → (¬B → ((¬¬(¬¬A → (¬¬(A → B) → ¬¬A)) → ¬B) → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) LS1
35. ¬B → ((¬¬(¬¬A → (¬¬(A → B) → ¬¬A)) → ¬B) → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) MP 34,33
36. (¬B → ((¬¬(¬¬A → (¬¬(A → B) → ¬¬A)) → ¬B) → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) → ((¬B → (¬¬(¬¬A → (¬¬(A → B) → ¬¬A)) → ¬B)) → (¬B → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) LS2
37. (¬B → (¬¬(¬¬A → (¬¬(A → B) → ¬¬A)) → ¬B)) → (¬B → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) MP 36,35
38. ¬B → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) MP 37,32
39. (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → (¬¬A → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) LS1
40. ((B → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → (¬¬A → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) → (¬B → ((B → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → (¬¬A → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))))) LS1
41. ¬B → ((B → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → (¬¬A → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) MP 40,39
42. (¬B → ((B → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → (¬¬A → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))))) → ((¬B → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) → (¬B → (¬¬A → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))))) LS2
43. (¬B → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) → (¬B → (¬¬A → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) MP 42,41
44. ¬B → (¬¬A → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) MP 43,38
45. (¬¬A → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) → ((¬¬A → B) → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) LS2
46. ((¬¬A → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) → ((¬¬A → B) → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) → (¬B → ((¬¬A → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) → ((¬¬A → B) → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))))) LS1
47. ¬B → ((¬¬A → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) → ((¬¬A → B) → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) MP 46,45
48. (¬B → ((¬¬A → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) → ((¬¬A → B) → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))))) → ((¬B → (¬¬A → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) → (¬B → ((¬¬A → B) → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))))) LS2
49. (¬B → (¬¬A → (B → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) → (¬B → ((¬¬A → B) → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) MP 48,47
50. ¬B → ((¬¬A → B) → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) MP 49,44
51. (¬B → ((¬¬A → B) → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) → ((¬B → (¬¬A → B)) → (¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) LS2
52. (¬B → (¬¬A → B)) → (¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) MP 51,50
53. ((¬B → (¬¬A → B)) → (¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) → ((A → B) → ((¬B → (¬¬A → B)) → (¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))))) LS1
54. (A → B) → ((¬B → (¬¬A → B)) → (¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) MP 53,52
55. ((A → B) → ((¬B → (¬¬A → B)) → (¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))))) → (((A → B) → (¬B → (¬¬A → B))) → ((A → B) → (¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))))) LS2
56. ((A → B) → (¬B → (¬¬A → B))) → ((A → B) → (¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) MP 55,54
57. (A → B) → (¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) MP 56,31
58. (¬¬A → (¬¬(A → B) → ¬¬A)) → ((¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → (¬¬A → (¬¬(A → B) → ¬¬A))) LS1
59. (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → (¬¬A → (¬¬(A → B) → ¬¬A)) MP 58,2
60. (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → ((¬¬A → (¬¬(A → B) → ¬¬A)) → ¬A) LS3
61. ((¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → ((¬¬A → (¬¬(A → B) → ¬¬A)) → ¬A)) → (((¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → (¬¬A → (¬¬(A → B) → ¬¬A))) → ((¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → ¬A)) LS2
62. ((¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → (¬¬A → (¬¬(A → B) → ¬¬A))) → ((¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → ¬A) MP 61,60
63. (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → ¬A MP 62,59
64. ((¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → ¬A) → (¬B → ((¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → ¬A)) LS1
65. ¬B → ((¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → ¬A) MP 64,63
66. (¬B → ((¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))) → ¬A)) → ((¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) → (¬B → ¬A)) LS2
67. (¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) → (¬B → ¬A) MP 66,65
68. ((¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) → (¬B → ¬A)) → ((A → B) → ((¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) → (¬B → ¬A))) LS1
69. (A → B) → ((¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) → (¬B → ¬A)) MP 68,67
70. ((A → B) → ((¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A)))) → (¬B → ¬A))) → (((A → B) → (¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) → ((A → B) → (¬B → ¬A))) LS2
71. ((A → B) → (¬B → (¬¬A → ¬(¬¬A → (¬¬(A → B) → ¬¬A))))) → ((A → B) → (¬B → ¬A)) MP 70,69
72. (A → B) → (¬B → ¬A) MP 71,57

Try it online!
> 28 votes

# Answer
# 91 Steps

Full Proof:

```
1. (A → B) → (¬¬A → (A → B)) LS1
2. (¬¬A → (A → B)) → ((¬¬A → A) → (¬¬A → B)) LS2
3. ((¬¬A → (A → B)) → ((¬¬A → A) → (¬¬A → B))) → ((A → B) → ((¬¬A → (A → B)) → ((¬¬A → A) → (¬¬A → B)))) LS1
4. (A → B) → ((¬¬A → (A → B)) → ((¬¬A → A) → (¬¬A → B))) MP 3,2
5. ((A → B) → ((¬¬A → (A → B)) → ((¬¬A → A) → (¬¬A → B)))) → (((A → B) → (¬¬A → (A → B))) → ((A → B) → ((¬¬A → A) → (¬¬A → B)))) LS2
6. ((A → B) → (¬¬A → (A → B))) → ((A → B) → ((¬¬A → A) → (¬¬A → B))) MP 5,4
7. (A → B) → ((¬¬A → A) → (¬¬A → B)) MP 6,1
8. ¬A → (¬¬(B → (¬A → A)) → ¬A) LS1
9. (¬¬(B → (¬A → A)) → ¬A) → (A → ¬(B → (¬A → A))) LS3
10. ((¬¬(B → (¬A → A)) → ¬A) → (A → ¬(B → (¬A → A)))) → (¬A → ((¬¬(B → (¬A → A)) → ¬A) → (A → ¬(B → (¬A → A))))) LS1
11. ¬A → ((¬¬(B → (¬A → A)) → ¬A) → (A → ¬(B → (¬A → A)))) MP 10,9
12. (¬A → ((¬¬(B → (¬A → A)) → ¬A) → (A → ¬(B → (¬A → A))))) → ((¬A → (¬¬(B → (¬A → A)) → ¬A)) → (¬A → (A → ¬(B → (¬A → A))))) LS2
13. (¬A → (¬¬(B → (¬A → A)) → ¬A)) → (¬A → (A → ¬(B → (¬A → A)))) MP 12,11
14. ¬A → (A → ¬(B → (¬A → A))) MP 13,8
15. (¬A → (A → ¬(B → (¬A → A)))) → ((¬A → A) → (¬A → ¬(B → (¬A → A)))) LS2
16. (¬A → A) → (¬A → ¬(B → (¬A → A))) MP 15,14
17. (¬A → ¬(B → (¬A → A))) → ((B → (¬A → A)) → A) LS3
18. ((¬A → ¬(B → (¬A → A))) → ((B → (¬A → A)) → A)) → ((¬A → A) → ((¬A → ¬(B → (¬A → A))) → ((B → (¬A → A)) → A))) LS1
19. (¬A → A) → ((¬A → ¬(B → (¬A → A))) → ((B → (¬A → A)) → A)) MP 18,17
20. ((¬A → A) → ((¬A → ¬(B → (¬A → A))) → ((B → (¬A → A)) → A))) → (((¬A → A) → (¬A → ¬(B → (¬A → A)))) → ((¬A → A) → ((B → (¬A → A)) → A))) LS2
21. ((¬A → A) → (¬A → ¬(B → (¬A → A)))) → ((¬A → A) → ((B → (¬A → A)) → A)) MP 20,19
22. (¬A → A) → ((B → (¬A → A)) → A) MP 21,16
23. (¬A → A) → (B → (¬A → A)) LS1
24. ((¬A → A) → ((B → (¬A → A)) → A)) → (((¬A → A) → (B → (¬A → A))) → ((¬A → A) → A)) LS2
25. ((¬A → A) → (B → (¬A → A))) → ((¬A → A) → A) MP 24,22
26. (¬A → A) → A MP 25,23
27. ¬¬A → (¬A → ¬¬A) LS1
28. (¬A → ¬¬A) → (¬A → A) LS3
29. ((¬A → ¬¬A) → (¬A → A)) → (¬¬A → ((¬A → ¬¬A) → (¬A → A))) LS1
30. ¬¬A → ((¬A → ¬¬A) → (¬A → A)) MP 29,28
31. (¬¬A → ((¬A → ¬¬A) → (¬A → A))) → ((¬¬A → (¬A → ¬¬A)) → (¬¬A → (¬A → A))) LS2
32. (¬¬A → (¬A → ¬¬A)) → (¬¬A → (¬A → A)) MP 31,30
33. ¬¬A → (¬A → A) MP 32,27
34. ((¬A → A) → A) → (¬¬A → ((¬A → A) → A)) LS1
35. ¬¬A → ((¬A → A) → A) MP 34,26
36. (¬¬A → ((¬A → A) → A)) → ((¬¬A → (¬A → A)) → (¬¬A → A)) LS2
37. (¬¬A → (¬A → A)) → (¬¬A → A) MP 36,35
38. ¬¬A → A MP 37,33
39. (¬¬A → A) → ((A → B) → (¬¬A → A)) LS1
40. (A → B) → (¬¬A → A) MP 39,38
41. ((A → B) → ((¬¬A → A) → (¬¬A → B))) → (((A → B) → (¬¬A → A)) → ((A → B) → (¬¬A → B))) LS2
42. ((A → B) → (¬¬A → A)) → ((A → B) → (¬¬A → B)) MP 41,7
43. (A → B) → (¬¬A → B) MP 42,40
44. ¬¬B → (¬¬(B → (¬¬B → ¬B)) → ¬¬B) LS1
45. (¬¬(B → (¬¬B → ¬B)) → ¬¬B) → (¬B → ¬(B → (¬¬B → ¬B))) LS3
46. ((¬¬(B → (¬¬B → ¬B)) → ¬¬B) → (¬B → ¬(B → (¬¬B → ¬B)))) → (¬¬B → ((¬¬(B → (¬¬B → ¬B)) → ¬¬B) → (¬B → ¬(B → (¬¬B → ¬B))))) LS1
47. ¬¬B → ((¬¬(B → (¬¬B → ¬B)) → ¬¬B) → (¬B → ¬(B → (¬¬B → ¬B)))) MP 46,45
48. (¬¬B → ((¬¬(B → (¬¬B → ¬B)) → ¬¬B) → (¬B → ¬(B → (¬¬B → ¬B))))) → ((¬¬B → (¬¬(B → (¬¬B → ¬B)) → ¬¬B)) → (¬¬B → (¬B → ¬(B → (¬¬B → ¬B))))) LS2
49. (¬¬B → (¬¬(B → (¬¬B → ¬B)) → ¬¬B)) → (¬¬B → (¬B → ¬(B → (¬¬B → ¬B)))) MP 48,47
50. ¬¬B → (¬B → ¬(B → (¬¬B → ¬B))) MP 49,44
51. (¬¬B → (¬B → ¬(B → (¬¬B → ¬B)))) → ((¬¬B → ¬B) → (¬¬B → ¬(B → (¬¬B → ¬B)))) LS2
52. (¬¬B → ¬B) → (¬¬B → ¬(B → (¬¬B → ¬B))) MP 51,50
53. (¬¬B → ¬(B → (¬¬B → ¬B))) → ((B → (¬¬B → ¬B)) → ¬B) LS3
54. ((¬¬B → ¬(B → (¬¬B → ¬B))) → ((B → (¬¬B → ¬B)) → ¬B)) → ((¬¬B → ¬B) → ((¬¬B → ¬(B → (¬¬B → ¬B))) → ((B → (¬¬B → ¬B)) → ¬B))) LS1
55. (¬¬B → ¬B) → ((¬¬B → ¬(B → (¬¬B → ¬B))) → ((B → (¬¬B → ¬B)) → ¬B)) MP 54,53
56. ((¬¬B → ¬B) → ((¬¬B → ¬(B → (¬¬B → ¬B))) → ((B → (¬¬B → ¬B)) → ¬B))) → (((¬¬B → ¬B) → (¬¬B → ¬(B → (¬¬B → ¬B)))) → ((¬¬B → ¬B) → ((B → (¬¬B → ¬B)) → ¬B))) LS2
57. ((¬¬B → ¬B) → (¬¬B → ¬(B → (¬¬B → ¬B)))) → ((¬¬B → ¬B) → ((B → (¬¬B → ¬B)) → ¬B)) MP 56,55
58. (¬¬B → ¬B) → ((B → (¬¬B → ¬B)) → ¬B) MP 57,52
59. (¬¬B → ¬B) → (B → (¬¬B → ¬B)) LS1
60. ((¬¬B → ¬B) → ((B → (¬¬B → ¬B)) → ¬B)) → (((¬¬B → ¬B) → (B → (¬¬B → ¬B))) → ((¬¬B → ¬B) → ¬B)) LS2
61. ((¬¬B → ¬B) → (B → (¬¬B → ¬B))) → ((¬¬B → ¬B) → ¬B) MP 60,58
62. (¬¬B → ¬B) → ¬B MP 61,59
63. ¬¬¬B → (¬¬B → ¬¬¬B) LS1
64. (¬¬B → ¬¬¬B) → (¬¬B → ¬B) LS3
65. ((¬¬B → ¬¬¬B) → (¬¬B → ¬B)) → (¬¬¬B → ((¬¬B → ¬¬¬B) → (¬¬B → ¬B))) LS1
66. ¬¬¬B → ((¬¬B → ¬¬¬B) → (¬¬B → ¬B)) MP 65,64
67. (¬¬¬B → ((¬¬B → ¬¬¬B) → (¬¬B → ¬B))) → ((¬¬¬B → (¬¬B → ¬¬¬B)) → (¬¬¬B → (¬¬B → ¬B))) LS2
68. (¬¬¬B → (¬¬B → ¬¬¬B)) → (¬¬¬B → (¬¬B → ¬B)) MP 67,66
69. ¬¬¬B → (¬¬B → ¬B) MP 68,63
70. ((¬¬B → ¬B) → ¬B) → (¬¬¬B → ((¬¬B → ¬B) → ¬B)) LS1
71. ¬¬¬B → ((¬¬B → ¬B) → ¬B) MP 70,62
72. (¬¬¬B → ((¬¬B → ¬B) → ¬B)) → ((¬¬¬B → (¬¬B → ¬B)) → (¬¬¬B → ¬B)) LS2
73. (¬¬¬B → (¬¬B → ¬B)) → (¬¬¬B → ¬B) MP 72,71
74. ¬¬¬B → ¬B MP 73,69
75. (¬¬¬B → ¬B) → (B → ¬¬B) LS3
76. B → ¬¬B MP 75,74
77. (B → ¬¬B) → (¬¬A → (B → ¬¬B)) LS1
78. ¬¬A → (B → ¬¬B) MP 77,76
79. (¬¬A → (B → ¬¬B)) → ((¬¬A → B) → (¬¬A → ¬¬B)) LS2
80. (¬¬A → B) → (¬¬A → ¬¬B) MP 79,78
81. ((¬¬A → B) → (¬¬A → ¬¬B)) → ((A → B) → ((¬¬A → B) → (¬¬A → ¬¬B))) LS1
82. (A → B) → ((¬¬A → B) → (¬¬A → ¬¬B)) MP 81,80
83. ((A → B) → ((¬¬A → B) → (¬¬A → ¬¬B))) → (((A → B) → (¬¬A → B)) → ((A → B) → (¬¬A → ¬¬B))) LS2
84. ((A → B) → (¬¬A → B)) → ((A → B) → (¬¬A → ¬¬B)) MP 83,82
85. (A → B) → (¬¬A → ¬¬B) MP 84,43
86. (¬¬A → ¬¬B) → (¬B → ¬A) LS3
87. ((¬¬A → ¬¬B) → (¬B → ¬A)) → ((A → B) → ((¬¬A → ¬¬B) → (¬B → ¬A))) LS1
88. (A → B) → ((¬¬A → ¬¬B) → (¬B → ¬A)) MP 87,86
89. ((A → B) → ((¬¬A → ¬¬B) → (¬B → ¬A))) → (((A → B) → (¬¬A → ¬¬B)) → ((A → B) → (¬B → ¬A))) LS2
90. ((A → B) → (¬¬A → ¬¬B)) → ((A → B) → (¬B → ¬A)) MP 89,88
91. (A → B) → (¬B → ¬A) MP 90,85

```

Try it online!

A more human-readable version using 5 lemmas:

```
Lemma 1: From A → B and B → C, instantiate A → C. (5 steps)

1. B → C                                         given
2. (B → C) → (A → (B → C))                       L.S.1
3. A → (B → C)                                   M.P. (1,2)
4. (A → (B → C)) → ((A → B) → (A → C))           L.S.2
5. (A → B) → (A → C)                             M.P. (3,4)
6. A → B                                         given
7. A → C                                         M.P. (6,5)

Lemma 2: ¬A → (A → B) (7 steps)

1. ¬A → (¬B → ¬A)                                L.S.1
2. (¬B → ¬A) → (A → B)                           L.S.3
3. ¬A → (A → B)                                  Lemma 1 (1,2)

Lemma 3: From A → (B → C) and A → B, instantiate A → C. (3 steps)

1. A → (B → C)                                   given
2. (A → (B → C)) → ((A → B) → (A → C))           L.S.2
3. (A → B) → (A → C)                             M.P. (1,2)
4. A → B                                         given
5. A → C                                         M.P. (4,3)

Lemma 4: ¬¬A → A (31 steps)

1. ¬A → (A → ¬(B → (¬A → A)))                    Lemma 2
2. (¬A → (A → ¬(B → (¬A → A)))) →
   ((¬A → A) → (¬A → ¬(B → (¬A → A))))           L.S.2
3. (¬A → A) → (¬A → ¬(B → (¬A → A)))             M.P. (1,2)
4. (¬A → ¬(B → (¬A → A))) →((B → (¬A → A)) → A)  L.S.3
5. (¬A → A) → ((B → (¬A → A)) → A)               Lemma 1 (3,4)
6. (¬A → A) → (B → (¬A → A))                     L.S.1
7. (¬A → A) → A                                  Lemma 3 (5,6)
8. ¬¬A → (¬A → A)                                Lemma 2
9. ¬¬A → A                                       Lemma 1 (8,7)

Lemma 5: (A → B) → (¬¬A → B) (43 steps)

1. (A → B) → (¬¬A → (A → B))                     L.S.1
2. (¬¬A → (A → B)) → ((¬¬A → A) → (¬¬A → B))     L.S.2
3. (A → B) → ((¬¬A → A) → (¬¬A → B))             Lemma 1 (1,2)
4. ¬¬A → A                                       Lemma 4
5. (¬¬A → A) → ((A → B) → (¬¬A → A))             L.S.1
6. (A → B) → (¬¬A → A)                           M.P. (4,5)
7. (A → B) → (¬¬A → B)                           Lemma 3 (3,6)

Theorem: (A → B) → (¬B → ¬A)

1. (A → B) → (¬¬A → B)                           Lemma 5
2. ¬¬¬B → ¬B                                     Lemma 4
3. (¬¬¬B → ¬B) → (B → ¬¬B)                       L.S.3
4. B → ¬¬B                                       M.P. (2,3)
5. (B → ¬¬B) → (¬¬A → (B → ¬¬B))                 L.S.1
6. ¬¬A → (B → ¬¬B)                               M.P. (4,5)
7. (¬¬A → (B → ¬¬B)) → ((¬¬A → B) → (¬¬A → ¬¬B)) L.S.2
8. (¬¬A → B) → (¬¬A → ¬¬B)                       M.P. (6,7)
9. (A → B) → (¬¬A → ¬¬B)                         Lemma 1 (1,8)
10.(¬¬A → ¬¬B) → (¬B → ¬A)                       L.S.3
11.(A → B) → (¬B → ¬A)                           Lemma 1 (9,10)

```
> 26 votes

# Answer
**59 steps**

Norman Megill, author of Metamath has told me about a 59 step proof, which I'm going to post here in this community wiki. The original can be found in theorem 2.16 on this page.

http://us.metamath.org/mmsolitaire/pmproofs.txt

Norm says: This page would provide plenty of challenges for you to beat!

Here's the proof

```
((P -> Q) -> (~ Q -> ~ P)); ! *2.16
((P -> Q) -> (~ Q -> ~ P)); ! Result of proof
DD2D1DD2D13DD2D1DD22D2DD2D13DD2D1311D2D1D3DD2DD2D13DD2D1311
; ! 59 steps

```

The proof is in Polish notation, so it starts from the conclusion and continues backwards until every term has been satisified by an axiom. The character mapping is as follows: "1" is LS axiom 1, "2" is LS axiom 2, "3" is LS axiom 3, and "D" is Modus Ponens.

Here's the proof in @W-W's suggested format

```
01 ax-1          $a |- ( ¬ ¬ ¬ B → ( ¬ B → ¬ ¬ ¬ B ) )
02 ax-1          $a |- ( ¬ ¬ ¬ B → ( ¬ ¬ ( ¬ B → ¬ ¬ ¬ B ) → ¬ ¬ ¬ B ) )
03 ax-3          $a |- ( ( ¬ ¬ ( ¬ B → ¬ ¬ ¬ B ) → ¬ ¬ ¬ B ) → ( ¬ ¬ B → ¬ ( ¬ B → ¬ ¬ ¬ B ) ) )
04 ax-1          $a |- ( ( ( ¬ ¬ ( ¬ B → ¬ ¬ ¬ B ) → ¬ ¬ ¬ B ) → ( ¬ ¬ B → ¬ ( ¬ B → ¬ ¬ ¬ B ) ) ) → ( ¬ ¬ ¬ B → ( ( ¬ ¬ ( ¬ B → ¬ ¬ ¬ B ) → ¬ ¬ ¬ B ) → ( ¬ ¬ B → ¬ ( ¬ B → ¬ ¬ ¬ B ) ) ) ) )
05 3,4 ax-mp     $a |- ( ¬ ¬ ¬ B → ( ( ¬ ¬ ( ¬ B → ¬ ¬ ¬ B ) → ¬ ¬ ¬ B ) → ( ¬ ¬ B → ¬ ( ¬ B → ¬ ¬ ¬ B ) ) ) )
06 ax-2          $a |- ( ( ¬ ¬ ¬ B → ( ( ¬ ¬ ( ¬ B → ¬ ¬ ¬ B ) → ¬ ¬ ¬ B ) → ( ¬ ¬ B → ¬ ( ¬ B → ¬ ¬ ¬ B ) ) ) ) → ( ( ¬ ¬ ¬ B → ( ¬ ¬ ( ¬ B → ¬ ¬ ¬ B ) → ¬ ¬ ¬ B ) ) → ( ¬ ¬ ¬ B → ( ¬ ¬ B → ¬ ( ¬ B → ¬ ¬ ¬ B ) ) ) ) )
07 5,6 ax-mp     $a |- ( ( ¬ ¬ ¬ B → ( ¬ ¬ ( ¬ B → ¬ ¬ ¬ B ) → ¬ ¬ ¬ B ) ) → ( ¬ ¬ ¬ B → ( ¬ ¬ B → ¬ ( ¬ B → ¬ ¬ ¬ B ) ) ) )
08 2,7 ax-mp     $a |- ( ¬ ¬ ¬ B → ( ¬ ¬ B → ¬ ( ¬ B → ¬ ¬ ¬ B ) ) )
09 ax-3          $a |- ( ( ¬ ¬ B → ¬ ( ¬ B → ¬ ¬ ¬ B ) ) → ( ( ¬ B → ¬ ¬ ¬ B ) → ¬ B ) )
10 ax-1          $a |- ( ( ( ¬ ¬ B → ¬ ( ¬ B → ¬ ¬ ¬ B ) ) → ( ( ¬ B → ¬ ¬ ¬ B ) → ¬ B ) ) → ( ¬ ¬ ¬ B → ( ( ¬ ¬ B → ¬ ( ¬ B → ¬ ¬ ¬ B ) ) → ( ( ¬ B → ¬ ¬ ¬ B ) → ¬ B ) ) ) )
11 9,10 ax-mp    $a |- ( ¬ ¬ ¬ B → ( ( ¬ ¬ B → ¬ ( ¬ B → ¬ ¬ ¬ B ) ) → ( ( ¬ B → ¬ ¬ ¬ B ) → ¬ B ) ) )
12 ax-2          $a |- ( ( ¬ ¬ ¬ B → ( ( ¬ ¬ B → ¬ ( ¬ B → ¬ ¬ ¬ B ) ) → ( ( ¬ B → ¬ ¬ ¬ B ) → ¬ B ) ) ) → ( ( ¬ ¬ ¬ B → ( ¬ ¬ B → ¬ ( ¬ B → ¬ ¬ ¬ B ) ) ) → ( ¬ ¬ ¬ B → ( ( ¬ B → ¬ ¬ ¬ B ) → ¬ B ) ) ) )
13 11,12 ax-mp   $a |- ( ( ¬ ¬ ¬ B → ( ¬ ¬ B → ¬ ( ¬ B → ¬ ¬ ¬ B ) ) ) → ( ¬ ¬ ¬ B → ( ( ¬ B → ¬ ¬ ¬ B ) → ¬ B ) ) )
14 8,13 ax-mp    $a |- ( ¬ ¬ ¬ B → ( ( ¬ B → ¬ ¬ ¬ B ) → ¬ B ) )
15 ax-2          $a |- ( ( ¬ ¬ ¬ B → ( ( ¬ B → ¬ ¬ ¬ B ) → ¬ B ) ) → ( ( ¬ ¬ ¬ B → ( ¬ B → ¬ ¬ ¬ B ) ) → ( ¬ ¬ ¬ B → ¬ B ) ) )
16 14,15 ax-mp   $a |- ( ( ¬ ¬ ¬ B → ( ¬ B → ¬ ¬ ¬ B ) ) → ( ¬ ¬ ¬ B → ¬ B ) )
17 1,16 ax-mp    $a |- ( ¬ ¬ ¬ B → ¬ B )
18 ax-3          $a |- ( ( ¬ ¬ ¬ B → ¬ B ) → ( B → ¬ ¬ B ) )
19 17,18 ax-mp   $a |- ( B → ¬ ¬ B )
20 ax-1          $a |- ( ( B → ¬ ¬ B ) → ( A → ( B → ¬ ¬ B ) ) )
21 19,20 ax-mp   $a |- ( A → ( B → ¬ ¬ B ) )
22 ax-2          $a |- ( ( A → ( B → ¬ ¬ B ) ) → ( ( A → B ) → ( A → ¬ ¬ B ) ) )
23 21,22 ax-mp   $a |- ( ( A → B ) → ( A → ¬ ¬ B ) )
24 ax-1          $a |- ( ( A → ¬ ¬ B ) → ( ¬ ¬ A → ( A → ¬ ¬ B ) ) )
25 ax-1          $a |- ( ¬ ¬ A → ( ¬ ¬ ( A → ¬ ¬ B ) → ¬ ¬ A ) )
26 ax-3          $a |- ( ( ¬ ¬ ( A → ¬ ¬ B ) → ¬ ¬ A ) → ( ¬ A → ¬ ( A → ¬ ¬ B ) ) )
27 ax-1          $a |- ( ( ( ¬ ¬ ( A → ¬ ¬ B ) → ¬ ¬ A ) → ( ¬ A → ¬ ( A → ¬ ¬ B ) ) ) → ( ¬ ¬ A → ( ( ¬ ¬ ( A → ¬ ¬ B ) → ¬ ¬ A ) → ( ¬ A → ¬ ( A → ¬ ¬ B ) ) ) ) )
28 26,27 ax-mp   $a |- ( ¬ ¬ A → ( ( ¬ ¬ ( A → ¬ ¬ B ) → ¬ ¬ A ) → ( ¬ A → ¬ ( A → ¬ ¬ B ) ) ) )
29 ax-2          $a |- ( ( ¬ ¬ A → ( ( ¬ ¬ ( A → ¬ ¬ B ) → ¬ ¬ A ) → ( ¬ A → ¬ ( A → ¬ ¬ B ) ) ) ) → ( ( ¬ ¬ A → ( ¬ ¬ ( A → ¬ ¬ B ) → ¬ ¬ A ) ) → ( ¬ ¬ A → ( ¬ A → ¬ ( A → ¬ ¬ B ) ) ) ) )
30 28,29 ax-mp   $a |- ( ( ¬ ¬ A → ( ¬ ¬ ( A → ¬ ¬ B ) → ¬ ¬ A ) ) → ( ¬ ¬ A → ( ¬ A → ¬ ( A → ¬ ¬ B ) ) ) )
31 25,30 ax-mp   $a |- ( ¬ ¬ A → ( ¬ A → ¬ ( A → ¬ ¬ B ) ) )
32 ax-3          $a |- ( ( ¬ A → ¬ ( A → ¬ ¬ B ) ) → ( ( A → ¬ ¬ B ) → A ) )
33 ax-1          $a |- ( ( ( ¬ A → ¬ ( A → ¬ ¬ B ) ) → ( ( A → ¬ ¬ B ) → A ) ) → ( ¬ ¬ A → ( ( ¬ A → ¬ ( A → ¬ ¬ B ) ) → ( ( A → ¬ ¬ B ) → A ) ) ) )
34 32,33 ax-mp   $a |- ( ¬ ¬ A → ( ( ¬ A → ¬ ( A → ¬ ¬ B ) ) → ( ( A → ¬ ¬ B ) → A ) ) )
35 ax-2          $a |- ( ( ¬ ¬ A → ( ( ¬ A → ¬ ( A → ¬ ¬ B ) ) → ( ( A → ¬ ¬ B ) → A ) ) ) → ( ( ¬ ¬ A → ( ¬ A → ¬ ( A → ¬ ¬ B ) ) ) → ( ¬ ¬ A → ( ( A → ¬ ¬ B ) → A ) ) ) )
36 34,35 ax-mp   $a |- ( ( ¬ ¬ A → ( ¬ A → ¬ ( A → ¬ ¬ B ) ) ) → ( ¬ ¬ A → ( ( A → ¬ ¬ B ) → A ) ) )
37 31,36 ax-mp   $a |- ( ¬ ¬ A → ( ( A → ¬ ¬ B ) → A ) )
38 ax-2          $a |- ( ( ¬ ¬ A → ( ( A → ¬ ¬ B ) → A ) ) → ( ( ¬ ¬ A → ( A → ¬ ¬ B ) ) → ( ¬ ¬ A → A ) ) )
39 37,38 ax-mp   $a |- ( ( ¬ ¬ A → ( A → ¬ ¬ B ) ) → ( ¬ ¬ A → A ) )
40 ax-2          $a |- ( ( ¬ ¬ A → ( A → ¬ ¬ B ) ) → ( ( ¬ ¬ A → A ) → ( ¬ ¬ A → ¬ ¬ B ) ) )
41 ax-2          $a |- ( ( ( ¬ ¬ A → ( A → ¬ ¬ B ) ) → ( ( ¬ ¬ A → A ) → ( ¬ ¬ A → ¬ ¬ B ) ) ) → ( ( ( ¬ ¬ A → ( A → ¬ ¬ B ) ) → ( ¬ ¬ A → A ) ) → ( ( ¬ ¬ A → ( A → ¬ ¬ B ) ) → ( ¬ ¬ A → ¬ ¬ B ) ) ) )
42 40,41 ax-mp   $a |- ( ( ( ¬ ¬ A → ( A → ¬ ¬ B ) ) → ( ¬ ¬ A → A ) ) → ( ( ¬ ¬ A → ( A → ¬ ¬ B ) ) → ( ¬ ¬ A → ¬ ¬ B ) ) )
43 39,42 ax-mp   $a |- ( ( ¬ ¬ A → ( A → ¬ ¬ B ) ) → ( ¬ ¬ A → ¬ ¬ B ) )
44 ax-1          $a |- ( ( ( ¬ ¬ A → ( A → ¬ ¬ B ) ) → ( ¬ ¬ A → ¬ ¬ B ) ) → ( ( A → ¬ ¬ B ) → ( ( ¬ ¬ A → ( A → ¬ ¬ B ) ) → ( ¬ ¬ A → ¬ ¬ B ) ) ) )
45 43,44 ax-mp   $a |- ( ( A → ¬ ¬ B ) → ( ( ¬ ¬ A → ( A → ¬ ¬ B ) ) → ( ¬ ¬ A → ¬ ¬ B ) ) )
46 ax-2          $a |- ( ( ( A → ¬ ¬ B ) → ( ( ¬ ¬ A → ( A → ¬ ¬ B ) ) → ( ¬ ¬ A → ¬ ¬ B ) ) ) → ( ( ( A → ¬ ¬ B ) → ( ¬ ¬ A → ( A → ¬ ¬ B ) ) ) → ( ( A → ¬ ¬ B ) → ( ¬ ¬ A → ¬ ¬ B ) ) ) )
47 45,46 ax-mp   $a |- ( ( ( A → ¬ ¬ B ) → ( ¬ ¬ A → ( A → ¬ ¬ B ) ) ) → ( ( A → ¬ ¬ B ) → ( ¬ ¬ A → ¬ ¬ B ) ) )
48 24,47 ax-mp   $a |- ( ( A → ¬ ¬ B ) → ( ¬ ¬ A → ¬ ¬ B ) )
49 ax-3          $a |- ( ( ¬ ¬ A → ¬ ¬ B ) → ( ¬ B → ¬ A ) )
50 ax-1          $a |- ( ( ( ¬ ¬ A → ¬ ¬ B ) → ( ¬ B → ¬ A ) ) → ( ( A → ¬ ¬ B ) → ( ( ¬ ¬ A → ¬ ¬ B ) → ( ¬ B → ¬ A ) ) ) )
51 49,50 ax-mp   $a |- ( ( A → ¬ ¬ B ) → ( ( ¬ ¬ A → ¬ ¬ B ) → ( ¬ B → ¬ A ) ) )
52 ax-2          $a |- ( ( ( A → ¬ ¬ B ) → ( ( ¬ ¬ A → ¬ ¬ B ) → ( ¬ B → ¬ A ) ) ) → ( ( ( A → ¬ ¬ B ) → ( ¬ ¬ A → ¬ ¬ B ) ) → ( ( A → ¬ ¬ B ) → ( ¬ B → ¬ A ) ) ) )
53 51,52 ax-mp   $a |- ( ( ( A → ¬ ¬ B ) → ( ¬ ¬ A → ¬ ¬ B ) ) → ( ( A → ¬ ¬ B ) → ( ¬ B → ¬ A ) ) )
54 48,53 ax-mp   $a |- ( ( A → ¬ ¬ B ) → ( ¬ B → ¬ A ) )
55 ax-1          $a |- ( ( ( A → ¬ ¬ B ) → ( ¬ B → ¬ A ) ) → ( ( A → B ) → ( ( A → ¬ ¬ B ) → ( ¬ B → ¬ A ) ) ) )
56 54,55 ax-mp   $a |- ( ( A → B ) → ( ( A → ¬ ¬ B ) → ( ¬ B → ¬ A ) ) )
57 ax-2          $a |- ( ( ( A → B ) → ( ( A → ¬ ¬ B ) → ( ¬ B → ¬ A ) ) ) → ( ( ( A → B ) → ( A → ¬ ¬ B ) ) → ( ( A → B ) → ( ¬ B → ¬ A ) ) ) )
58 56,57 ax-mp   $a |- ( ( ( A → B ) → ( A → ¬ ¬ B ) ) → ( ( A → B ) → ( ¬ B → ¬ A ) ) )
59 23,58 ax-mp   $a |- ( ( A → B ) → ( ¬ B → ¬ A ) )

```

Try it online!

Here it is in The Incredible Proof Machine

png svg
> 19 votes

---
Tags: logic, combinatory-logic, proof-golf
---


title: Quine Relay!

text:
# Question
Title: Quine Relay!
# Quine Relay!

A quine relay of order \\$n\\$ is a series of programs \\$P\_1\dots P\_n\\$ in **distinct** languages such that each program \\$P\_i\\$ outputs \\$P\_{i+1}\\$ and \\$P\_n\\$ outputs \\$P\_1\\$.

For example, a 2-quine relay can be seen here:

### Ruby → Perl 6:

This Ruby program:

```
$><<"print ("
p (DATA.read*2).chars.map(&:ord)
$><<".map: &chr).join"

__END__
$><<"print ("
p (DATA.read*2).chars.map(&:ord)
$><<".map: &chr).join"

__END__

```

outputs this Perl 6 program:

```
print ([36, 62, 60, 60, 34, 112, 114, 105, 110, 116, 32, 40, 34, 10, 112, 32, 40, 68, 65, 84, 65, 46, 114, 101, 97, 100, 42, 50, 41, 46, 99, 104, 97, 114, 115, 46, 109, 97, 112, 40, 38, 58, 111, 114, 100, 41, 10, 36, 62, 60, 60, 34, 46, 109, 97, 112, 58, 32, 38, 99, 104, 114, 41, 46, 106, 111, 105, 110, 34, 10, 10, 95, 95, 69, 78, 68, 95, 95, 10, 36, 62, 60, 60, 34, 112, 114, 105, 110, 116, 32, 40, 34, 10, 112, 32, 40, 68, 65, 84, 65, 46, 114, 101, 97, 100, 42, 50, 41, 46, 99, 104, 97, 114, 115, 46, 109, 97, 112, 40, 38, 58, 111, 114, 100, 41, 10, 36, 62, 60, 60, 34, 46, 109, 97, 112, 58, 32, 38, 99, 104, 114, 41, 46, 106, 111, 105, 110, 34, 10, 10, 95, 95, 69, 78, 68, 95, 95, 10]
.map: &chr).join

```

which in turn outputs the original Ruby program.

# The challenge

The first answer will be an order \\$1\\$ quine relay: a normal quine. It can be in any language.

The next answer will choose a different language and create an order \\$2\\$ quine relay using those two languages.

And so on. The \\$k\\$-th answer will create an order \\$k\\$ quine relay using the previous \\$k - 1\\$ languages and a new, different language.

## Winning

If, after 2 weeks, no more answers have been added, the winner is the person who answered with the highest-order quine relay.

## More rules

* No person may answer twice in a row.
* No person may answer within an hour of their most recent post.
* Languages which differ by version are considered distinct. Thus, Python 2 and Python 3 can both be part of the chain.
* Languages which differ by compiler or interpreter are **not** considered distinct. So, Python 3 (Cython) and Python 3 are considered *interchangeable* but not distinct.
* Each answer may order the languages in any way. If the third answer has an order of Ruby → Perl 6 → JavaScript, you are not restricted to appending a language to the end. If you were adding Java to the mix, then you could write JavaScript → Ruby → Java → Perl 6 if you so desired.

## Format for answers

> # n. Language1 → Language2 → … → Languagen
>
> ## Language1
>
> ```
> language 1 code
>
> ```
>
> ## Language2
>
> ```
> language 2 code
>
> ```
>
> ## Language3
>
> ```
> language 3 code
>
> ```
>
> ## Languagen
>
> ```
> language n code
>
> ```

Feel free to add explanations anywhere in your answers.

# Answer
> 6 votes
# 10. Javascript → Foo → brainfuck → Wumpus → Gol\>\<\> → RAD → \>\<\> → Python 2 → Brain-Flak → 4

Edit: For the next answer, I've golfed how the Python generates the Brain-Flak a bit more here, but I can't be bothered to update the links.

Added Gol\>\<\>. It's just a short `rpH` after the Wumpus code. I've also optimised the brainfuck and Brain-Flak code somewhat. Verification!

# JavaScript (Node.js)

```
(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);b=c(96);x=0;console.log(Q+[...`${Q}99+&;75*1-l2:8-&o@rpH${q}${q+q}rpp>o<def n(x):global l;r=p+p+f+F+P+q+(p+P)*(l-x)+Q+P if x<l else p+p+f+F+P+(p+P)*(x-l)+P;l=x;return r;''a''p=chr(40);P=chr(41);q=chr(91);Q=chr(93);f=chr(123);F=chr(125);l=4;print p+p+p+p+p+p+P+p+P+p+P+P+f+F+P+f+F+p+P+P+f+F+P+f+F+P,str().join(map(n,map(lambda x:ord(x)-48,str().join(map(lambda c:${b}600${b}+(str(c-99).zfill(2)+${b}99500${b}.zfill(10) if c>99 else str(c).zfill(2)+${b}500${b}),[`+[...`(f=${f})()`].map(c=>c.charCodeAt())+`]))[::-1]+${b}99996${b}))),${[...'pppppppPpPPfFpPPfFpPPfFpPPfFPppPpPPfFpPP'].join`+`}${q}`].map(s=>c(43).repeat(Math.max(-x+s.charCodeAt(),0))+c(45).repeat(Math.max(x-(x=s.charCodeAt()),0))+c(46)).join(c()).match(/.{1,253}/g).join(c(10)+Q))})()

```

Try it online!

# Foo

Try it online!

# brainfuck

Try it online!

# Wumpus

```
"99+&;75*1-l2:8-&o@rpH'''rpp>o<def n(x):global l;r=p+p+f+F+P+q+(p+P)*(l-x)+Q+P if x<l else p+p+f+F+P+(p+P)*(x-l)+P;l=x;return r;''a''p=chr(40);P=chr(41);q=chr(91);Q=chr(93);f=chr(123);F=chr(125);l=4;print p+p+p+p+p+p+P+p+P+p+P+P+f+F+P+f+F+p+P+P+f+F+P+f+F+P,str().join(map(n,map(lambda x:ord(x)-48,str().join(map(lambda c:`600`+(str(c-99).zfill(2)+`99500`.zfill(10) if c>99 else str(c).zfill(2)+`500`),[40,102,61,95,61,62,123,113,61,40,99,61,83,116,114,105,110,103,46,102,114,111,109,67,104,97,114,67,111,100,101,41,40,51,57,41,59,81,61,99,40,51,52,41,59,98,61,99,40,57,54,41,59,120,61,48,59,99,111,110,115,111,108,101,46,108,111,103,40,81,43,91,46,46,46,96,36,123,81,125,57,57,43,38,59,55,53,42,49,45,108,50,58,56,45,38,111,64,114,112,72,36,123,113,125,36,123,113,43,113,125,114,112,112,62,111,60,100,101,102,32,110,40,120,41,58,103,108,111,98,97,108,32,108,59,114,61,112,43,112,43,102,43,70,43,80,43,113,43,40,112,43,80,41,42,40,108,45,120,41,43,81,43,80,32,105,102,32,120,60,108,32,101,108,115,101,32,112,43,112,43,102,43,70,43,80,43,40,112,43,80,41,42,40,120,45,108,41,43,80,59,108,61,120,59,114,101,116,117,114,110,32,114,59,39,39,97,39,39,112,61,99,104,114,40,52,48,41,59,80,61,99,104,114,40,52,49,41,59,113,61,99,104,114,40,57,49,41,59,81,61,99,104,114,40,57,51,41,59,102,61,99,104,114,40,49,50,51,41,59,70,61,99,104,114,40,49,50,53,41,59,108,61,52,59,112,114,105,110,116,32,112,43,112,43,112,43,112,43,112,43,112,43,80,43,112,43,80,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,80,44,115,116,114,40,41,46,106,111,105,110,40,109,97,112,40,110,44,109,97,112,40,108,97,109,98,100,97,32,120,58,111,114,100,40,120,41,45,52,56,44,115,116,114,40,41,46,106,111,105,110,40,109,97,112,40,108,97,109,98,100,97,32,99,58,36,123,98,125,54,48,48,36,123,98,125,43,40,115,116,114,40,99,45,57,57,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,57,57,53,48,48,36,123,98,125,46,122,102,105,108,108,40,49,48,41,32,105,102,32,99,62,57,57,32,101,108,115,101,32,115,116,114,40,99,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,53,48,48,36,123,98,125,41,44,91,96,43,91,46,46,46,96,40,102,61,36,123,102,125,41,40,41,96,93,46,109,97,112,40,99,61,62,99,46,99,104,97,114,67,111,100,101,65,116,40,41,41,43,96,93,41,41,91,58,58,45,49,93,43,36,123,98,125,57,57,57,57,54,36,123,98,125,41,41,41,44,36,123,91,46,46,46,39,112,112,112,112,112,112,112,80,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,80,112,112,80,112,80,80,102,70,112,80,80,39,93,46,106,111,105,110,96,43,96,125,36,123,113,125,96,93,46,109,97,112,40,115,61,62,99,40,52,51,41,46,114,101,112,101,97,116,40,77,97,116,104,46,109,97,120,40,45,120,43,115,46,99,104,97,114,67,111,100,101,65,116,40,41,44,48,41,41,43,99,40,52,53,41,46,114,101,112,101,97,116,40,77,97,116,104,46,109,97,120,40,120,45,40,120,61,115,46,99,104,97,114,67,111,100,101,65,116,40,41,41,44,48,41,41,43,99,40,52,54,41,41,46,106,111,105,110,40,99,40,41,41,46,109,97,116,99,104,40,47,46,123,49,44,50,53,51,125,47,103,41,46,106,111,105,110,40,99,40,49,48,41,43,81,41,41,125,41,40,41]))[::-1]+`99996`))),p+p+p+p+p+p+p+P+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+P+p+p+P+p+P+P+f+F+p+P+P'

```

Try it online!

# Gol\>\<\>

```
"rpH'''rpp>o<def n(x):global l;r=p+p+f+F+P+q+(p+P)*(l-x)+Q+P if x<l else p+p+f+F+P+(p+P)*(x-l)+P;l=x;return r;''a''p=chr(40);P=chr(41);q=chr(91);Q=chr(93);f=chr(123);F=chr(125);l=4;print p+p+p+p+p+p+P+p+P+p+P+P+f+F+P+f+F+p+P+P+f+F+P+f+F+P,str().join(map(n,map(lambda x:ord(x)-48,str().join(map(lambda c:`600`+(str(c-99).zfill(2)+`99500`.zfill(10) if c>99 else str(c).zfill(2)+`500`),[40,102,61,95,61,62,123,113,61,40,99,61,83,116,114,105,110,103,46,102,114,111,109,67,104,97,114,67,111,100,101,41,40,51,57,41,59,81,61,99,40,51,52,41,59,98,61,99,40,57,54,41,59,120,61,48,59,99,111,110,115,111,108,101,46,108,111,103,40,81,43,91,46,46,46,96,36,123,81,125,57,57,43,38,59,55,53,42,49,45,108,50,58,56,45,38,111,64,114,112,72,36,123,113,125,36,123,113,43,113,125,114,112,112,62,111,60,100,101,102,32,110,40,120,41,58,103,108,111,98,97,108,32,108,59,114,61,112,43,112,43,102,43,70,43,80,43,113,43,40,112,43,80,41,42,40,108,45,120,41,43,81,43,80,32,105,102,32,120,60,108,32,101,108,115,101,32,112,43,112,43,102,43,70,43,80,43,40,112,43,80,41,42,40,120,45,108,41,43,80,59,108,61,120,59,114,101,116,117,114,110,32,114,59,39,39,97,39,39,112,61,99,104,114,40,52,48,41,59,80,61,99,104,114,40,52,49,41,59,113,61,99,104,114,40,57,49,41,59,81,61,99,104,114,40,57,51,41,59,102,61,99,104,114,40,49,50,51,41,59,70,61,99,104,114,40,49,50,53,41,59,108,61,52,59,112,114,105,110,116,32,112,43,112,43,112,43,112,43,112,43,112,43,80,43,112,43,80,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,80,44,115,116,114,40,41,46,106,111,105,110,40,109,97,112,40,110,44,109,97,112,40,108,97,109,98,100,97,32,120,58,111,114,100,40,120,41,45,52,56,44,115,116,114,40,41,46,106,111,105,110,40,109,97,112,40,108,97,109,98,100,97,32,99,58,36,123,98,125,54,48,48,36,123,98,125,43,40,115,116,114,40,99,45,57,57,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,57,57,53,48,48,36,123,98,125,46,122,102,105,108,108,40,49,48,41,32,105,102,32,99,62,57,57,32,101,108,115,101,32,115,116,114,40,99,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,53,48,48,36,123,98,125,41,44,91,96,43,91,46,46,46,96,40,102,61,36,123,102,125,41,40,41,96,93,46,109,97,112,40,99,61,62,99,46,99,104,97,114,67,111,100,101,65,116,40,41,41,43,96,93,41,41,91,58,58,45,49,93,43,36,123,98,125,57,57,57,57,54,36,123,98,125,41,41,41,44,36,123,91,46,46,46,39,112,112,112,112,112,112,112,80,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,80,112,112,80,112,80,80,102,70,112,80,80,39,93,46,106,111,105,110,96,43,96,125,36,123,113,125,96,93,46,109,97,112,40,115,61,62,99,40,52,51,41,46,114,101,112,101,97,116,40,77,97,116,104,46,109,97,120,40,45,120,43,115,46,99,104,97,114,67,111,100,101,65,116,40,41,44,48,41,41,43,99,40,52,53,41,46,114,101,112,101,97,116,40,77,97,116,104,46,109,97,120,40,120,45,40,120,61,115,46,99,104,97,114,67,111,100,101,65,116,40,41,41,44,48,41,41,43,99,40,52,54,41,41,46,106,111,105,110,40,99,40,41,41,46,109,97,116,99,104,40,47,46,123,49,44,50,53,51,125,47,103,41,46,106,111,105,110,40,99,40,49,48,41,43,81,41,41,125,41,40,41]))[::-1]+`99996`))),p+p+p+p+p+p+p+P+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+P+p+p+P+p+P+P+f+F+p+P+P'

```

Try it online!

# RAD

```
'''rpp>o<def n(x):global l;r=p+p+f+F+P+q+(p+P)*(l-x)+Q+P if x<l else p+p+f+F+P+(p+P)*(x-l)+P;l=x;return r;''a''p=chr(40);P=chr(41);q=chr(91);Q=chr(93);f=chr(123);F=chr(125);l=4;print p+p+p+p+p+p+P+p+P+p+P+P+f+F+P+f+F+p+P+P+f+F+P+f+F+P,str().join(map(n,map(lambda x:ord(x)-48,str().join(map(lambda c:`600`+(str(c-99).zfill(2)+`99500`.zfill(10) if c>99 else str(c).zfill(2)+`500`),[40,102,61,95,61,62,123,113,61,40,99,61,83,116,114,105,110,103,46,102,114,111,109,67,104,97,114,67,111,100,101,41,40,51,57,41,59,81,61,99,40,51,52,41,59,98,61,99,40,57,54,41,59,120,61,48,59,99,111,110,115,111,108,101,46,108,111,103,40,81,43,91,46,46,46,96,36,123,81,125,57,57,43,38,59,55,53,42,49,45,108,50,58,56,45,38,111,64,114,112,72,36,123,113,125,36,123,113,43,113,125,114,112,112,62,111,60,100,101,102,32,110,40,120,41,58,103,108,111,98,97,108,32,108,59,114,61,112,43,112,43,102,43,70,43,80,43,113,43,40,112,43,80,41,42,40,108,45,120,41,43,81,43,80,32,105,102,32,120,60,108,32,101,108,115,101,32,112,43,112,43,102,43,70,43,80,43,40,112,43,80,41,42,40,120,45,108,41,43,80,59,108,61,120,59,114,101,116,117,114,110,32,114,59,39,39,97,39,39,112,61,99,104,114,40,52,48,41,59,80,61,99,104,114,40,52,49,41,59,113,61,99,104,114,40,57,49,41,59,81,61,99,104,114,40,57,51,41,59,102,61,99,104,114,40,49,50,51,41,59,70,61,99,104,114,40,49,50,53,41,59,108,61,52,59,112,114,105,110,116,32,112,43,112,43,112,43,112,43,112,43,112,43,80,43,112,43,80,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,80,44,115,116,114,40,41,46,106,111,105,110,40,109,97,112,40,110,44,109,97,112,40,108,97,109,98,100,97,32,120,58,111,114,100,40,120,41,45,52,56,44,115,116,114,40,41,46,106,111,105,110,40,109,97,112,40,108,97,109,98,100,97,32,99,58,36,123,98,125,54,48,48,36,123,98,125,43,40,115,116,114,40,99,45,57,57,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,57,57,53,48,48,36,123,98,125,46,122,102,105,108,108,40,49,48,41,32,105,102,32,99,62,57,57,32,101,108,115,101,32,115,116,114,40,99,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,53,48,48,36,123,98,125,41,44,91,96,43,91,46,46,46,96,40,102,61,36,123,102,125,41,40,41,96,93,46,109,97,112,40,99,61,62,99,46,99,104,97,114,67,111,100,101,65,116,40,41,41,43,96,93,41,41,91,58,58,45,49,93,43,36,123,98,125,57,57,57,57,54,36,123,98,125,41,41,41,44,36,123,91,46,46,46,39,112,112,112,112,112,112,112,80,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,80,112,112,80,112,80,80,102,70,112,80,80,39,93,46,106,111,105,110,96,43,96,125,36,123,113,125,96,93,46,109,97,112,40,115,61,62,99,40,52,51,41,46,114,101,112,101,97,116,40,77,97,116,104,46,109,97,120,40,45,120,43,115,46,99,104,97,114,67,111,100,101,65,116,40,41,44,48,41,41,43,99,40,52,53,41,46,114,101,112,101,97,116,40,77,97,116,104,46,109,97,120,40,120,45,40,120,61,115,46,99,104,97,114,67,111,100,101,65,116,40,41,41,44,48,41,41,43,99,40,52,54,41,41,46,106,111,105,110,40,99,40,41,41,46,109,97,116,99,104,40,47,46,123,49,44,50,53,51,125,47,103,41,46,106,111,105,110,40,99,40,49,48,41,43,81,41,41,125,41,40,41]))[::-1]+`99996`))),p+p+p+p+p+p+p+P+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+P+p+p+P+p+P+P+f+F+p+P+P'

```

Try it online!

# \>\<\>

```
'rpp>o<def n(x):global l;r=p+p+f+F+P+q+(p+P)*(l-x)+Q+P if x<l else p+p+f+F+P+(p+P)*(x-l)+P;l=x;return r;'a'p=chr(40);P=chr(41);q=chr(91);Q=chr(93);f=chr(123);F=chr(125);l=4;print p+p+p+p+p+p+P+p+P+p+P+P+f+F+P+f+F+p+P+P+f+F+P+f+F+P,str().join(map(n,map(lambda x:ord(x)-48,str().join(map(lambda c:`600`+(str(c-99).zfill(2)+`99500`.zfill(10) if c>99 else str(c).zfill(2)+`500`),[40,102,61,95,61,62,123,113,61,40,99,61,83,116,114,105,110,103,46,102,114,111,109,67,104,97,114,67,111,100,101,41,40,51,57,41,59,81,61,99,40,51,52,41,59,98,61,99,40,57,54,41,59,120,61,48,59,99,111,110,115,111,108,101,46,108,111,103,40,81,43,91,46,46,46,96,36,123,81,125,57,57,43,38,59,55,53,42,49,45,108,50,58,56,45,38,111,64,114,112,72,36,123,113,125,36,123,113,43,113,125,114,112,112,62,111,60,100,101,102,32,110,40,120,41,58,103,108,111,98,97,108,32,108,59,114,61,112,43,112,43,102,43,70,43,80,43,113,43,40,112,43,80,41,42,40,108,45,120,41,43,81,43,80,32,105,102,32,120,60,108,32,101,108,115,101,32,112,43,112,43,102,43,70,43,80,43,40,112,43,80,41,42,40,120,45,108,41,43,80,59,108,61,120,59,114,101,116,117,114,110,32,114,59,39,39,97,39,39,112,61,99,104,114,40,52,48,41,59,80,61,99,104,114,40,52,49,41,59,113,61,99,104,114,40,57,49,41,59,81,61,99,104,114,40,57,51,41,59,102,61,99,104,114,40,49,50,51,41,59,70,61,99,104,114,40,49,50,53,41,59,108,61,52,59,112,114,105,110,116,32,112,43,112,43,112,43,112,43,112,43,112,43,80,43,112,43,80,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,80,44,115,116,114,40,41,46,106,111,105,110,40,109,97,112,40,110,44,109,97,112,40,108,97,109,98,100,97,32,120,58,111,114,100,40,120,41,45,52,56,44,115,116,114,40,41,46,106,111,105,110,40,109,97,112,40,108,97,109,98,100,97,32,99,58,36,123,98,125,54,48,48,36,123,98,125,43,40,115,116,114,40,99,45,57,57,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,57,57,53,48,48,36,123,98,125,46,122,102,105,108,108,40,49,48,41,32,105,102,32,99,62,57,57,32,101,108,115,101,32,115,116,114,40,99,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,53,48,48,36,123,98,125,41,44,91,96,43,91,46,46,46,96,40,102,61,36,123,102,125,41,40,41,96,93,46,109,97,112,40,99,61,62,99,46,99,104,97,114,67,111,100,101,65,116,40,41,41,43,96,93,41,41,91,58,58,45,49,93,43,36,123,98,125,57,57,57,57,54,36,123,98,125,41,41,41,44,36,123,91,46,46,46,39,112,112,112,112,112,112,112,80,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,80,112,112,80,112,80,80,102,70,112,80,80,39,93,46,106,111,105,110,96,43,96,125,36,123,113,125,96,93,46,109,97,112,40,115,61,62,99,40,52,51,41,46,114,101,112,101,97,116,40,77,97,116,104,46,109,97,120,40,45,120,43,115,46,99,104,97,114,67,111,100,101,65,116,40,41,44,48,41,41,43,99,40,52,53,41,46,114,101,112,101,97,116,40,77,97,116,104,46,109,97,120,40,120,45,40,120,61,115,46,99,104,97,114,67,111,100,101,65,116,40,41,41,44,48,41,41,43,99,40,52,54,41,41,46,106,111,105,110,40,99,40,41,41,46,109,97,116,99,104,40,47,46,123,49,44,50,53,51,125,47,103,41,46,106,111,105,110,40,99,40,49,48,41,43,81,41,41,125,41,40,41]))[::-1]+`99996`))),p+p+p+p+p+p+p+P+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+P+p+p+P+p+P+P+f+F+p+P+P

```

Try it online!

# Python 2

```
def n(x):global l;r=p+p+f+F+P+q+(p+P)*(l-x)+Q+P if x<l else p+p+f+F+P+(p+P)*(x-l)+P;l=x;return r;
p=chr(40);P=chr(41);q=chr(91);Q=chr(93);f=chr(123);F=chr(125);l=4;print p+p+p+p+p+p+P+p+P+p+P+P+f+F+P+f+F+p+P+P+f+F+P+f+F+P,str().join(map(n,map(lambda x:ord(x)-48,str().join(map(lambda c:`600`+(str(c-99).zfill(2)+`99500`.zfill(10) if c>99 else str(c).zfill(2)+`500`),[40,102,61,95,61,62,123,113,61,40,99,61,83,116,114,105,110,103,46,102,114,111,109,67,104,97,114,67,111,100,101,41,40,51,57,41,59,81,61,99,40,51,52,41,59,98,61,99,40,57,54,41,59,120,61,48,59,99,111,110,115,111,108,101,46,108,111,103,40,81,43,91,46,46,46,96,36,123,81,125,57,57,43,38,59,55,53,42,49,45,108,50,58,56,45,38,111,64,114,112,72,36,123,113,125,36,123,113,43,113,125,114,112,112,62,111,60,100,101,102,32,110,40,120,41,58,103,108,111,98,97,108,32,108,59,114,61,112,43,112,43,102,43,70,43,80,43,113,43,40,112,43,80,41,42,40,108,45,120,41,43,81,43,80,32,105,102,32,120,60,108,32,101,108,115,101,32,112,43,112,43,102,43,70,43,80,43,40,112,43,80,41,42,40,120,45,108,41,43,80,59,108,61,120,59,114,101,116,117,114,110,32,114,59,39,39,97,39,39,112,61,99,104,114,40,52,48,41,59,80,61,99,104,114,40,52,49,41,59,113,61,99,104,114,40,57,49,41,59,81,61,99,104,114,40,57,51,41,59,102,61,99,104,114,40,49,50,51,41,59,70,61,99,104,114,40,49,50,53,41,59,108,61,52,59,112,114,105,110,116,32,112,43,112,43,112,43,112,43,112,43,112,43,80,43,112,43,80,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,80,44,115,116,114,40,41,46,106,111,105,110,40,109,97,112,40,110,44,109,97,112,40,108,97,109,98,100,97,32,120,58,111,114,100,40,120,41,45,52,56,44,115,116,114,40,41,46,106,111,105,110,40,109,97,112,40,108,97,109,98,100,97,32,99,58,36,123,98,125,54,48,48,36,123,98,125,43,40,115,116,114,40,99,45,57,57,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,57,57,53,48,48,36,123,98,125,46,122,102,105,108,108,40,49,48,41,32,105,102,32,99,62,57,57,32,101,108,115,101,32,115,116,114,40,99,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,53,48,48,36,123,98,125,41,44,91,96,43,91,46,46,46,96,40,102,61,36,123,102,125,41,40,41,96,93,46,109,97,112,40,99,61,62,99,46,99,104,97,114,67,111,100,101,65,116,40,41,41,43,96,93,41,41,91,58,58,45,49,93,43,36,123,98,125,57,57,57,57,54,36,123,98,125,41,41,41,44,36,123,91,46,46,46,39,112,112,112,112,112,112,112,80,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,80,112,112,80,112,80,80,102,70,112,80,80,39,93,46,106,111,105,110,96,43,96,125,36,123,113,125,96,93,46,109,97,112,40,115,61,62,99,40,52,51,41,46,114,101,112,101,97,116,40,77,97,116,104,46,109,97,120,40,45,120,43,115,46,99,104,97,114,67,111,100,101,65,116,40,41,44,48,41,41,43,99,40,52,53,41,46,114,101,112,101,97,116,40,77,97,116,104,46,109,97,120,40,120,45,40,120,61,115,46,99,104,97,114,67,111,100,101,65,116,40,41,41,44,48,41,41,43,99,40,52,54,41,41,46,106,111,105,110,40,99,40,41,41,46,109,97,116,99,104,40,47,46,123,49,44,50,53,51,125,47,103,41,46,106,111,105,110,40,99,40,49,48,41,43,81,41,41,125,41,40,41]))[::-1]+`99996`))),p+p+p+p+p+p+p+P+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+P+p+p+P+p+P+P+f+F+p+P+P

```

Try it online!

# Brain-Flak

Try it online!

#

```
3.[encoded data, 8374 characters]

```

Try it online!

# Answer
> 6 votes
# 3. JavaScript → Python 2 → Foo

## JavaScript

Classic JS quine. This is a full program which outputs to console:

```
(f=_=>console.log(`print(${String.fromCharCode(39)}${String.fromCharCode(34)}(f=${f})()${String.fromCharCode(34)}${String.fromCharCode(39)})`))()

```

Try it online!

## Python 2

Python code basically prints out the JS code with quotes:

```
print('"(f=_=>console.log(`print(${String.fromCharCode(39)}${String.fromCharCode(34)}(f=${f})()${String.fromCharCode(34)}${String.fromCharCode(39)})`))()"')

```

Try it online!

## Foo

Foo code is JS code with quotes:

```
"(f=_=>console.log(`print(${String.fromCharCode(39)}${String.fromCharCode(34)}(f=${f})()${String.fromCharCode(34)}${String.fromCharCode(39)})`))()"

```

Try it online!

# Answer
> 5 votes
# 1. Foo

## Foo

A simple Foo quine:

```
>&41>&60>&99>&36>&40>&60>&41>&62>&105>&36>&34>&38>&62>&34>&40>&62>&41>&60>&40(<)>(">&"$i>)<($c<)

```

Try it online!

# Answer
> 5 votes
# 2. Python 2 → Foo

i used the basic python quine, then added the quotes

python:

```
_='_=%r;print chr(34)+_%%_+chr(34)';print chr(34)+_%_+chr(34)

```

Try it online!

foo:

```
"_='_=%r;print chr(34)+_%%_+chr(34)';print chr(34)+_%_+chr(34)"

```

Try it online!

# Answer
> 5 votes
# 5. \>\<\> → JavaScript → brainfuck → Python 2 → Foo

Obligatory brainfuck answer.

## \>\<\>

```
'rpp>o<(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log([...`print(${q+Q+s+q}rpp>o<(f=${f})()${Q+q})`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join())})()

```

Try it online!

## JavaScript

```
(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log([...`print(${q+Q+s+q}rpp>o<(f=${f})()${Q+q})`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join())})()

```

Try it online!

## brainfuck

```
[encoded data, 15021 characters]

```

Try it online!

## Python 2

```
print('"\'rpp>o<(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log([...`print(${q+Q+s+q}rpp>o<(f=${f})()${Q+q})`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join())})()"')

```

Try it online!

## Foo

```
"'rpp>o<(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log([...`print(${q+Q+s+q}rpp>o<(f=${f})()${Q+q})`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join())})()"

```

Try it online!

# Answer
> 5 votes
# 9. JavaScript → Foo → Brainfuck → Wumpus → RAD → \>\<\> → Python 2 → Brain-Flak → 4

This... was.. fun.
No idea why I decided that 4 should be added, no idea why I remade the entire thing (of course, copy-pasting stuff around), but I did it :D

## JavaScript

```
(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);b=c(96);console.log(Q+[...`${Q}76+&;l2:6-&o@${q}${q+q}rpp>o<def n(x):global l;r=p+p+f+F+P+q+(p+P)*(l-x)+Q+P if x<l else p+p+f+F+P+(p+P)*(x-l)+P;l=x;return r;''a''p=chr(40);P=chr(41);q=chr(91);Q=chr(93);f=chr(123);F=chr(125);l=4;print p+p+p+p+p+p+P+p+P+p+P+P+f+F+P+f+F+p+P+P+f+F+P+f+F+P,map(n,map(lambda x:ord(x)-48,str().join(map(lambda c:${b}600${b}+(str(c-99).zfill(2)+${b}99500${b}.zfill(10) if c>99 else str(c).zfill(2)+${b}500${b}),[`+[...`(f=${f})()`].map(c=>c.charCodeAt())+`]))[::-1]+${b}99996${b})),${[...'pppppppPpPPfFpPPfFpPPfFpPPfFPppPpPPfFpPP'].join`+`}${q}`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join().match(/.{1,253}/g).join(c(10)+Q))})()

```

Try it online!

Since the output is too big for TIO to return, Here's a tester that cycles trough all the languages.

## Foo

Try it online!

## brainfuck

Try it online!

## Wumpus

```
"76+&;l2:6-&o@'''rpp>o<def n(x):global l;r=p+p+f+F+P+q+(p+P)*(l-x)+Q+P if x<l else p+p+f+F+P+(p+P)*(x-l)+P;l=x;return r;''a''p=chr(40);P=chr(41);q=chr(91);Q=chr(93);f=chr(123);F=chr(125);l=4;print p+p+p+p+p+p+P+p+P+p+P+P+f+F+P+f+F+p+P+P+f+F+P+f+F+P,map(n,map(lambda x:ord(x)-48,str().join(map(lambda c:`600`+(str(c-99).zfill(2)+`99500`.zfill(10) if c>99 else str(c).zfill(2)+`500`),[40,102,61,95,61,62,123,113,61,40,99,61,83,116,114,105,110,103,46,102,114,111,109,67,104,97,114,67,111,100,101,41,40,51,57,41,59,81,61,99,40,51,52,41,59,98,61,99,40,57,54,41,59,99,111,110,115,111,108,101,46,108,111,103,40,81,43,91,46,46,46,96,36,123,81,125,55,54,43,38,59,108,50,58,54,45,38,111,64,36,123,113,125,36,123,113,43,113,125,114,112,112,62,111,60,100,101,102,32,110,40,120,41,58,103,108,111,98,97,108,32,108,59,114,61,112,43,112,43,102,43,70,43,80,43,113,43,40,112,43,80,41,42,40,108,45,120,41,43,81,43,80,32,105,102,32,120,60,108,32,101,108,115,101,32,112,43,112,43,102,43,70,43,80,43,40,112,43,80,41,42,40,120,45,108,41,43,80,59,108,61,120,59,114,101,116,117,114,110,32,114,59,39,39,97,39,39,112,61,99,104,114,40,52,48,41,59,80,61,99,104,114,40,52,49,41,59,113,61,99,104,114,40,57,49,41,59,81,61,99,104,114,40,57,51,41,59,102,61,99,104,114,40,49,50,51,41,59,70,61,99,104,114,40,49,50,53,41,59,108,61,52,59,112,114,105,110,116,32,112,43,112,43,112,43,112,43,112,43,112,43,80,43,112,43,80,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,80,44,109,97,112,40,110,44,109,97,112,40,108,97,109,98,100,97,32,120,58,111,114,100,40,120,41,45,52,56,44,115,116,114,40,41,46,106,111,105,110,40,109,97,112,40,108,97,109,98,100,97,32,99,58,36,123,98,125,54,48,48,36,123,98,125,43,40,115,116,114,40,99,45,57,57,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,57,57,53,48,48,36,123,98,125,46,122,102,105,108,108,40,49,48,41,32,105,102,32,99,62,57,57,32,101,108,115,101,32,115,116,114,40,99,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,53,48,48,36,123,98,125,41,44,91,96,43,91,46,46,46,96,40,102,61,36,123,102,125,41,40,41,96,93,46,109,97,112,40,99,61,62,99,46,99,104,97,114,67,111,100,101,65,116,40,41,41,43,96,93,41,41,91,58,58,45,49,93,43,36,123,98,125,57,57,57,57,54,36,123,98,125,41,41,44,36,123,91,46,46,46,39,112,112,112,112,112,112,112,80,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,80,112,112,80,112,80,80,102,70,112,80,80,39,93,46,106,111,105,110,96,43,96,125,36,123,113,125,96,93,46,109,97,112,40,115,61,62,99,40,52,51,41,46,114,101,112,101,97,116,40,115,46,99,104,97,114,67,111,100,101,65,116,40,41,41,43,99,40,52,54,41,41,46,106,111,105,110,40,41,46,109,97,116,99,104,40,47,46,123,49,44,50,53,51,125,47,103,41,46,106,111,105,110,40,99,40,49,48,41,43,81,41,41,125,41,40,41]))[::-1]+`99996`)),p+p+p+p+p+p+p+P+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+P+p+p+P+p+P+P+f+F+p+P+P'

```

Try it online!

## RAD

```
'''rpp>o<def n(x):global l;r=p+p+f+F+P+q+(p+P)*(l-x)+Q+P if x<l else p+p+f+F+P+(p+P)*(x-l)+P;l=x;return r;''a''p=chr(40);P=chr(41);q=chr(91);Q=chr(93);f=chr(123);F=chr(125);l=4;print p+p+p+p+p+p+P+p+P+p+P+P+f+F+P+f+F+p+P+P+f+F+P+f+F+P,map(n,map(lambda x:ord(x)-48,str().join(map(lambda c:`600`+(str(c-99).zfill(2)+`99500`.zfill(10) if c>99 else str(c).zfill(2)+`500`),[40,102,61,95,61,62,123,113,61,40,99,61,83,116,114,105,110,103,46,102,114,111,109,67,104,97,114,67,111,100,101,41,40,51,57,41,59,81,61,99,40,51,52,41,59,98,61,99,40,57,54,41,59,99,111,110,115,111,108,101,46,108,111,103,40,81,43,91,46,46,46,96,36,123,81,125,55,54,43,38,59,108,50,58,54,45,38,111,64,36,123,113,125,36,123,113,43,113,125,114,112,112,62,111,60,100,101,102,32,110,40,120,41,58,103,108,111,98,97,108,32,108,59,114,61,112,43,112,43,102,43,70,43,80,43,113,43,40,112,43,80,41,42,40,108,45,120,41,43,81,43,80,32,105,102,32,120,60,108,32,101,108,115,101,32,112,43,112,43,102,43,70,43,80,43,40,112,43,80,41,42,40,120,45,108,41,43,80,59,108,61,120,59,114,101,116,117,114,110,32,114,59,39,39,97,39,39,112,61,99,104,114,40,52,48,41,59,80,61,99,104,114,40,52,49,41,59,113,61,99,104,114,40,57,49,41,59,81,61,99,104,114,40,57,51,41,59,102,61,99,104,114,40,49,50,51,41,59,70,61,99,104,114,40,49,50,53,41,59,108,61,52,59,112,114,105,110,116,32,112,43,112,43,112,43,112,43,112,43,112,43,80,43,112,43,80,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,80,44,109,97,112,40,110,44,109,97,112,40,108,97,109,98,100,97,32,120,58,111,114,100,40,120,41,45,52,56,44,115,116,114,40,41,46,106,111,105,110,40,109,97,112,40,108,97,109,98,100,97,32,99,58,36,123,98,125,54,48,48,36,123,98,125,43,40,115,116,114,40,99,45,57,57,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,57,57,53,48,48,36,123,98,125,46,122,102,105,108,108,40,49,48,41,32,105,102,32,99,62,57,57,32,101,108,115,101,32,115,116,114,40,99,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,53,48,48,36,123,98,125,41,44,91,96,43,91,46,46,46,96,40,102,61,36,123,102,125,41,40,41,96,93,46,109,97,112,40,99,61,62,99,46,99,104,97,114,67,111,100,101,65,116,40,41,41,43,96,93,41,41,91,58,58,45,49,93,43,36,123,98,125,57,57,57,57,54,36,123,98,125,41,41,44,36,123,91,46,46,46,39,112,112,112,112,112,112,112,80,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,80,112,112,80,112,80,80,102,70,112,80,80,39,93,46,106,111,105,110,96,43,96,125,36,123,113,125,96,93,46,109,97,112,40,115,61,62,99,40,52,51,41,46,114,101,112,101,97,116,40,115,46,99,104,97,114,67,111,100,101,65,116,40,41,41,43,99,40,52,54,41,41,46,106,111,105,110,40,41,46,109,97,116,99,104,40,47,46,123,49,44,50,53,51,125,47,103,41,46,106,111,105,110,40,99,40,49,48,41,43,81,41,41,125,41,40,41]))[::-1]+`99996`)),p+p+p+p+p+p+p+P+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+P+p+p+P+p+P+P+f+F+p+P+P'

```

Try it online!

## \>\<\>

```
'rpp>o<def n(x):global l;r=p+p+f+F+P+q+(p+P)*(l-x)+Q+P if x<l else p+p+f+F+P+(p+P)*(x-l)+P;l=x;return r;'a'p=chr(40);P=chr(41);q=chr(91);Q=chr(93);f=chr(123);F=chr(125);l=4;print p+p+p+p+p+p+P+p+P+p+P+P+f+F+P+f+F+p+P+P+f+F+P+f+F+P,map(n,map(lambda x:ord(x)-48,str().join(map(lambda c:`600`+(str(c-99).zfill(2)+`99500`.zfill(10) if c>99 else str(c).zfill(2)+`500`),[40,102,61,95,61,62,123,113,61,40,99,61,83,116,114,105,110,103,46,102,114,111,109,67,104,97,114,67,111,100,101,41,40,51,57,41,59,81,61,99,40,51,52,41,59,98,61,99,40,57,54,41,59,99,111,110,115,111,108,101,46,108,111,103,40,81,43,91,46,46,46,96,36,123,81,125,55,54,43,38,59,108,50,58,54,45,38,111,64,36,123,113,125,36,123,113,43,113,125,114,112,112,62,111,60,100,101,102,32,110,40,120,41,58,103,108,111,98,97,108,32,108,59,114,61,112,43,112,43,102,43,70,43,80,43,113,43,40,112,43,80,41,42,40,108,45,120,41,43,81,43,80,32,105,102,32,120,60,108,32,101,108,115,101,32,112,43,112,43,102,43,70,43,80,43,40,112,43,80,41,42,40,120,45,108,41,43,80,59,108,61,120,59,114,101,116,117,114,110,32,114,59,39,39,97,39,39,112,61,99,104,114,40,52,48,41,59,80,61,99,104,114,40,52,49,41,59,113,61,99,104,114,40,57,49,41,59,81,61,99,104,114,40,57,51,41,59,102,61,99,104,114,40,49,50,51,41,59,70,61,99,104,114,40,49,50,53,41,59,108,61,52,59,112,114,105,110,116,32,112,43,112,43,112,43,112,43,112,43,112,43,80,43,112,43,80,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,80,44,109,97,112,40,110,44,109,97,112,40,108,97,109,98,100,97,32,120,58,111,114,100,40,120,41,45,52,56,44,115,116,114,40,41,46,106,111,105,110,40,109,97,112,40,108,97,109,98,100,97,32,99,58,36,123,98,125,54,48,48,36,123,98,125,43,40,115,116,114,40,99,45,57,57,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,57,57,53,48,48,36,123,98,125,46,122,102,105,108,108,40,49,48,41,32,105,102,32,99,62,57,57,32,101,108,115,101,32,115,116,114,40,99,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,53,48,48,36,123,98,125,41,44,91,96,43,91,46,46,46,96,40,102,61,36,123,102,125,41,40,41,96,93,46,109,97,112,40,99,61,62,99,46,99,104,97,114,67,111,100,101,65,116,40,41,41,43,96,93,41,41,91,58,58,45,49,93,43,36,123,98,125,57,57,57,57,54,36,123,98,125,41,41,44,36,123,91,46,46,46,39,112,112,112,112,112,112,112,80,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,80,112,112,80,112,80,80,102,70,112,80,80,39,93,46,106,111,105,110,96,43,96,125,36,123,113,125,96,93,46,109,97,112,40,115,61,62,99,40,52,51,41,46,114,101,112,101,97,116,40,115,46,99,104,97,114,67,111,100,101,65,116,40,41,41,43,99,40,52,54,41,41,46,106,111,105,110,40,41,46,109,97,116,99,104,40,47,46,123,49,44,50,53,51,125,47,103,41,46,106,111,105,110,40,99,40,49,48,41,43,81,41,41,125,41,40,41]))[::-1]+`99996`)),p+p+p+p+p+p+p+P+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+P+p+p+P+p+P+P+f+F+p+P+P

```

`><>` outputs a newline in the middle with `'a'` so the python function would be separated from the rest of the code.

Try it online!

## Python 2

```
def n(x):global l;r=p+p+f+F+P+q+(p+P)*(l-x)+Q+P if x<l else p+p+f+F+P+(p+P)*(x-l)+P;l=x;return r;
p=chr(40);P=chr(41);q=chr(91);Q=chr(93);f=chr(123);F=chr(125);l=4;print p+p+p+p+p+p+P+p+P+p+P+P+f+F+P+f+F+p+P+P+f+F+P+f+F+P,map(n,map(lambda x:ord(x)-48,str().join(map(lambda c:`600`+(str(c-99).zfill(2)+`99500`.zfill(10) if c>99 else str(c).zfill(2)+`500`),[40,102,61,95,61,62,123,113,61,40,99,61,83,116,114,105,110,103,46,102,114,111,109,67,104,97,114,67,111,100,101,41,40,51,57,41,59,81,61,99,40,51,52,41,59,98,61,99,40,57,54,41,59,99,111,110,115,111,108,101,46,108,111,103,40,81,43,91,46,46,46,96,36,123,81,125,55,54,43,38,59,108,50,58,54,45,38,111,64,36,123,113,125,36,123,113,43,113,125,114,112,112,62,111,60,100,101,102,32,110,40,120,41,58,103,108,111,98,97,108,32,108,59,114,61,112,43,112,43,102,43,70,43,80,43,113,43,40,112,43,80,41,42,40,108,45,120,41,43,81,43,80,32,105,102,32,120,60,108,32,101,108,115,101,32,112,43,112,43,102,43,70,43,80,43,40,112,43,80,41,42,40,120,45,108,41,43,80,59,108,61,120,59,114,101,116,117,114,110,32,114,59,39,39,97,39,39,112,61,99,104,114,40,52,48,41,59,80,61,99,104,114,40,52,49,41,59,113,61,99,104,114,40,57,49,41,59,81,61,99,104,114,40,57,51,41,59,102,61,99,104,114,40,49,50,51,41,59,70,61,99,104,114,40,49,50,53,41,59,108,61,52,59,112,114,105,110,116,32,112,43,112,43,112,43,112,43,112,43,112,43,80,43,112,43,80,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,112,43,80,43,80,43,102,43,70,43,80,43,102,43,70,43,80,44,109,97,112,40,110,44,109,97,112,40,108,97,109,98,100,97,32,120,58,111,114,100,40,120,41,45,52,56,44,115,116,114,40,41,46,106,111,105,110,40,109,97,112,40,108,97,109,98,100,97,32,99,58,36,123,98,125,54,48,48,36,123,98,125,43,40,115,116,114,40,99,45,57,57,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,57,57,53,48,48,36,123,98,125,46,122,102,105,108,108,40,49,48,41,32,105,102,32,99,62,57,57,32,101,108,115,101,32,115,116,114,40,99,41,46,122,102,105,108,108,40,50,41,43,36,123,98,125,53,48,48,36,123,98,125,41,44,91,96,43,91,46,46,46,96,40,102,61,36,123,102,125,41,40,41,96,93,46,109,97,112,40,99,61,62,99,46,99,104,97,114,67,111,100,101,65,116,40,41,41,43,96,93,41,41,91,58,58,45,49,93,43,36,123,98,125,57,57,57,57,54,36,123,98,125,41,41,44,36,123,91,46,46,46,39,112,112,112,112,112,112,112,80,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,112,80,80,102,70,80,112,112,80,112,80,80,102,70,112,80,80,39,93,46,106,111,105,110,96,43,96,125,36,123,113,125,96,93,46,109,97,112,40,115,61,62,99,40,52,51,41,46,114,101,112,101,97,116,40,115,46,99,104,97,114,67,111,100,101,65,116,40,41,41,43,99,40,52,54,41,41,46,106,111,105,110,40,41,46,109,97,116,99,104,40,47,46,123,49,44,50,53,51,125,47,103,41,46,106,111,105,110,40,99,40,49,48,41,43,81,41,41,125,41,40,41]))[::-1]+`99996`)),p+p+p+p+p+p+p+P+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+p+P+P+f+F+P+p+p+P+p+P+P+f+F+p+P+P

```

Try it online!

Python generates the brain-flak somewhat optimized, reusing the previous item on the stack, because otherwise the brain-flak code would be around a megabyte..

## Brain-Flak

Try it online!

## 4

```
3.[encoded data, 7431 characters]
```

Try it online!

# Answer
> 4 votes
# 4. \>\<\> → JavaScript → Python 2 → Foo

# \>\<\>

```
'rpp>o<(f=_=>console.log(`print(${String.fromCharCode(39,34,92,39)}rpp>o<(f=${f})()${String.fromCharCode(34,39)})`))()

```

Try it online!

I've added the classic wrapping string literal, appending `'rpp>o<` to the start, and editing it into the center part too.

# JavaScript (Node.js)

```
(f=_=>console.log(`print(${(a=String.fromCharCode)(39,34,92,39)}rpp>o<(f=${f})()${a(34,39)})`))()

```

Try it online!

I've shortened the `String.fromCharCode` part as per comments on the last answer. I know this challenge isn't actually code-golf, but it makes me feel better.

# Python 2

```
print('"\'rpp>o<(f=_=>console.log(`print(${(a=String.fromCharCode)(39,34,92,39)}rpp>o<(f=${f})()${a(34,39)})`))()"')

```

Try it online!

Added the `\'` for the start of the \>\<\> quine.

# Foo

```
"'rpp>o<(f=_=>console.log(`print(${(a=String.fromCharCode)(39,34,92,39)}rpp>o<(f=${f})()${a(34,39)})`))()"

```

Try it online!

# Answer
> 4 votes
# 6. \>\<\> → JavaScript → brainfuck → Python 2 → Brain-Flak → Foo

Obligatory Brain-Flak answer.

# \>\<\>

```
'rpp>o<(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log([...`print map(lambda x:${q}(${q}+ord(x)*${q}()${q}+${q})${q},${q+Q+s+q}rpp>o<(f=${f})()${Q+q}),${q}{({}<>)<>}<>${q}`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join())})()

```

Try it online!

# JavaScript (Node.js)

```
(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log([...`print map(lambda x:${q}(${q}+ord(x)*${q}()${q}+${q})${q},${q+Q+s+q}rpp>o<(f=${f})()${Q+q}),${q}{({}<>)<>}<>${q}`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join())})()

```

Try it online!

# brainfuck

```
[encoded data, 24781 characters]

```

Try it online!

# Python 2

```
print map(lambda x:'('+ord(x)*'()'+')','"\'rpp>o<(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log([...`print map(lambda x:${q}(${q}+ord(x)*${q}()${q}+${q})${q},${q+Q+s+q}rpp>o<(f=${f})()${Q+q}),${q}{({}<>)<>}<>${q}`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join())})()"'),'{({}<>)<>}<>'

```

Try it online!

# Brain-Flak

Try it online!

I've excluded the code since it's 41568 bytes and reaches the character limit of the answer. The code is similar to the brainfuck code and is in the format:

```
[
  '( ()*byte value )', Repeated for each character
] '{({}<>)<>}<>'      To reverse the stack

```

# Foo

```
"'rpp>o<(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log([...`print map(lambda x:${q}(${q}+ord(x)*${q}()${q}+${q})${q},${q+Q+s+q}rpp>o<(f=${f})()${Q+q}),${q}{({}<>)<>}<>${q}`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join())})()"

```

Try it online!

# Answer
> 4 votes
# 7. \>\<\> → JavaScript → RAD → brainfuck → Python 2 → Brain-Flak → Foo

## \>\<\>

```
'rpp>o<(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log(q+[...`print map(lambda x:${q}(${q}+ord(x)*${q}()${q}+${q})${q},${q+Q+s+q}rpp>o<(f=${f})()${Q+q}),${q}{({}<>)<>}<>${q}`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join()+q)})()

```

Try it online!

## JavaScript

```
(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log(q+[...`print map(lambda x:${q}(${q}+ord(x)*${q}()${q}+${q})${q},${q+Q+s+q}rpp>o<(f=${f})()${Q+q}),${q}{({}<>)<>}<>${q}`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join()+q)})()

```

Try it online!

## RAD

```
[encoded data, 25103 characters]

```

Try it online!

## brainfuck

```
[encoded data, 25101 characters]

```

Try it online!

## Python 2

```
print map(lambda x:'('+ord(x)*'()'+')','"\'rpp>o<(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log(q+[...`print map(lambda x:${q}(${q}+ord(x)*${q}()${q}+${q})${q},${q+Q+s+q}rpp>o<(f=${f})()${Q+q}),${q}{({}<>)<>}<>${q}`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join()+q)})()"'),'{({}<>)<>}<>'

```

Try it online!

## Brain-Flak

Try it online!

## Foo

```
"'rpp>o<(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log(q+[...`print map(lambda x:${q}(${q}+ord(x)*${q}()${q}+${q})${q},${q+Q+s+q}rpp>o<(f=${f})()${Q+q}),${q}{({}<>)<>}<>${q}`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join()+q)})()"

```

Try it online!

# Answer
> 4 votes
# 8. \>\<\> → Wumpus → JavaScript → RAD → brainfuck → Python 2 → Brain-Flak → Foo

Adding in another 2D language, Wumpus.

# \>\<\>

```
'rppp:1->o#13&;l2:6-&o@(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log(q+[...`print map(lambda x:(ord(x)*${q}()${q},),${q+Q+s+q}rppp:1->o#13&;l2:6-&o@${Q+s}n${Q}(f=${f})()${Q+q}),${q}{({}<>)<>}<>${q}`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join()+q)})()

```

Try it online!

This now prints an extra `"` before the Wumpus code.

# Wumpus

```
"#13&;l2:6-&o@(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log(q+[...`print map(lambda x:(ord(x)*${q}()${q},),${q+Q+s+q}rppp:1->o#13&;l2:6-&o@${Q+s}n${Q}(f=${f})()${Q+q}),${q}{({}<>)<>}<>${q}`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join()+q)})()

```

Try it online!

Similar to the `><>` quine, this wraps a string literal around the code and printsonly the javascript section

# JavaScript (Node.js)

```
(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log(q+[...`print map(lambda x:(ord(x)*${q}()${q},),${q+Q+s+q}rppp:1->o#13&;l2:6-&o@${Q+s}n${Q}(f=${f})()${Q+q}),${q}{({}<>)<>}<>${q}`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join()+q)})()

```

Try it online!

# RAD

Try it online!

I'm excluding very long sections of code from the chain, so as to not fill up the answer.

# brainfuck

Try it online!

# Python 2

```
print map(lambda x:(ord(x)*'()',),'"\'rppp:1->o#13&;l2:6-&o@"\n"(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log(q+[...`print map(lambda x:(ord(x)*${q}()${q},),${q+Q+s+q}rppp:1->o#13&;l2:6-&o@${Q+s}n${Q}(f=${f})()${Q+q}),${q}{({}<>)<>}<>${q}`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join()+q)})()"'),'{({}<>)<>}<>'

```

Try it online!

# Brain-Flak

Try it online!

# Foo

```
"'rppp:1->o#13&;l2:6-&o@"
"(f=_=>{q=(c=String.fromCharCode)(39);Q=c(34);s=c(92);console.log(q+[...`print map(lambda x:(ord(x)*${q}()${q},),${q+Q+s+q}rppp:1->o#13&;l2:6-&o@${Q+s}n${Q}(f=${f})()${Q+q}),${q}{({}<>)<>}<>${q}`].map(s=>c(43).repeat(s.charCodeAt())+c(46)).join()+q)})()"

```

Try it online!

This was actually the most annoying part of the code. Foo only supports string literals of 254 length for some reason, so I had to golf a bit and split up the Javascript from the 2D code to keep it short. The next person will probably have to split up the Javascript.

---
Tags: quine, answer-chaining
---


title: Sort by what the digit pairs describe

text:
# Question
Title: Sort by what the digit pairs describe
Given a positive integer, we can form a new number that's described by its digits taken pairwise (with a leading 0 added for numbers with odd number of digits).

For eg.:

* 1234 can be read as one 2, three 4s - so, the output for 1234 is 2444.
* 643 has an odd number of digits, so a leading zero is added to make it even. Then, 0643 can be read as: zero 6s, four 3s, hence the output would be 3333.

(This is OEIS A056967).

**Task:** Given an array of positive integers, sort them by their digit-pair-described value, in ascending order. Order does not matter between input numbers that lead to the same value.

**Input**: an array/list/set of positive integers. Leading zeros in the input are **not** allowed, and input as strings/lists of digits/etc. are not allowed - the inputs should be as close to an integer/numeric type as your language is capable of using.

**Output**: the array sorted in the above-mentioned way, returned in any of the usual ways (function return value/STDOUT/shouting into the void/etc.) You can print them individually, return them as numbers, strings, or lists of digits.

### Test cases

```
Input
Output

[19, 91, 2345, 2023]
[19, 2023, 2345, 91]

[25257, 725, 91, 5219, 146125, 14620512]
[725, 5219, 14620512, 91, 146125, 25257]

[123130415 3335 91 111111111 528 88]
[528, 111111111, 123130415, 3335, 88, 91]

[1 21 33 4 5]
[1 4 5 21 33]

[3725, 10, 2537, 1, 1225, 2512]
[10, 1, 1225, 2512, 2537, 3725]

[125, 26, 1115, 1024]
[1115, 1024, 125, 26]

```

(In the 4th test case, 1, 4, and 5 all evaluate to 0, and so can be sorted among themselves in any order. Similarly in the fifth test case, 10 and 1 both evaluate to 0s, and so can be sorted in either order.)

(Related: Say what you see, One 1, Two 1's, One 2 One 1

*Thanks to Kevin Cruijssen for help clarifying the question in the Sandbox.*

# Answer
> 5 votes
# APL (Dyalog), 26 bytes

Thanks ngn for saving 1 byte :)

```
{⍵[⍋⌽↑,⍨⌿⍴⌿0 10⊤⍵⊤⍨⍴⍨100]}

```

Try it online!

Inspiration take from dzaima & ngn

# Answer
> 3 votes
# Jelly, 10 bytes

```
ṚẋƝm2ṚFḌµÞ

```

Try it online!

Check out a test suite!

### How it works

```
ṚẋƝm2ṚFḌµÞ    Monadic link / Full program.                            | Example: [25257, 725, 91, 5219, 146125, 14620512]
        µÞ    Sort the input list by the result of the monadic link:  | Example: 725
Ṛ             Promote N to its digit array and reverse it.            | [5, 2, 7]
 ẋƝ           For each two consecutive digits x, y, repeat x y times. | [[5, 5], [2, 2, 2, 2, 2, 2, 2]]
   m2         Modular 2. Take every other element of this array.      | [[5, 5]]
     Ṛ        Reverse.                                                | [[5, 5]]
      F       Flatten.                                                | [5, 5]
       Ḍ      Convert from decimal to integer.                        | 55

```

# Answer
> 3 votes
# , 141 bytes

```
(s<-scan(,""))[order(strtoi(sapply(s,function(x)paste(strrep((m=matrix(c(if(nchar(x)%%2)0,el(strsplit(x,""))),2))[2,],m[1,]),collapse=""))))]

```

Try it online!

Rather laborious answer - but it works on all test cases. Builds the digit-pair output and sorts the input according to this.

# Answer
> 3 votes
# , 120 bytes

```
(v=scan())[order(sapply(v,function(n,e=nchar(n))sum((a=rep((x=n%/%10^(0:(e-1-e%%2))%%10)[!0:1],x[!1:0]))*10^seq(a=a))))]

```

Try it online!

* -11 bytes thanks to @sundar "arithmetical" suggestion !

**Ungolfed code with explanation :**

```
# define a function G which takes a number 'n' and uncompress it multiplied by 10
# e.g. 2735 -> 775550, 61345 -> 355550 etc.
G=function(n){
  e = nchar(n)                   # get the n.of digits in the compressed number

  x = n%/%10^(0:(e-1-e%%2))%%10  # split 'n' into vector of digits reversed adding
                                 # trailing zero if 'e' is odd (e.g. 123 -> c(0,3,2,1))

  even = x[!0:1]                 # take only the odd elements of 'x' (= even digits)
  odd  = x[!1:0]                 # take only the even elements of 'x' (= odd digits)
                                 # N.B. :
                                 # these tricks work because !0:1 is c(TRUE,FALSE)
                                 # and V[c(TRUE,FALSE)] exploits the fact that R
                                 # automatically recycles the logical indexes up to the
                                 # length of the vector V

  a = rep(even,odd)              # repeat each value in 'even' 'odd' times obtaining the
                                 # uncompressed number as digits vector. Note that in
                                 #  case of single digit 'n', 'a' will be an empty vector

  sum(a*10^seq(a=a))             # multiplies 'a' * 10^(1:length(a)) and sum
                                 # obtaining the uncompressed number multiplied by 10
                                 # N.B. in case of empty 'a', we get 0
}

v = scan()                       # take vector v from stdin

w = sapply(v,G(n))               # apply G to all numbers of 'v'

v[order(w)]                      # use the uncompressed values as weights to sort 'v'

```

# Answer
> 2 votes
# Pyth, 14 bytes

```
oir9c.[Z2jNT2T

```

**Try it here! | Test suite! | 12 bytes with list of digits I/O**

### How it works?

```
oir9c.[Z2jNT2T – Full program.
o              – Sort the input list by the results of the following code (variable: N).
         jNT   – Cast the current element to a list of digits.
     .[Z2      – Pad it on the left with 0s to the nearest multiple of 2.
    c       2  – Split in pieces of length 2.
  r9           – Run length decode.
 i           T – Cast the list of digits to a base 10 integer.

```

# Answer
> 2 votes
# Dyalog APL, ~~41~~ ~~39~~ ~~36~~ ~~35~~ ~~31~~ ~~30~~ 29 bytes

```
f←⊂⌷¨⍨∘⍋{10⊥∊⍴⌿0 10⊤100⊥⍣¯1⊢⍵}¨

```

Try it online!

-2 thanks to Cows quack
-4 (plus -4 for the base conversion idea) thanks to ngn
-2 thanks so H.PWiz

# Answer
> 2 votes
# C (gcc) (32bit systems), 188 177 176 bytes

```
char*p,*q,c[99],b[99]="0";i;d(x){for(p=b+!(sprintf(b+1,"%d",x)&1),q=c;i=*p++;++p)for(i-=48;i--;*q++=*p);*q=0;atoi(c);}m(int*a,int*b){return d(*a)-d(*b);}s(l,c){qsort(l,c,4,m);}

```

Try it online!

on `amd64` add flag `-m32` for compiling.

**Usage**: `s(x,n);` where `x` points to an array of integers to sort and `n` is the length of that array.

Second test case gives wrong result because converting `25257` gives `2222277777` which overflows a 32bit integer -- added a 5th test case without that number.

### Explanation:

```
char*p,                                     // d(): input pointer
    *q,                                     // d(): output pointer
    c[99],                                  // d(): output buffer
    b[99]="0";                              // d(): input buffer
                                            //      (fixed first char 0)
i;                                          // d(): repeat counter

d(x){                                       // conversion function
    for(
            p=b+!(sprintf(b+1,"%d",x)&1),   // print number in decimal to
                                            // input buffer, starting at second
                                            // character, initialize input
                                            // pointer to first or second char
                                            // depending on the length of the
                                            // number
            q=c;                            // initialize output pointer
            i=*p++;                         // set repeat counter to digit and
                                            // point to next digit, stop when
                                            // NUL character is found
            ++p)                            // point to next digit after loop
        for(i-=48;i--;*q++=*p);             // inner loop, append current digit
                                            // i-48 ('0') times to output buffer
    *q=0;                                   // terminate output with NUL
    atoi(c);                                // convert to number, 'return' not
                                            // needed as atoi() leaves result
                                            // on the stack
}

m(int*a,int*b){                             // comparison function for qsort
    return d(*a)-d(*b);                     // return difference of converted
}                                           // values

s(l,c){                                     // sorting function
    qsort(l,c,4,m);                         // only "wrap" qsort, assuming
}                                           // sizeof(int) is 4

```

# Answer
> 2 votes
# Perl 6, 53 bytes

```
*.sort(+*.flip.comb.rotor(2).map({[x] $_}).join.flip)

```

Try it online!

Anonymous Whatever lambda that takes a list of values and sorts it by what the pairs of numbers describe.

In this case, I'm reversing the number, then `rotor`ing the list by two to get each pair of numbers. This will exclude the first digit for numbers of odd length, but since that translates to `0` times that number, it's okay. Plus, it lines up the values to use `[x]` correctly.

# Answer
> 2 votes
# Haskell, ~~89~~ 88 bytes

*Saved a byte thanks to ovs*

```
import Data.List
(%)=mod
m?n|n<1=0|n%100<10=m?div n 100|w<-n-10=m*10?w+m*n%10
sortOn(1?)

```

The last line defines an anonymous function that can be used like so:

```
> sortOn(1?)[19, 91, 2345, 2023]
[19,2023,2345,91]

```

The core functionality is provided by the infix operator `(?)` which keeps track of a multiplier, `m`, and the remaining RLE input `n`. `(?)` continually subtracts 10 from `n` whilst there is a tens digit to subtract from, and as it does so it pushes another copy of the final digit to the front of the output (via the multiplier `m`, which is increased by 10 each time). When the tens place is exhausted, the final two digits are discarded and the process repeats until the number is reduced to 0. Finally, we use the operator (with an initial multiplier of 1) as a sort key.

# Answer
> 2 votes
# Python 2, ~~80~~ 74 bytes

```
def g(l):l.sort(key=f)
f=lambda n:+(n>9)and int(`f(n/100)`+n/10%10*`n%10`)

```

Try it online!

# Answer
> 2 votes
# Husk, 10 bytes

```
ÖödṁΓ*C_2d

```

Try it online!

# Explanation

```
ÖödṁΓ*C_2d    Full function
Ö             Sort the input list by the result of...
 ö            The composition of these four functions:
         d      Convert to a list of digits
      C_2       Split into length-2 sublists starting at the end
   ṁ            Map the following function and concatenate the results:
    Γ*            Repeat the list tail X times, where X is the list head
  d             Convert back to an integer

```

# Answer
> 1 votes
# Python 2, ~~102~~ ~~101~~ ~~97~~ 101 bytes

```
lambda l:sorted(l,key=lambda x:int(''.join(v*int(c)for c,v in zip(*[iter(`x`[len(`x`)%2:])]*2))or 0))

```

Try it online!

# Answer
> 1 votes
# Japt, 13 bytes

```
ñ_ì_ò2n)®rçÃ¬

```

Try it or run all test cases

---

## Explanation

```
ñ_                :Sort by passing each integer through a function
  ì_              :  Split to an array of digits, pass it through the following function and implicitly convert back to an integer
    ò2n)          :    Starting from the end of the array, split at every second element
        ®         :    Map
         rç       :      Reduce X & Y by repeating X Y times
           Ã      :    End mapping
            ¬     :    Join

```

# Answer
> 1 votes
# Brachylog, 18 bytes

```
{↔ġ₂ẹ{Ċj₎|Ȯt}ˢ↔c}ᵒ

```

Try it online!

### Explanation

Loads of small stuff needed to account for the three different cases: odd number of digits, pair of 0 times a number, and normal pairs.

```
{               }ᵒ     Order the Input according to the output of this predicate
 ↔                       Reverse the number
  ġ₂                     Group into pairs; the last digit is alone if there are
                           an odd number of them
    ẹ{      }ˢ           For each group:
      Ċ                    If there are two elements
       j₎                  Juxtapose the first one as many times as the second
                             element (won't work if the second element is 0)
         |                 Else
          Ȯ                If there is one element (odd number of digits)
           t               just take that element
                           (Else don't select anything, i.e. 0 repetitions)
              ↔c         Reverse and concatenate back into an integer

```

# Answer
> 1 votes
# Perl 5, 76 bytes

A function instead of a one-liner for once.

Quite straight forward: `g` sorts the inputs numerically, using `h` to convert the numbers. `h` does this by using the regex `s/(.)(.)/$2x$1/gre` (which is probably readable enough). And the `0` left-padding is done with `0 x("@_"=~y///c%2)."@_"` (where `y///c` is a shorted way of writing `length`, `x` is the repetition operator and `.` the concatenation).

```
sub h{(0 x("@_"=~y///c%2)."@_")=~s/(.)(.)/$2x$1/gre}sub g{sort{h($a)-h$b}@_}

```

Try it online!

I'm expecting to see some shorter Perl answers though!

# Answer
> 1 votes
# 05AB1E, ~~20~~ 19 bytes

```
ΣDgÉi¦}2ôε`sиJ}J0ìï

```

Bug-fixed for +1 byte, and then golfed by -2 bytes thanks to *@sundar*.

Try it online or verify all test cases.

Can definitely be golfed.. Not too happy about it tbh..

**Explanation:**

```
Σ                    # Sort by:
 Dg                  #  Duplicate the current number, and take it's length
                     #   i.e. 25257 → 5
                     #   i.e. 4 → 1
   Éi }              #  If this length is odd:
     ¦               #   Remove the first digit
                     #    i.e. 25257 → '5257'
                     #    i.e. 4 → ''
       2ô            #  Then split the number in pieces of 2
                     #   i.e. '5257' → ['52','57']
                     #   i.e. '' → []
         ε    }      #  And map each to:
          `          #   Push both digits to the stack
                     #    i.e. '52' → '5' and '2'
           s         #   Swap them
            и        #   Repeat the first digit the second digit amount of times
                     #    i.e. '2' and '5' → ['2','2','2','2','2']
             J       #   Join the list of digits together
                     #    i.e. ['2','2','2','2','2'] → '22222'
               J     #  Join all numbers back together again
                     #   i.e. ['','22222','77777'] → '2222277777'
                     #   i.e. [] → ''
                0ì   #  Prepend a 0 (because `Σ` will put all '' at the back)
                     #   i.e. 2222277777 → '02222277777'
                     #   i.e. '' → '0'
                  ï  #  Cast it to an integer, because sorting is done string-wise by
                     #  default despite 05AB1E's interchangeability of strings and numbers;
                     #  and it's also to remove all leading zeros
                     #   i.e. '02222277777' → 2222277777
                     #   i.e. '0' → 0

```

# Answer
> 1 votes
# Retina, 44 bytes

```
^.?((..)*)$
$1 $&
%)`\G(\d)(.)
$1*$2
N`
.+

```

Try it online! Generating the sort key at the start of the line is harder but the short sort stage results in an overall 3 byte saving. Explanation:

```
%)`

```

Apply the first two stages on each line individually.

```
^.?((..)*)$
$1 $&

```

Match and copy an even number of trailing digits.

```
\G(\d)(.)
$1*$2

```

Replace each digit pair with their described value. The `\G\d` causes the match to stop at the space.

```
N`

```

Sort numerically.

```
.+

```

Delete the sort keys.

# Answer
> 1 votes
# JavaScript (ES8), ~~72~~ 70 bytes

```
a=>a.sort((a,b)=>(g=n=>n&&g(n/100|0)+''.padEnd(n/10%10,n%10))(a)-g(b))

```

Try it online!

# Answer
> 1 votes
# Attache, 50 bytes

```
SortBy!N@Flip##~`&&>PadRight&0&2=>Chop&2@Flip@List

```

Try it online!

## Explanation

```
SortBy!N@Flip##~`&&>PadRight&0&2=>Chop&2@Flip@List      anonymous function, argument: [a1..aN]
SortBy!                                                 sort the given array by grading f[ai]
                                                        e.g. 42513
                                              List      digits of ai
                                                        e.g. [4, 2, 5, 1, 3]
                                         Flip@          flip the digits around
                                                        e.g. [3, 1, 5, 2, 4]
                                  Chop&2@               chop into groups of 2
                                                        e.g. [[3, 1], [5, 2], [4]]
                    PadRight&0&2=>                      pad each group to size 2 with 0's
                                                        e.g. [[3, 1], [5, 2], [0, 4]]
                  &>                                    using each sub array as arguments...
               ~`&                                      ...repeat the 2nd the 1st amount of times
                                                        e.g. [[1, 1, 1], [2, 2, 2, 2, 2], []]
             ##                                         then:
         Flip                                           reverse the groups
                                                        e.g. [[2, 2, 2, 2, 2], [1, 1, 1]]
       N@                                               then convert it to an number
                                                        e.g. 22222111

```

# Answer
> 0 votes
# Jelly, 14 bytes

```
DŻLḂ$¡ẋ@2/ẎḌµÞ

```

Try it online!

# Answer
> 0 votes
# Java 11, ~~204~~ 189 bytes

```
L->{L.sort((a,b)->Long.compare(s(a+""),s(b+"")));}long s(String s){var r="";for(int l=s.length(),i=l%2;i<l;)r+=s.split("")[++i].repeat(s.charAt(i++-1)-48);return r.isEmpty()?0:new Long(r);}

```

Takes a List of Longs as parameter and sorts this input-List (without returning a new List).

Try it online (NOTE: `String.repeat(int)` is emulated as `repeat(String,int)` because Java 11 isn't on TIO yet. The byte-count remains the same.)

**Explanation:**

```
L->{                     // Method with ArrayList<Long> parameter and no return-type
  L.sort(                //  Sort the list by:
   (a,b)->Long.compare(  //   Using a builtin Long-comparator with:
     s(a+""),s(b+"")));} //   The correctly formatted values as described in the challenge

long s(String s){        // Separated method with String parameter and long return-type
  var r="";              //  Temp-String, starting empty
  for(int l=s.length(),  //  The length of the input-String
      i=l%2;i<l;)        //   If the length is even:
                         //    Loop `i` in the range [0,`l`) (in steps of 2)
                         //   Else (the length is odd):
                         //    Loop `i` in the range [1,`l`) (in steps of 2) instead
    r+=                  //   Append the result-String with:
      s.split("")[++i].  //    The digit at index `i+1`
      .repeat(s.charAt(i++-1)-48);
                         //    Repeated the digit at index `i` amount of times
  return r.isEmpty()?    //  If the temp-String is empty:
          0              //   Return 0
         :               //  Else:
          new Long(r);}  //   Convert the temp-String to a long and return it

```

# Answer
> 0 votes
# Ruby, 71 bytes

```
->a{a.sort_by{|a|s='';s,a=[a%10]*(a/10%10)*''+s,a/100while a>0;s.to_i}}

```

Try it online!

---
Tags: code-golf, math, number, sequence, sorting
---