How to multiply two numbers using one variable?

Started by
13 comments, last by Antheus 13 years, 1 month ago
One correct solution is:int value = 42;

This solves the problem of multiplying n numbers using a single variable. In this particular case, n = 2.



readInt();
[/quote]
No input can be read.

Input is either an empty set or a set of values.

Any set of values has non-zero entropy, hence requires more than 0 bits to store. As such, any input would be an extra variable.

Without this restriction, I might as well solve the problem using:int values[INFINITE][INFINITE};

result = values[a];
How to store infinite number of results would be left as exercise to the reader. And any real world implementation would require a Turing machine with infinite tape.
Advertisement
Found out solution today, you can store multiple numbers in one number as [prime_number]^[number1] * [next_prime_number]^[number2]*... etc.. finding multiplycant of these numbers is then trivial.

Found out solution today, you can store multiple numbers in one number as [prime_number]^[number1] * [next_prime_number]^[number2]*... etc.. finding multiplycant of these numbers is then trivial.


Um...

If that is allowed:struct Parameters {
int a;
int b;
boost::spirit just_because_we_can;
static foo;
std::vector<std::string> lets_burn_heap_memory;
};

Parameters only_one;



Storing multiple numbers in same "variable" is an encoding, just like two variables are encoded in certain way in memory. For example:int32_t a;
int32_t b;

// will have same representation in memory as
int64_t ab;
The memory layouts of these two will be identical, but the values will not be.


And there is another problem with prime numbers. They get big fast. 32 or 64 bits will not be enough for anything except single digit numbers.

Any bignum class however needs several variables to represent the value (number of bits used, pointer to data). And multiplication of these values requires even more variables for FFT or even just naive multiplication.
Its just school homework aimed on recursion and simulating some sort of machine, not some solution bussiness project.

Its just school homework aimed on recursion and simulating some sort of machine, not some solution bussiness project.


Obviously.

It's just one of those nonsensical questions which requires one to guess what exactly the asker meant, not how to actually solve the problem. Similar question was/is used in interviews, allegedly at Google as well. It's not really a good question.

In information theory there is a distinction between information and data. The "one variable" is data. Two numbers is two distinct pieces of numbers. We can write 42 or XXXXII or 0x2a. They all represent same amount of information but use different data representations.

By using primes to encode two numbers we still have two numbers.

If we treat these two numbers as information, then multiplication is abstract concept and requires no storage or computer language variables. a=b*c is just that.
If we treat these two numbers as data, then there is always a way to encode them into something we can call "one". Be it a struct, tuple, or prime-encoding. Either way, we shall require at least as many bits as sum of entropy of both numbers.


The prime solution requires agreeing on two primes. Let's say 3 and 7. This becomes implicitly encoded into our "single" variable. Which is exactly the answer I gave originally. But instead of combining 3*a+7*b, I multiplied a*b and stored that.

This topic is closed to new replies.

Advertisement