Skip to main content
added 537 characters in body
Source Link
pxeger
  • 25.3k
  • 4
  • 59
  • 146

Zsh, 206 + 6 = 12 bytes

tr 1 2

Attempt This Online!

tr 2 3

Attempt This Online!

The first program replaces 1s with 2s; the second replaces 2s with 3s. These commute only for numbers not containing the digit 1.


Old answer:

Zsh, 10 + 10 = 20 bytes

\$ f(x) = \lfloor \frac x 2 \rfloor \$

<<<$[$1/2]

Attempt This Online!

\$ g(x) = 2x \$

<<<$[$1*2]

Attempt This Online!

For even \$ x \$, \$ f(x) = \frac x 2 \$, so \$ g(f(x)) = x \$.

But for odd \$ x \$, /2 rounds downwards, so \$ f(x) = f(x-1) \$, so \$ g(f(x)) = x - 1 \ne x \$

\$ g(x) \$ is always even, so for all \$ x \$, \$ f(g(x)) = x \$.

Therefore, \$ f(g(x)) = g(f(x)) \$ if and only if \$ x \$ is even.

Zsh, 20 bytes

\$ f(x) = \lfloor \frac x 2 \rfloor \$

<<<$[$1/2]

Attempt This Online!

\$ g(x) = 2x \$

<<<$[$1*2]

Attempt This Online!

For even \$ x \$, \$ f(x) = \frac x 2 \$, so \$ g(f(x)) = x \$.

But for odd \$ x \$, /2 rounds downwards, so \$ f(x) = f(x-1) \$, so \$ g(f(x)) = x - 1 \ne x \$

\$ g(x) \$ is always even, so for all \$ x \$, \$ f(g(x)) = x \$.

Therefore, \$ f(g(x)) = g(f(x)) \$ if and only if \$ x \$ is even.

Zsh, 6 + 6 = 12 bytes

tr 1 2

Attempt This Online!

tr 2 3

Attempt This Online!

The first program replaces 1s with 2s; the second replaces 2s with 3s. These commute only for numbers not containing the digit 1.


Old answer:

Zsh, 10 + 10 = 20 bytes

\$ f(x) = \lfloor \frac x 2 \rfloor \$

<<<$[$1/2]

Attempt This Online!

\$ g(x) = 2x \$

<<<$[$1*2]

Attempt This Online!

For even \$ x \$, \$ f(x) = \frac x 2 \$, so \$ g(f(x)) = x \$.

But for odd \$ x \$, /2 rounds downwards, so \$ f(x) = f(x-1) \$, so \$ g(f(x)) = x - 1 \ne x \$

\$ g(x) \$ is always even, so for all \$ x \$, \$ f(g(x)) = x \$.

Therefore, \$ f(g(x)) = g(f(x)) \$ if and only if \$ x \$ is even.

deleted 1198 characters in body
Source Link
pxeger
  • 25.3k
  • 4
  • 59
  • 146

Zsh, 20 bytes

\$ f(x) = \lfloor \frac x 2 \rfloor \$

<<<$[$1/2]

Attempt This Online!

\$ g(x) = 2x \$

<<<$[$1*2]

Attempt This Online!

For even \$ x \$, \$ f(x) = \frac x 2 \$, so \$ g(f(x)) = x \$.

But for odd \$ x \$, /2 rounds downwards, so \$ f(x) = f(x-1) \$, so \$ g(f(x)) = x - 1 \ne x \$

\$ g(x) \$ is always even, so for all \$ x \$, \$ f(g(x)) = x \$.

Therefore, \$ f(g(x)) = g(f(x)) \$ if and only if \$ x \$ is even.


Almost valid Zsh, 3 + 3 = 6 bytes, \$ Z = \mathbb Z^* \$

rev

Attempt This Online!

This reverses the digits of the input.

cat

Attempt This Online!

This outputs the input unchanged: \$ \operatorname{cat}(x) = x \$

If \$ x \$ is a palindrome, then \$ \operatorname{rev}(x) = x \$, so \$ \operatorname{rev}(\operatorname{cat}(x)) = \operatorname{cat}(\operatorname{rev}(x)) \$.

If \$ x \$ is not a palindrome, then \$ \operatorname{rev}(x) \ne x \$, so \$ \operatorname{rev}(\operatorname{cat}(x)) \ne \operatorname{cat}(\operatorname{rev}(x)) \$.

Clearly there are infinitely many palindromes and non-palindromes, so this must be valid, right?

