Questions tagged [multiplication]

Multiplication is the mathematical operation of scaling one number by another. It is an elementary operation of most programming languages and is typically denoted by the symbol *.

Multiplication is the mathematical operation of scaling one number by another.

It is an elementary arithmetic operation in most programming languages along with addition, subtraction, division and sometimes modulo.

It is typically denoted by * (asterisk symbol) in programming, and × (cross symbol) in math.

Example (math): multiplication of numbers 3 and 4

3 × 4 = 12

See also: Multiplication on Wikipedia

2530 questions
763
votes
10 answers

Create list of single item repeated N times

I want to create a series of lists, all of varying lengths. Each list will contain the same element e, repeated n times (where n = length of the list). How do I create the lists, without using a list comprehension [e for number in xrange(n)] for…
chimeracoder
  • 20,648
  • 21
  • 60
  • 60
329
votes
19 answers

Is multiplication and division using shift operators in C actually faster?

Multiplication and division can be achieved using bit operators, for example i*2 = i<<1 i*3 = (i<<1) + i; i*10 = (i<<3) + (i<<1) and so on. Is it actually faster to use say (i<<3)+(i<<1) to multiply with 10 than using i*10 directly? Is there any…
eku
  • 3,357
  • 3
  • 19
  • 20
326
votes
5 answers

Extracting bits with a single multiplication

I saw an interesting technique used in an answer to another question, and would like to understand it a little better. We're given an unsigned 64-bit integer, and we are interested in the following…
NPE
  • 486,780
  • 108
  • 951
  • 1,012
273
votes
15 answers

How can I multiply all items in a list together with Python?

Given a list of numbers like [1,2,3,4,5,6], how can I write code to multiply them all together, i.e. compute 1*2*3*4*5*6?
user1897814
  • 2,755
  • 2
  • 13
  • 3
261
votes
9 answers

What's the function like sum() but for multiplication? product()?

Python's sum() function returns the sum of numbers in an iterable. sum([3,4,5]) == 3 + 4 + 5 == 12 I'm looking for the function that returns the product instead. somelib.somefunc([3,4,5]) == 3 * 4 * 5 == 60 I'm pretty sure such a function exists,…
Patrick McElhaney
  • 57,901
  • 40
  • 134
  • 167
189
votes
15 answers

How to perform element-wise multiplication of two lists?

I want to perform an element wise multiplication, to multiply two lists together by value in Python, like we can do it in Matlab. This is how I would do it in Matlab. a = [1,2,3,4] b = [2,3,4,5] a .* b = [2, 6, 12, 20] A list comprehension would…
xxjjnn
  • 14,591
  • 19
  • 61
  • 94
109
votes
15 answers

How can I multiply and divide using only bit shifting and adding?

How can I multiply and divide using only bit shifting and adding?
Spidfire
  • 5,433
  • 6
  • 28
  • 36
94
votes
14 answers

Catch and compute overflow during multiplication of two large integers

I am looking for an efficient (optionally standard, elegant and easy to implement) solution to multiply relatively large numbers, and store the result into one or several integers : Let say I have two 64 bits integers declared like this : uint64_t a…
Ben
  • 7,372
  • 8
  • 38
  • 46
78
votes
6 answers

Multiply rows of matrix by vector?

I have a numeric matrix with 25 columns and 23 rows, and a vector of length 25. How can I multiply each row of the matrix by the vector without using a for loop? The result should be a 25x23 matrix (the same size as the input), but each row has been…
pixel
  • 24,905
  • 36
  • 149
  • 251
70
votes
6 answers

How to multiply all integers inside list

Hello so I want to multiply the integers inside a list. For example; l = [1, 2, 3] l = [1*2, 2*2, 3*2] output: l = [2, 4, 6] So I was searching online and most of the answers were regarding multiply all the integers with each other such…
Andre
  • 1,601
  • 6
  • 19
  • 19
61
votes
12 answers

Is integer multiplication really done at the same speed as addition on a modern CPU?

I hear this statement quite often, that multiplication on modern hardware is so optimized that it actually is at the same speed as addition. Is that true? I never can get any authoritative confirmation. My own research only adds questions. The speed…
exebook
  • 32,014
  • 33
  • 141
  • 226
57
votes
4 answers

How to multiply individual elements of a list with a number?

S = [22, 33, 45.6, 21.6, 51.8] P = 2.45 Here S is an array How will I multiply this and get the value? SP = [53.9, 80.85, 111.72, 52.92, 126.91]
bharath
  • 725
  • 1
  • 5
  • 7
53
votes
2 answers

Why is division more expensive than multiplication?

I am not really trying to optimize anything, but I remember hearing this from programmers all the time, that I took it as a truth. After all they are supposed to know this stuff. But I wonder why is division actually slower than multiplication?…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
50
votes
3 answers

pandas dataframe multiply with a series

What is the best way to multiply all the columns of a Pandas DataFrame by a column vector stored in a Series? I used to do this in Matlab with repmat(), which doesn't exist in Pandas. I can use np.tile(), but it looks ugly to convert the data…
jianpan
  • 621
  • 1
  • 5
  • 6
46
votes
4 answers

Python list multiplication: [[...]]*3 makes 3 lists which mirror each other when modified

Why this is happening? I don't really understand: >>> P = [ [()]*3 ]*3 >>> P [[(), (), ()], [(), (), ()], [(), (), ()]] >>> P[0][0]=1 >>> P [[1, (), ()], [1, (), ()], [1, (), ()]]
KFL
  • 17,162
  • 17
  • 65
  • 89
1
2 3
99 100