-1

I'm trying to think of some interesting, reusable way to implement big integers using passed amount of bytes or resizing themselves when needed. I have no idea how to make it optimal in any way tho. Are there any tricks I could use, or do I have to simply work on those numbers bit by bit while adding/multiplying/dividing?

edit: if it is important, I need it to safe text as number in base 10 so I can play with some ideas for encrypting it

Crimsoon
  • 83
  • 8
  • try things. vector would seem like a useful building block. SO will not offer a lot of help becuase the question is too vague – pm100 Jun 20 '22 at 16:54
  • 1
    Instead of applying elementary-school arithmetic rules to numbers written in base-10 (the way you learned them) or base-2 (the bit-by-bit approach you propose), apply in base-256 or base-65536 (each "digit" is an entire byte or even several of them). As long as your individual operations fit inside the native machine word, you can handle entire groups of bits at once. – Ben Voigt Jun 20 '22 at 16:55
  • @BenVoigt it actually sounds like a fun thing to try – Crimsoon Jun 20 '22 at 17:03
  • Do you have an N byte integer solution already implemented? I used `std::string` for the internal backing store representation of mine. But mine is not *optimal* by any stretch. I just made mine for fun, so it's focus is simplicity and straightforwardness. (And I did not implement division. Didn't need it for my problem.) – Eljay Jun 20 '22 at 17:34
  • 1
    Knuth's *Seminumerical Algorithms* is an old book, but an excellent one, and has a very detailed treatment of bignum arithmetic, progressing from classical algorithms to advance FFT-based techniques. – President James K. Polk Jun 20 '22 at 22:52
  • 1
    Here is a different [free online reference](https://github.com/bitkeeper-scm/tcl/blob/master/libtommath/tommath.pdf), and you might look at the code for libtommath as well. It's designed to be readable rather than as fast as possible. – President James K. Polk Jun 20 '22 at 22:54

1 Answers1

4

Use The GNU Multiple Precision Arithmetic Library. If you try to reinvent the wheel you will end up with a square.

Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
  • I totally agree, learn about reuse of tested code, so you can work on the other things like encrypting (which have their own libraties with reviewed algorithms). Unless ofcourse you want to this as a training excercise. In that case be prepared to write a lot of unit tests too. – Pepijn Kramer Jun 20 '22 at 16:58
  • I'm not exactly trying to reinvent it, I just like to solve my problems from ground up in order to learn things I wouldn't normally have any motivation to learn – Crimsoon Jun 20 '22 at 16:59
  • I would compute PI and the mandelbrot fractal as test cases and for benchmarking. Always a good workout for bignum libraries and easy to verify results. – Goswin von Brederlow Jun 20 '22 at 17:00
  • 2
    @Crimsoon If you want to solve it yourself then why are you asking for finished solutions? If it's to learn things then why ask for optimal solutions instead of things that teach you stuff? The question is quite contradictory. – Goswin von Brederlow Jun 20 '22 at 17:02
  • @GoswinvonBrederlow I'm asking about it to know if there is anything I should know, and to read about couple diffrent aproaches so I actually know anything about what I work on – Crimsoon Jun 20 '22 at 17:09
  • 2
    Then the docs for GMP should be a good read for you. It discusses all the optimal approaches. – Goswin von Brederlow Jun 20 '22 at 17:16
  • 1
    Note: there are many big number libraries out there. GMP is a goodie, but if it doesn't hit all of your requirements, look around for others before rolling your own. – user4581301 Jun 20 '22 at 17:24