getting ZeroDivisionError: integer division or modulo by zero - python

I had written a simple pascal triangle code in python but I am getting a error
def factorial(n):
c=1
re=1
for c in range(n):
re = re * c;
return(re)
print "Enter how many rows of pascal triangle u want to show \n"
n=input();
i=1
c=1
for i in range(n):
for c in range(n-i-1):
print ""
for c in range(i):
a = factorial(i);
b = factorial(c);
d = factorial(i-c);
z = (a/(b*d));
print "%d" % z
print "\n"
ERROR:
Traceback (most recent call last):
File "/home/tanmaya/workspace/abc/a.py", line 19, in <module>
z = (a/(b*d));
ZeroDivisionError: integer division or modulo by zero

Your factorial() function returns 0 for any input because of how you defined your range.
The range builtin starts at 0 unless otherwise defined so:
for c in range(n):
re = re * c # no semicolons in Python
is doing:
re = re * 0
on the first iteration so for all subsequent iterations:
re = 0 * c
will always be 0
Start your range at 1 like so
for c in range(1, n):
re *= c # The *= operator is short hand for a = a * b
you can see this more explicityly:
>>> print(list(range(5)))
[0, 1, 2, 3, 4]
>>> print(list(range(1,5)))
[1, 2, 3, 4]
>>>
or instead of rolling your own function use the one that comes with Python:
>>> from math import factorial
>>> factorial(3)
6
Upon closer reading of your code it seems you tried to circumvent this by setting c = 1 outside your for loop. This is not going to work because the variables you declared outside the loop are being reassigned inside it.

ZeroDivisionError means that you were trying to divide or modulo, a number n with 0.
in your case, z = (a/(b*d)) resulted in z = (a/0)
Also, as #theB pointed out, your factorial function is incorrect.
Try fixing those.
Also, you don't really need ; in your code. It's usually the case we put ; when we want to make the code one liner.

Related

Python function-unexpected output

I have this Python function:
def main(n,x):
g=0
for i in range(1,n):
g+=((-1)^i)*(x^(2*i+1))/(2*i+1)
return g
print main(3,2)
and the output is -6, when I think it should be 86/15. Where is my mistake? I want to find the n-value of x-(x^3)/3+(x^5)/5+...
A few issues with your current solution:
Your exponentiation operator should be ** not ^ which is XOR.
You should start range from 0 not 1 (then first multiplier is -1**0 = 1)
Change one of the numbers in the division to float, to avoid integer division in Python 2.
def main(n, x):
g = 0
for i in range(n):
g += ((-1)**i) * (x**(2*i+1))/float(2*i+1)
return g
If you want your answer in fraction you can use:
from fractions import Fraction
def main(n,x):
g=0
for i in range(n):
g+=Fraction(((-1)**i)*(x**(2*i+1)),(2*i+1))
return g
print main(3,2)
It gives output:
86/15

Python Queuing integer division error

import math
import random
m = 1.5 #mu
l = 2 #lambda
c = 3 #control/number of servers
def single(m, l, c):
p = (l/m)
Po = (1-(l/m))
Ls = (l/(m-l))
Ws = (1/(m-l))
Wq = (l/(m*(m-l)))
Lq = (l**2/(m*(m-l)))
return(p, Po, Ls, Ws, Wq, Lq)
def multi(m, lm, mu):
rho=lm/mu
n=0
sm=0
while(n<=m-1):
sm=(1/math.factorial(n))*pow(rho,n)
n+=1
sm = sm + 1/(1/math.factorial(m))*(pow(rho,m)*m*mu/(m*mu-lm))
lS=lm*mu*pow(rho,m)/((math.factorial(m-1)*(m*mu-lm)**2))*(1/sm)+rho
lQ=lS-rho
#Po = 1/sm
return(lq, ls)
singReturn=single(m, l, c)
multiReturn=multi(3, 2, 1.5)
print("SINGLE SERVER QUEUEING")
print("-----------------------")
print("p: %4.4f \nPo: %4.4f \nLs: %4.4f \nWs: %4.4f \nWq: %4.4f \nLq: %4.4f"%singReturn)
I am being returned and error with:
Traceback (most recent call last):
File "/home/schnipdip/Desktop/final_part1_chris_herzog.py", line 35, in <module>
multiReturn=multi(3, 2, 1.5)
File "/home/_____/Desktop/final_part1_.py", line 28, in multi
sm = sm + 1/(1/math.factorial(m))*(pow(rho,m)*m*mu/(m*mu-lm))
ZeroDivisionError: integer division or modulo by zero
I am trying to find the value of SM and then convert it into the Po variable. The while loop is controlling how many servers there are by m(or c) - 1.
I changed the variables in the loop to see if the variable was being overwritten in memory by a previous value and not resetting for whatever reason. I'm sure that has nothing to do with it.
If you're using Python 2, it's probably due to this part:
... 1/(1/math.factorial(m)) ...
Logically, it doesn't make much sense: mathematically, 1/(1/x) is just a clumsy way to spell plain x. So I bet your code has a logical error there.
But, in Python 2, it also has a programming error: / applied to integers does truncating integer division in Python 2:
>>> import math
>>> m = 3
>>> math.factorial(m)
6
>>> 1 / math.factorial(m)
0
To prevent that, use, e.g., 1.0 instead of 1 to force float division:
>>> 1.0 / math.factorial(m)
0.16666666666666666

