-1

Hi I am doing an algorithm question require get the full result of 5208334^2, which is 27126743055556 I was able to do it with by represent integer using Charracter array. However can we have any better way (shorter or faster) to do that? any idea is welcome ?

Updated: For my case, both long long and int64 work, just that I did not cast value before return:

int val (int n1, n2) {
........
return (long long) n1 * n2; 

}
sillyboy
  • 29
  • 6
  • 2
    `std::vector`, so, essentially an easily resized character array. unless you want a `std::bitset` to do some crazy magic. – AndyG Jun 18 '15 at 01:13
  • 4
    Use `int64_t`. Or maybe my own [Bigint class](http://stackoverflow.com/a/22004815/5987). – Mark Ransom Jun 18 '15 at 01:18
  • 2
    Tempted to close as a dupe of: http://stackoverflow.com/q/8146938/179910 (among many others). – Jerry Coffin Jun 18 '15 at 01:19
  • 2
    That's only 45 bits, if 27126743055556 is as high as you need to go 64bit should be fine. – Christopher Ian Stern Jun 18 '15 at 01:35
  • __int128_t up to 40 digits – Filip Bulovic Jun 18 '15 at 01:46
  • A *Charracter* array is only limited by the memory available. Using other means means having to check if the data type has overflowed. With *Charracter* array, you check for overflow with each digit. – Thomas Matthews Jun 18 '15 at 02:24
  • This is a complete C and C++ library for arbitrary precision. https://gmplib.org/ – KeithSmith Jun 18 '15 at 02:26
  • look here [Fast bignum square computation](http://stackoverflow.com/q/18465326/2521214) for simple and more advanced approaches. btw on 80386+ you can use asm 32 bit multiply it returns 64 bit result see [ALU32](http://stackoverflow.com/a/26603589/2521214) – Spektre Jun 18 '15 at 04:32
  • State your measure for `better`. Representing your numbers base 5208334(base 10), the result is 100. – greybeard Jun 18 '15 at 08:57

1 Answers1

0

This number fits into long long(present in GCC and after c++11) type or int64(for some other compilers before c++11). Thus the simplest solution is to use this type.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176