9

Well, I've just started doing puzzles and it's so annoying to see puzzles which are easy to do but also need to handle very large numbers. That's the problem.

Say I have to deal with numbers like 10 ^ 6 / 2147483647 / etc. Need to do arithmetic operations on!

However, I feel that these shouldn't be handled in brute force way but the math theorems/ statements just don't click in my mind at times.

How could I possible handle such puzzles (shouldn't use external lib)

Robert Harvey
  • 200,592
0cool
  • 91

5 Answers5

7

Large numbers are used in code challenges because they are hard to deal with, its the same reason they are used for encryption. There are BigInt classes for most languages that can help make them more manageable. Most code challenges can be solved while minimally dealing with large numbers though, there are clever solutions that find ways to avoid having to work with big numbers until absolutely necessary.

Ryathal
  • 13,486
  • 1
  • 36
  • 48
4

While the exact solution depends on the actual problem, there are various approaches you can try to take before simply calculating in brute force using an arbitrary or multiple precision math library (BigInt, GMP, MPFR, ARPREC, etc.).

First, use some math. Can the expressions be simplified? You said that the source of these tasks involves puzzles, so I would be very inclined to look at this approach for a solution, as this may be a factor in the puzzles' aha moment. Equations with factorials, such as binomial probabilities can typically be simplified or calculated (indirectly) using mathematical techniques rather than brute force.

Factoring the numbers and cancelling common factors would be one of the first things I would try, by hand if need be. A multiple precision calculator can helpful.

Would re-framing the question or its values in either a different base (e.g. binary, hexadecimal) or a difference scale (e.g. logarithmic base 2, 10, or e -- natural) make the values easier to deal with? One example of a logarithmic scale, is the decibel, used for RF, and audio levels.

Using techniques not as commonly taught nowadays, but well known amongst programmers, engineers, mathematicians who are familiar with the slide rule can sometime be helpful.

Depending on the question, doing an approximation first can sometimes lead you towards the correct answer by preventing the minutiae from distracting you from attacking the problem creatively.

For your example; calculate a related (approximate), but simplified equation.

 \frac{10^6}{2147483647}  ≅  \frac{1 * 10^6}{2 * 10^9} \frac{1 * 10^6}{2 * 10^9} = \frac{1}{2} * \frac{10^6}{10^9} \frac{1}{2} * \frac{10^6}{10^9} = 0.5 * 10^{-3} = 0.0005

which is very close to the correct or exact answer

Another "trick" is to use modulo (mod, %, modulo, a \bmod n ) which is one of my favourite ways to reduce numbers, so if you know some basic abstract algebra you can sometimes work with modular arithmetic.

That is an off-the-cuff, very rough guide to how I would approach a "puzzle" equation or programming problem that involves large numbers.

mctylr
  • 1,163
  • 6
  • 12
1

Every programming language supports the use of big numbers: some have this support directly in the standard library, others have special libraries written for this purpose (even several ones).

As long as you are fine with integer numbers you should look for something like BigInteger that is present in many languages. If you need real numbers, then you should look for arbitrary precision arithmetics libraries.

Here are some examples:

1

If you are working your way through a puzzle challenge and large numbers start to show up then the challenge isn't so much finding the answer (as others have noted, libraries exist for working with large numbers) but rather the challenge is to develop your own library to work with large numbers. This can be quite challenging as you are usually going to have to split the number up in some way across several different memory spaces. For integers this is fairly straightforward, if time consuming to read up on and implement; however, support for floating point values will definitely improve your math skills!

To get you started, the following sites have some open source code that you might want to read through to see how they work with the numbers:

Plus, the Wikipedia page on arbitrary-precision arithmetic can send you off on some interesting directions as well.

rjzii
  • 11,304
0

Those really aren't particularly huge numbers. Your example is well within the range supported by a C 'unsigned int' or 'long' on a typical x86 compiler, and vanilla configurations of python will automatically support arbitrary precision arithmetic. Part of your problem may be that in Python "^" is not the exponentiation operator, it's the bitwise 'XOR' operator. Try using 10**6 instead. Of course, using integer division the answer will be zero. Do you want floating point division instead?