Take into account that
123456 * 654321 * 80779853 == 6525384681074833728
but std::numeric_limits<long>::max() is equal to 2147483647 on 32-bit processors (integer overflow is not detected).
Try long long type (it's supported by modern compilers now), its max value is 9223372036854775807.
Otherwise try one of big int class libraries...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
It seems you don't understand that no need in Karatsuba algorithm while you are trying to get unsigned long results: use built-in arithmetic - that's all. But if the result is greater than unsigned long max value you can't return unsigned long value from Karatsuba function! No need in Karatsuba algorithm if you have bigint library: there is bigint multiplication in every such library.
Therefore all your codes are senseless from the beginning. Start your design from the big int representation. See, for example, http://en.wikipedia.org/wiki/Karatsuba_algorithm .
Apropos: the simplest (but ineffective) arbitrary precision integer representation is a simple text string. Try to implement Karatsuba algorithm for this representation.
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348