This question already has answers here:
Why is exponentiation applied right to left?
(4 answers)
Closed 4 years ago.
Python 2.7.13 interpreter hangs on calculating
1 ** 2 ** 3 ** 4 ** 5
But why?
I think the problem here is your understanding of exponentiation associativity:
>>> 2**3**4
2417851639229258349412352
>>> 2**(3**4)
2417851639229258349412352
>>> (2**3)**4
4096
You need to reed from right to left unlike division associativity.
>>> 2/3/4
0.16666666666666666
>>> 2/(3/4)
2.6666666666666665
>>> (2/3)/4
0.16666666666666666
Related
This question already has answers here:
modulo of a number - python vs c#
(3 answers)
Mod of negative number is melting my brain
(15 answers)
Closed 7 months ago.
I'm trying to get % result of ( -1%26 )
the result should be 25
in python it's right
but C# gives -1 as result
why and how to get 25 in C# not -1
You can use a different kind of modulus function, like this:
int Modulus(int a, int b) {
return ((a % b) + b) % b;
}
Console.WriteLine(Modulus(-1, 26)); // prints 25
This question already has answers here:
Division in Python 3 gives different result than in Python 2
(3 answers)
Closed 6 years ago.
I was given the task to compute lcm of any two numbers.I have coded in python.The problem is when i compiled it under python2.7, i got a result which is different from , when i compiled under python3.
import sys
def gcd(a,b):
if b == 0:
return a
remainder = a % b
return gcd(b,remainder)
def lcm(a, b):
return int( (a*b) / gcd(a,b))
if __name__ == '__main__':
input = sys.stdin.read()
a, b = map(int, input.split())
print(int(lcm(a, b)))
Input
226553150 1023473145
Output
46374212988031352 (under python3.5)
46374212988031350 (under python2.7)
Can someone help me ?
In Python 2 the division operator / will perform integer division, and not float division, when dealing with two integers. You can force the Python 3 behavior in Python 2 by importing division from __future__
>>> from __future__ import division
>>> 2/3
1.5
This question already has answers here:
Why does 1+++2 = 3?
(6 answers)
Closed 7 years ago.
I am new to python and I read that there is no ++ operator in Python but I am not able to understand the below code.
>>>print (2++3)
>>>5
+ and - act as unary as well as binary operators. So,
a ++ b is same as a + (+b)
a -+ b is same as a - (+b)
a -- b is same as a - (-b)
a +- b is same as a + (-b)
As can be seen below
>>> 2++3
5
>>> 2-+3
-1
>>> 2--3
5
>>> 2+-3
-1
This is not the ++ operator. Your code is interpreted as follows:
2 + (+3)
Now since +3 is 3, the final result is 5. For fun, try the following:
2++++++++++++++3
2+++++++++++++++++++++-8
See Expressions for details.
I ran into a curious issue in both Python 2 and Python 3.
>>> 1**4**4**4
1L
which seems fine, but when I do this:
>>> 1**4**4**4**4
it swamps the CPU and never finishes.
Why?
I also ran these to see if it was something with the power function, or with the ** operator and it seems to be just the ** operator.
>>> (((((1**4)**4)**4)**4)**4)
1
>>> pow(pow(pow(pow(pow(pow(1,4),4),4),4),4),4)
1
>>> pow(pow(pow(pow(pow(pow(1.0,4),4),4),4),4),4)
1.0
>>> pow(pow(pow(pow(pow(pow(1L,4),4),4),4),4),4)
1L
>>> 1L**4**4**4
1L
I also tried another language and it seems to be just Python.
Why can't it evaluate this in microseconds? Can someone explain what it's doing with the CPU time?
Is there something non-intuitive about the order of operations that I am not understanding?
With the ** operator, the binding rules are such that in unparenthesized expressions the right-hand side is evaluated first.
Your 1 ** 4 ** 4 ** 4 ** 4 is evaluated in the following order:
1 ** (4 ** (4 ** (4 ** 4)))
and it is producing and allocating the memory for the huge number on the right that takes all the time:
>>> 4 ** 4 ** 4
13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096L
>>> 4 ** 4 ** 4 ** 4
# ... wait a long time as Python allocates GBs of memory ...
Quoting the ** documentation:
Thus, in an unparenthesized sequence of power and unary operators, the operators are evaluated from right to left
Mathematically, exponentiation is right associative, so
1**4**4**4**4
is not the same as
(((((1**4)**4)**4)**4)**4)
Because it is not doing
(((((1**4)**4)**4)**4)**4)
It is doing
1**(4**(4**(4**(4**4))))
Note that the latter has to compute a HUGE number before simply raising 1 to that result.
Right associativity:
>>> 2 ** 2 ** 3
256
>>>
It's slow because it has to evaluate 4**4**4**4, which is does by multiplication to get the exact result.
(Don't forget that exponentiation is right-associative.)
P.S. 4**4**4 is 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096, so computing 4**4**4**4 by multiplication is going to take a very, very long time.
This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 8 years ago.
How can I make this function return and print a float:
x = input ("Enter 5 numbers:")
def average(x):
return sum(x) / len(x)
print average(x)
In python 2.x, int object divided by int yields int.
You should convert one (or both) of the operand to float to get float result:
>>> 10 / 2
5
>>> float(10) / 2
5.0
Or turn on true division using __future__ module:
>>> from __future__ import division
>>> 10 / 2
5.0