Does there exists any function or algorithm that can take two numeric values as input and generate a variable data output in the way we can generate two digits back?
-
I am trying to write this in c++. – Katolog Katolog Nov 11 '13 at 19:42
-
1Can you add an example of what you mean, along with your attempt at solving the problem? – Bernhard Barker Nov 11 '13 at 19:49
-
1Without specifics about the number type and their range, it's impossible to answer this question. Please edit your question to clarify what you're trying to do. – Jim Mischel Nov 11 '13 at 21:12
3 Answers
The function f(x, y) = (2 ^ x) * (3 ^ y)
is injective for positive integers.

- 54,811
- 11
- 92
- 118
You didn't specify a language originally, so I did it in Ruby:
def combine(n1, n2)
(n1.to_s + n2.to_s + ("%02d" % n2.to_s.length).to_s).to_i
end
combine(4321, 76) # => 43217602
This concatenates stringified versions of the two numbers with a 0-padded 2-digit length for the second number. Decompose by taking the result modulo 100 to find out how to break up the leading digits of the encoding. Since Ruby has arbitrary precision integer arithmetic, this will work for input up to 99 digits for the second value. It should be pretty straightforward to translate to C++, but you'll probably want to keep the concatenation as a string rather than converting it back to an integer as I do at the end because of int/long size limitations.

- 18,696
- 4
- 27
- 56
Do you mean to output them as 2 separate variables? Python offers something like that as listed here: How do you return multiple values in Python?