42
6
For this challenge you need to implement two functions, f and g, on the integers, such that f ∘ g is a strictly decreasing function while g ∘ f is a strictly increasing function. In other words, if you take any two integers a < b, then f(g(a)) > f(g(b)) and g(f(a)) < g(f(b)). There are no restrictions on f and g individually, except that they must each map one integer to another integer.
Please include a short description of f and g and an argument for why they have the required property.
Credit: This challenge was inspired by a problem in the 2011 Romanian Master of Mathematics competition (which asks the same thing but on the real numbers, instead of integers). If you really want spoilers, you now know what to search for.
Rules
The word "function" in this challenge should be taken in the mathematical sense of mapping one integer to another: you may either write two programs or two functions and use any of the standard methods of receiving input and providing output, as usual. You may use string representations of integers instead of actual integer variables, but the types of input and output should be identical, so that the functions can be composed without manually converting types in between. Remember that conceptually, f and g still need to be functions on ℤ, so you can't cheat by using two different string representations of the same number or anything like that.
Remember that functions may be unnamed, as long as their name isn't needed by itself or another function you define. If you do name one or both of the functions, you may assume that they exist in the same program, so that they may refer to each other in their implementation (e.g
def f(x): return -g(x)
in Python).The usual integer overflow rules apply: your solution must be able to work for arbitrarily large integers in a hypothetical (or perhaps real) version of your language in which all integers are unbounded by default, but if your program fails in practice due to the implementation not supporting integers that large, that doesn't invalidate the solution.
You may use any programming language, but note that these loopholes are forbidden by default.
This is code-golf, so your score is the sum of the number of bytes of both functions and the shortest valid answer wins.
May the functions return a string? – Matthew Roh – 2017-04-06T12:11:16.647
@SIGSEGV I'd say yes, but only if they also take a string as input, so they can be composed without having to insert any type conversion. – Martin Ender – 2017-04-06T12:12:09.213
Aww darnit, I tried conversion to string to make the other function unable to edit the results further. – Matthew Roh – 2017-04-06T12:13:26.327
I deleted my answer. you say There are no restrictions on f and g individually, except that they must each map one integer to another integer. Therefore if both map integers to lists and lists to integers this is not valid right? – Fatalize – 2017-04-06T12:15:42.513
1@Fatalize Correct. Each must be a function of type ℤ → ℤ. – Martin Ender – 2017-04-06T12:17:03.040
Can the functions be piecewise functions? – Anthony Pham – 2017-04-06T13:52:29.277
@AnthonyPham If you mean you may use conditionals in their definitions, then yes of course. Any computable functions on the integers are allowed. – Martin Ender – 2017-04-06T13:53:31.347
Does it have to work with both negative and positive integers, or can it work with only positive? – Bijan – 2017-04-08T17:06:28.623
1@Bijan both positive and negative. – Martin Ender – 2017-04-08T17:07:15.653