multiplication

Karatsuba Algorithm without BigInteger usage

蹲街弑〆低调 提交于 2019-12-04 03:08:42
问题 I have been trying to implement Karatsuba Algorithm in java without using BigInteger. My code is applicable only when both the integers are same & have same number of digits. I do not get the correct answer however I get answer which is quite near to the right one. For instance I get 149 when 12*12. I can not figure out what is wrong with my code since I believe I have done everything right (by the book). Here's my code. public static void main(String[] args) { long ans=karatsuba(12,12);

Opencv multiply scalar and matrix

北城余情 提交于 2019-12-04 01:34:57
I have been trying to achieve something which should pretty trivial and is trivial in Matlab . I want to simply achieve something such as: cv::Mat sample = [4 5 6; 4 2 5; 1 4 2]; sample = 5*sample; After which sample should just be: [20 24 30; 20 10 25; 5 20 10] I have tried scaleAdd , Mul , Multiply and neither allow a scalar multiplier and require a matrix of the same "size and type". In this scenario I could create a Matrix of Ones and then use the scale parameter but that seems so very extraneous Any direct simple method would be great! OpenCV does in fact support multiplication by a

Why is uint_least16_t faster than uint_fast16_t for multiplication in x86_64?

↘锁芯ラ 提交于 2019-12-04 00:10:24
问题 The C standard is quite unclear about the uint_fast*_t family of types. On a gcc-4.4.4 linux x86_64 system, the types uint_fast16_t and uint_fast32_t are both 8 bytes in size. However, multiplication of 8-byte numbers seems to be fairly slower than multiplication of 4-byte numbers. The following piece of code demonstrates that: #include <stdio.h> #include <stdint.h> #include <inttypes.h> int main () { uint_least16_t p, x; int count; p = 1; for (count = 100000; count != 0; --count) for (x = 1;

Multiplying using Bitwise Operators

与世无争的帅哥 提交于 2019-12-03 21:12:23
I was wondering how I could go about multiplying a series of binary bits using bitwise operators. However, I'm interested in doing this to find the a decimal fraction value for the binary value. Here's an example of what I'm trying to do: Given, say: 1010010, I want to use each individual bit so that it will be computed as: 1*(2^-1) + 0*(2^-2) + 1*(2^-3) + 0*(2^-4)..... Though I'm interested in doing this in ARM assembly, having an example in C/C++ will still help as well. I was thinking of performing a loop with a counter, where each time the loop iterates, a counter will increment, the value

detecting multiplication of uint64_t integers overflow with C

大兔子大兔子 提交于 2019-12-03 16:51:57
问题 Is there any efficient and portable way to check when multiplication operations with int64_t or uint64_t operands overflow in C? For instance, for addition of uint64_t I can do: if (UINT64_MAX - a < b) overflow_detected(); else sum = a + b; But I can not get to a similar simple expression for multiplication. All that occurs to me is breaking the operands into high and low uint32_t parts and performing the multiplication of those parts while checking for overflow, something really ugly and

modular multiplication of large numbers in c++

时光总嘲笑我的痴心妄想 提交于 2019-12-03 14:30:20
问题 I have three integers A, B (less than 10^12) and C (less than 10^15). I want to calculate (A * B) % C . I know that (A * B) % C = ((A % C) * (B % C)) % C but say if A = B = 10^11 then above expression will cause an integer overflow. Is there any simple solution for above case or I have to use fast multiplication algorithms. If I have to use fast multiplication algorithm then which algorithm I should use. EDIT: I have tried above problem in C++ (which does not cause overflow, not sure why),

Why does (int)(33.46639 * 1000000) return 33466389?

岁酱吖の 提交于 2019-12-03 10:02:10
(int)(33.46639 * 1000000) returns 33466389 Why does this happen? Floating point math isn't perfect. What every programmer should know about it. Floating-point arithmetic is considered an esoteric subject by many people. This is rather surprising because floating-point is ubiquitous in computer systems. Almost every language has a floating-point datatype; computers from PCs to supercomputers have floating-point accelerators; most compilers will be called upon to compile floating-point algorithms from time to time; and virtually every operating system must respond to floating-point exceptions

Understanding Schönhage-Strassen algorithm (huge integer multiplication)

匆匆过客 提交于 2019-12-03 07:08:07
I need to multiply several 1000s digits long integers as efficiently as possible in Python. The numbers are read from a file. I am trying to implement the Schönhage-Strassen algorithm for integer multiplication, but I am stuck on understanding the definition and mathematics behind it, specially the Fast Fourier Transform. Any help to understand this algorithm, like a practical example or some pseudo-code would be highly appreciated. Chapter 4.3.3 of Knuth's TAOCP describes it and also has some FFT pseudocode in other chapters that could be used for this. 1000 digits is "small" for Schönhage

How do I efficiently multiply a range of values of an array with a given number?

大城市里の小女人 提交于 2019-12-03 04:00:56
The naive way would be to linearly iterate the range and multiply with each number in the range. Example: Array: {1,2,3,4,5,6,7,8,9,10}; Multiply index 3 to index 8 with 2. Assuming one based index. Result array should be : {1,2,6,8,10,12,14,16,9,10}; I know that Binary indexed tree can be used for the 'sum' part. How can I efficiently multiply a given range with a number? If you want to actually modify the array, you can't do better than the naive linear algorithm: you have to iterate the entire range and modify each index accordingly. If you mean something like, you have update operations

Raising a matrix to the power method JAVA

删除回忆录丶 提交于 2019-12-02 20:59:03
问题 I am having a really hard time creating a method to raise a matrix to the power. I tried using this public static int powerMethod(int matrix, int power) { int temp = matrix ; for (int i = power; i == 1; i--) temp = temp * matrix ; return temp ; but the return is WAYYY off. Only the first (1,1) matrix element is on point. I tried using that method in a main like so // Multiplying matrices for (i = 0; i < row; i++) { for (j = 0; j < column; j++) { for (l = 0; l < row; l++) { sum += matrix[i][l]