This question already has answers here:
What is the result of % in Python?
(20 answers)
Closed 3 years ago.
I have just now started learning python from Learn Python 3 The Hard Way by Zed Shaw. In exercise 3 of the book, there was a problem to get the value of 100 - 25 * 3 % 4. The solution to this problem is already mentioned in the archives, in which the order preference is given to * and %(from left to right).
I made a problem on my own to get the value of 100 - 25 % 3 + 4. The answer in the output is 103.
I just wrote: print ("the value of", 100 - 25 % 3 + 4), which gave the output value 103.
If the % is given the preference 25 % 3 will give 3/4. Then how the answer is coming 103. Do I need to mention any float command or something?
I would like to know how can I use these operations. Is there any pre-defined rule to solve these kinds of problems?
Actually, the % operator gives you the REMAINDER of the operation.
Therefore, 25 % 3 returns 1, because 25 / 3 = 8 and the remainder of this operation is 1.
This way, your operation 100 - 25 % 3 + 4 is the same as 100 - 1 + 4 = 103
The % operator is used to find the remainder of a quotient. So 25 % 3 = 1 not 3/4.
From the docs you can find the full python operation order.
Then, % operator is called the modulus operator and return the remainder of the integuer division:
11 % 2 == 1
You may want to take a look at divmod
Related
This question already has answers here:
What is the result of % in Python?
(20 answers)
Closed 1 year ago.
I ran this code
x = 2
y = 6
print(x%y)
Output
2
What did '%' exactly do?
It's the modulo operator, it gives the rest from a division that doesn't give an integer( or 0 if the division gives an integer)
So 10 modulo 4 is 2 since 10-4*2=2
This question already has answers here:
Numpy is calculating wrong [duplicate]
(2 answers)
Closed 3 years ago.
I am attempting to solve problem 8 of projecteuler. I am having difficulty understanding exactly why my code is not outputting the correct solution. I am aware that the solution to this problem is above the 32 bit maximum, but I do not know how to allow python to work with numbers above that within my code.
For reference, the original question states : "Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?"
from numpy import prod
f = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
z = list(int(i) for i in str(f))
a1 =[]
start = 0
end = start + 13
while end <= len(z):
a1.append(prod(z[start:end]))
start+=1
end+=1
a = a1.index(max(a1))
print(a1[a]) #prints the product solution
print('---')
dimlen=end-start
newstart = a
newend=a+dimlen
print(z[newstart:newend]) #prints the integers that build the solution
I keep getting the number 2091059712, (the solution is 23514624000)
I think it might be numpy.prod. It might be preserving the input type and wrapping the value. Try using:
def prod(it):
p = 1
for m in it:
p *= m
return p
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
For example:
x = (y*6) % 26 #this is a consistent formula
18 = (y*6) % 26
How would I solve for y? Or is there a way to reverse a % operation easily?
You can't really reverse modulo arithmetic like that. Modulo arithmetic returns basically equivalence groups. Think about a calendar...
Calendars ( and clocks ) are mod 12.
3 months after October is January, ie (10 + 3) % 12 == 1
15 months after October is January. ie (10 + 15) % 12 == 1
etc...
so your question seems to ask if you can tell what year it is by knowing what calendar month it is, and this is of course impossible
There is no exact way to reverse the operation as you have lost information as % only gives you the remainder when two numbers are divided.
It is like saying that if I divide x by 6 I get a whole number and a remainder of 3. I cant say what X is - it could be 9, it could be 15 or any of an infinite number of possibilities. To work it out you need the whole number as well as the modulus.
Why are you trying to do this - perhaps we can help if you tells us more of the problem you are trying to solve.
18 = (y*6) % 26
means that
y*6=n*26+18 where n can be any integer value. So
y=n* 26/6 + 3
or
y=n* 13/3 + 3
You may now say that y is an integer, too. This only happens for
y=n* 13 + 3
so, y= 3, 16, 29, 42, ...
This question already has answers here:
Generate random integers between 0 and 9
(22 answers)
Closed 8 years ago.
I've read the manual for pseudo-randomness in Python, and to my knowledge, you can only generate numbers up to a given maximum value, i.e. 0-1, 0-30, 0-1000, etc. I want to:
a) Generate a number between two ints, i.e. 5-55, and
b) Only include multiples of 5 (or those ending in 5 or 0, if that's easier)
I've looked around, and I can't find anywhere that explains this.
Create an integer random between e.g. 1-11 and multiply it by 5. Simple math.
import random
for x in range(20):
print random.randint(1,11)*5,
print
produces e.g.
5 40 50 55 5 15 40 45 15 20 25 40 15 50 25 40 20 15 50 10
>>> import random
>>> random.randrange(5,60,5)
should work in any Python >= 2.
If you don't want to do it all by yourself, you can use the random.randrange function.
For example import random; print random.randrange(10, 25, 5) prints a number that is between 10 and 25 (10 included, 25 excluded) and is a multiple of 5. So it would print 10, 15, or 20.
The simplest way is to generate a random nuber between 0-1 then strech it by multiplying, and shifting it.
So yo would multiply by (x-y) so the result is in the range of 0 to x-y,
Then add x and you get the random number between x and y.
To get a five multiplier use rounding. If this is unclear let me know and I'll add code snippets.
This question already has answers here:
What is the result of % in Python?
(20 answers)
Closed 9 years ago.
I'm working on the exercises in Learning Python the Hard Way and I am wondering what the % function does. I am getting the wrong answer when i count my eggs and hoping understanding this will help me understand what I'm doing wrong.
I can't really tell why your code is broken because you haven't shown anybody what your code is. Please post samples and links next time.
Python % is used in two places, one is mathematical (the modulo operator), and the other has to do with formatting text. I'm going to assume "counting eggs" means the math way.
The modulo operator in X % Y means "Divide X by Y and give me the remainder." So:
10 % 2 == 0
10 % 3 == 1
10 % 11 == 10
That is the the modulo operator