Imprecise results of logarithm and power functions in Python

I am trying to complete the following exercise:
https://www.codewars.com/kata/whats-a-perfect-power-anyway/train/python
I tried multiple variations, but my code breaks down when big numbers are involved (I tried multiple variations with solutions involving log and power functions):
Exercise:
Your task is to check wheter a given integer is a perfect power. If it is a perfect power, return a pair m and k with m^k = n as a proof. Otherwise return Nothing, Nil, null, None or your language's equivalent.
Note: For a perfect power, there might be several pairs. For example 81 = 3^4 = 9^2, so (3,4) and (9,2) are valid solutions. However, the tests take care of this, so if a number is a perfect power, return any pair that proves it.
The exercise uses Python 3.4.3
My code:
import math
def isPP(n):
for i in range(2 +n%2,n,2):
a = math.log(n,i)
if int(a) == round(a, 1):
if pow(i, int(a)) == n:
return [i, int(a)]
return None
Question:
How is it possible that I keep getting incorrect answers for bigger numbers? I read that in Python 3, all ints are treated as "long" from Python 2, i.e. they can be very large and still represented accurately. Thus, since i and int(a) are both ints, shouldn't the pow(i, int(a)) == n be assessed correctly? I'm actually baffled.
(edit note: also added integer nth root bellow)
you are in the right track with logarithm but you are doing the math wrong, also you are skipping number you should not and only testing all the even number or all the odd number without considering that a number can be even with a odd power or vice-versa
check this
>>> math.log(170**3,3)
14.02441559235585
>>>
not even close, the correct method is described here Nth root
which is:
let x be the number to calculate the Nth root, n said root and r the result, then we get
rn = x
take the log in any base from both sides, and solve for r
logb( rn ) = logb( x )
n * logb( r ) = logb( x )
logb( r ) = logb( x ) / n
blogb( r ) = blogb( x ) / n
r = blogb( x ) / n
so for instance with log in base 10 we get
>>> pow(10, math.log10(170**3)/3 )
169.9999999999999
>>>
that is much more closer, and with just rounding it we get the answer
>>> round(169.9999999999999)
170
>>>
therefore the function should be something like this
import math
def isPP(x):
for n in range(2, 1+round(math.log2(x)) ):
root = pow( 10, math.log10(x)/n )
result = round(root)
if result**n == x:
return result,n
the upper limit in range is to avoid testing numbers that will certainly fail
test
>>> isPP(170**3)
(170, 3)
>>> isPP(6434856)
(186, 3)
>>> isPP(9**2)
(9, 2)
>>> isPP(23**8)
(279841, 2)
>>> isPP(279841)
(529, 2)
>>> isPP(529)
(23, 2)
>>>
EDIT
or as Tin Peters point out you can use pow(x,1./n) as the nth root of a number is also expressed as x1/n
for example
>>> pow(170**3, 1./3)
169.99999999999994
>>> round(_)
170
>>>
but keep in mind that that will fail for extremely large numbers like for example
>>> pow(8191**107,1./107)
Traceback (most recent call last):
File "<pyshell#90>", line 1, in <module>
pow(8191**107,1./107)
OverflowError: int too large to convert to float
>>>
while the logarithmic approach will success
>>> pow(10, math.log10(8191**107)/107)
8190.999999999999
>>>
the reason is that 8191107 is simple too big, it have 419 digits which is greater that the maximum float representable, but reducing it with a log produce a more reasonable number
EDIT 2
now if you want to work with numbers ridiculously big, or just plain don't want to use floating point arithmetic altogether and use only integer arithmetic, then the best course of action is to use the method of Newton, that the helpful link provided by Tin Peters for the particular case for cube root, show us the way to do it in general alongside the wikipedia article
def inthroot(A,n):
if A<0:
if n%2 == 0:
raise ValueError
return - inthroot(-A,n)
if A==0:
return 0
n1 = n-1
if A.bit_length() < 1024: # float(n) safe from overflow
xk = int( round( pow(A,1/n) ) )
xk = ( n1*xk + A//pow(xk,n1) )//n # Ensure xk >= floor(nthroot(A)).
else:
xk = 1 << -(-A.bit_length()//n) # power of 2 closer but greater than the nth root of A
while True:
sig = A // pow(xk,n1)
if xk <= sig:
return xk
xk = ( n1*xk + sig )//n
check the explanation by Mark Dickinson to understand the working of the algorithm for the case of cube root, which is basically the same for this
now lets compare this with the other one
>>> def nthroot(x,n):
return pow(10, math.log10(x)/n )
>>> n = 2**(2**12) + 1 # a ridiculously big number
>>> r = nthroot(n**2,2)
Traceback (most recent call last):
File "<pyshell#48>", line 1, in <module>
nthroot(n**2,2)
File "<pyshell#47>", line 2, in nthroot
return pow(10, math.log10(x)/n )
OverflowError: (34, 'Result too large')
>>> r = inthroot(n**2,2)
>>> r == n
True
>>>
then the function is now
import math
def isPPv2(x):
for n in range(2,1+round(math.log2(x))):
root = inthroot(x,n)
if root**n == x:
return root,n
test
>>> n = 2**(2**12) + 1 # a ridiculously big number
>>> r,p = isPPv2(n**23)
>>> p
23
>>> r == n
True
>>> isPPv2(170**3)
(170, 3)
>>> isPPv2(8191**107)
(8191, 107)
>>> isPPv2(6434856)
(186, 3)
>>>
now lets check isPP vs isPPv2
>>> x = (1 << 53) + 1
>>> x
9007199254740993
>>> isPP(x**2)
>>> isPPv2(x**2)
(9007199254740993, 2)
>>>
clearly, avoiding floating point is the best choice

Python GCD - errors

I have an issue with my code whose purpose is to find the GCD of two inputs. When I try to run the module it tells me that 'gcd' is not defined.
def GCD(12,4):
gcd = 1
for i in range(2, max(12,4)/2):
if((12 % i == 0) and (4 % i == 0)):
gcd = i
return gcd
You are not calling the GCF function. You have just defined your function. You need to add a line
gcf = GCF(a,b)
after the place where you accept the input. That is after b = int(input('denomenator: '))
Edit:
Change the input statements to
a = float(input('numerator: '))
b = float(input('denomenator: '))
You can use Euclid division algorithm to find gcd in less time.
Take floating point numbers to a and b.
def gcd(a,b):
c = 1
a,b = max(a,b),min(a,b)
while c != 0:
c = a%b
a,b = b,c
return a
print gcd(12,5)

Math domain error in fermat

from math import sqrt
def fermatBook (n):
x=int(sqrt(n))
c=x**2-n
while (sqrt(c)!=int(sqrt(c))):
x=x+1
y=sqrt(c)
a=x+y
b=x-y
if a==1 or b==1:
print "The number is prime"
return a, b
error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
fermatBook (23867)
File "C:/Python27/fermatLivro.py", line 6, in fermatBook
while (sqrt(c)!=int(sqrt(c))):
ValueError: math domain error
I don't know what is going wrong with the program... Could someone help me ?
most likely your variable c is going negative:
Example
if you call:
n = 2
fermatBook(n)
it will assign the following values to the following variables:
x = int(sqrt(n)) = int(1.47...) = 1
c = x**2 - n = 1**2 - 2 = 1 - 2 = -1
This will likely happen alot on values of n whose square root is not an integer.
sqrt(n) >= int(sqrt(n)), n >= 0
Then when you call sqrt(c) it is out of the domain because it cannot handle negative values.
>>> from math import sqrt
>>> sqrt(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
You should rather use something that can handle imaginary numbers, i.e. cmath
Or perform checks to assure this does not happen:
As an example...
if c < 0:
c = 0
As soon as you fix this however you are going to run into another problem:
This is an infinite loop:
while (sqrt(c)!=int(sqrt(c))):
x=x+1
you need to update c otherwise the condidtion will never change no matter how many times you increment x. You probably meant this?
while (sqrt(c)!=int(sqrt(c))):
x=x+1
c = x**2+n # <--- UPDATE c

Categories

Resources