Unfortunately, no: if the number has trailing zeroes, like \$ 130 \$, then the output is \$ 031 \$. We could just say that the I/O format is "a decimal string with optional leading zeroes", but then the input \$ 07 \$ and \$ 7 \$ would give different outputs.

Zsh, 20 bytes

\$ f(x) = \lfloor \frac x 2 \rfloor \$

<<<$[$1/2]

Attempt This Online!

\$ g(x) = 2x \$

<<<$[$1*2]

Attempt This Online!

For even \$ x \$, \$ f(x) = \frac x 2 \$, so \$ g(f(x)) = x \$.

But for odd \$ x \$, /2 rounds downwards, so \$ f(x) = f(x-1) \$, so \$ g(f(x)) = x - 1 \ne x \$

\$ g(x) \$ is always even, so for all \$ x \$, \$ f(g(x)) = x \$.

Therefore, \$ f(g(x)) = g(f(x)) \$ if and only if \$ x \$ is even.


Almost valid Zsh, 3 + 3 = 6 bytes, \$ Z = \mathbb Z^* \$

rev

Attempt This Online!

This reverses the digits of the input.

cat

Attempt This Online!

This outputs the input unchanged: \$ \operatorname{cat}(x) = x \$

If \$ x \$ is a palindrome, then \$ \operatorname{rev}(x) = x \$, so \$ \operatorname{rev}(\operatorname{cat}(x)) = \operatorname{cat}(\operatorname{rev}(x)) \$.

If \$ x \$ is not a palindrome, then \$ \operatorname{rev}(x) \ne x \$, so \$ \operatorname{rev}(\operatorname{cat}(x)) \ne \operatorname{cat}(\operatorname{rev}(x)) \$.

Clearly there are infinitely many palindromes and non-palindromes, so this must be valid, right?

Unfortunately, no: if the number has trailing zeroes, like \$ 130 \$, then the output is \$ 031 \$. We could just say that the I/O format is "a decimal string with optional leading zeroes", but then the input \$ 07 \$ and \$ 7 \$ would give different outputs.

Zsh, 20 bytes

\$ f(x) = \lfloor \frac x 2 \rfloor \$

<<<$[$1/2]

Attempt This Online!

\$ g(x) = 2x \$

<<<$[$1*2]

Attempt This Online!

For even \$ x \$, \$ f(x) = \frac x 2 \$, so \$ g(f(x)) = x \$.

But for odd \$ x \$, /2 rounds downwards, so \$ f(x) = f(x-1) \$, so \$ g(f(x)) = x - 1 \ne x \$

\$ g(x) \$ is always even, so for all \$ x \$, \$ f(g(x)) = x \$.

Therefore, \$ f(g(x)) = g(f(x)) \$ if and only if \$ x \$ is even.

Source Link
pxeger
  • 25.3k
  • 4
  • 59
  • 146

Zsh, 20 bytes

\$ f(x) = \lfloor \frac x 2 \rfloor \$

<<<$[$1/2]

Attempt This Online!

\$ g(x) = 2x \$

<<<$[$1*2]

Attempt This Online!

For even \$ x \$, \$ f(x) = \frac x 2 \$, so \$ g(f(x)) = x \$.

But for odd \$ x \$, /2 rounds downwards, so \$ f(x) = f(x-1) \$, so \$ g(f(x)) = x - 1 \ne x \$

\$ g(x) \$ is always even, so for all \$ x \$, \$ f(g(x)) = x \$.

Therefore, \$ f(g(x)) = g(f(x)) \$ if and only if \$ x \$ is even.


Almost valid Zsh, 3 + 3 = 6 bytes, \$ Z = \mathbb Z^* \$

rev

Attempt This Online!

This reverses the digits of the input.

cat

Attempt This Online!

This outputs the input unchanged: \$ \operatorname{cat}(x) = x \$

If \$ x \$ is a palindrome, then \$ \operatorname{rev}(x) = x \$, so \$ \operatorname{rev}(\operatorname{cat}(x)) = \operatorname{cat}(\operatorname{rev}(x)) \$.

If \$ x \$ is not a palindrome, then \$ \operatorname{rev}(x) \ne x \$, so \$ \operatorname{rev}(\operatorname{cat}(x)) \ne \operatorname{cat}(\operatorname{rev}(x)) \$.

Clearly there are infinitely many palindromes and non-palindromes, so this must be valid, right?

Unfortunately, no: if the number has trailing zeroes, like \$ 130 \$, then the output is \$ 031 \$. We could just say that the I/O format is "a decimal string with optional leading zeroes", but then the input \$ 07 \$ and \$ 7 \$ would give different outputs.