0

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?

Bernhard Barker
  • 50,899
  • 13
  • 85
  • 122

3 Answers3

1

The function f(x, y) = (2 ^ x) * (3 ^ y) is injective for positive integers.

Paul Hankin
  • 44,768
  • 11
  • 79
  • 97
0

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.

pjs
  • 16,350
  • 4
  • 23
  • 44
-1

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?

Community
  • 1
  • 1
xv47
  • 948
  • 3
  • 12
  • 17