Debugging Python Program LCM - python

So I have this program:
def gcd(x, y):
while y % x != 0:
y = y % x
x = x % y
return y
def lcm(x, y):
(x * y) / gcd(x, y)
a = lcm(1, 2)
b = lcm(3, a)
c = lcm(b, 4)
d = lcm(5, c)
e = lcm(6, d)
f = lcm(e, 7)
g = lcm(f, 8)
h = lcm(9, g)
i = lcm(h, 10)
j = lcm(11, i)
k = lcm(12, j)
l = lcm(k, 13)
m = lcm(l, 14)
n = lcm(15, m)
o = lcm(n, 16)
p = lcm(17, o)
q = lcm(18, p)
r = lcm(q, 19)
print(lcm(r, 20))
Ok, so I've worked that one out now thanks to answers on this post, but why does this:
def gcd(x, y):
while y % x != 0:
y = y % x
x = x % y
return y
def lcm(x, y):
return (x * y) / gcd(x, y)
a = lcm(1, 2)
b = lcm(3, a)
c = lcm(b, 4)
d = lcm(5, c)
e = lcm(6, d)
f = lcm(e, 7)
g = lcm(f, 8)
h = lcm(9, g)
i = lcm(h, 10)
j = lcm(11, i)
k = lcm(12, j)
l = lcm(k, 13)
m = lcm(l, 14)
n = lcm(15, m)
o = lcm(n, 16)
p = lcm(17, o)
q = lcm(18, p)
r = lcm(q, 19)
print(lcm(r, 20))
return this:
ZeroDivisonError: float modulo
Complaining about the second line, I gather I might need some sort of if statement to put the two numbers in order.
Anyone got any information about why this is happening?
Thanks.

You forgot to return something in the lcm function.
def lcm(x, y):
return (x * y) / gcd(x, y)
When you don't explicitly return anything in a function, Python automatically returns None. When you try lcm(r, 20), that's the same as lcm(None, 20), which doesn't work since you multiply both parameters in the lcm function - you can't do None * 20.
However this will yield another error - there's a problem with your gcd function as well!
The error comes when y % x is 0, and then you do x % y straight after. This would be a working solution:
def gcd(x, y):
while y != 0:
x, y = y, x % y
return x
This is taken straight from Euclid's algorithm from the Wikipedia article (Section 2.2).

There is a misunderstanding here.when you use gcd() inside lcm(), you are creating nested environments. gcd()'s environment doesn't have access to the global environment.anything that it returns, will be accessible by lcm()'s environment.you don't return anything to the global environment in lcm(), so python by default returns None.to fix this try:
def lcm(x, y):
return (x * y) / gcd(x, y)
also you have to add this block to the lcm() to avoid throwing ZeroDivision exception:
def lcm(x, y):
try:
return (x * y) / gcd(x, y)
except ZeroDivisionError:
do whatever you want and return a value

Related

taking derivative using sparse matrix

I’m trying to solve the following problem for around 2 days, but have not had success.
I want to calculate the derivative using the sparse matrix, but the result doesn’t true. I think there is a mistake in the solution function, but I cannot find it.
class Cahn_Hillard(object):
def __init__(self, n, X, T, dt, skip):
self.n = n
self.X = X
self.dx = 1 / (n - 1)
self.T = T
self.dt = dt
self.N = int(self.T / self.dt)
def __call__(self):
central = self.forward_diff_sparse()
itr = int(self.T / self.dt)
for i in range(0, itr + 1):
if i == 0:
c = np.random.uniform(0, 1, (self.n, 1))
else:
c_next = self.solution(c, central)
c = c_next
print(i)
return c
def forward_diff_sparse(self):
sparse_mat = np.eye(self.n) - np.eye(self.n, k=-1)
return sparse_mat
def solution(self, c, central):
# calculate derivative of concentration
deriv = central.dot(c) / self.dx
# calculate difusion coeffcient
D_eff = (1 - 2 * self.X * c * (1 - c)) * deriv
Diff = central.dot(D_eff) / self.dx
# Calculate the next step concentration
next_c = c + self.dt * Diff
return next_c
It would be great if you help me.

Python script to calculate hypotenuse and angle measure based on user inputs and classes

So far, finding the hypotenuse, "side c" works fine without problems, the problem is when I try to calculate the angle, I am pretty sure it's returning a wrong value for the angle.
import math
from math import sqrt
class Triangle:
def __init__(self, side_a, side_b, ):
self.side_a = side_a
self.side_b = side_b
def SidesCalculate(self, ):
side_c = sqrt(self.side_a ** 2 + self.side_b ** 2)
return side_c
def AnglesCalculate(self, side_c):
x = math.sin(self.side_a / side_c)
math.asin(x)
x = round(x * 180 / math.pi)
return x
g = Triangle(side_a=int(input("Enter side a: ")), side_b=int(input("Enter side b: ")))
print("side c =", + g.SidesCalculate())
print("angle is =", + g.AnglesCalculate(side_c=True), '°')
Here is a way to do what you want.
First when you compute side_c, save it as an attribute.
def SidesCalculate(self, ):
self.side_c = sqrt(self.side_a ** 2 + self.side_b ** 2)
return self.side_c
Secondly, give the parameter side_c a default value None. (You also have another math error in the following function pointed by CFLS)
def AnglesCalculate(self, side_c = None):
if side_c == None:
side_c = self.side_c
x = math.asin(self.side_a / side_c)
x = round(x * 180 / math.pi)
return x
Now, if you want the class to pick previously computed side_c, you can do this:
g = Triangle(side_a=int(input("Enter side a: ")), side_b=int(input("Enter side b: ")))
print("side c =", + g.SidesCalculate())
print("angle is =", + g.AnglesCalculate(), '°')
def AnglesCalculate(self, side_c):
x = math.asin(self.side_a / side_c)
x = round(x * 180 / math.pi)
return x

python ECDSA implementation miscalculation on big integers

I'm trying to implement ECDSA using python as part of my homework, I have a function named multiplication which takes two arguments Point P and t calculates B= tP.I have implemented this algorith based on iterative double and add algorithm on this wikipedia page the problem is when p coordination is small(one or two digits), the algorithm works fine but when coordination is large (about 70 digits) the result is different than what it supposed to be. here is the part of my code which calculates multiplication:
def addition(self, p, q):
if p.is_infinite:
return q
elif q.is_infinite:
return p
else :
if (q.x - p.x) == 0:
point = Point.Point(0, 0)
point.is_infinite = True
return point
s = int(((q.y - p.y) * Utils.Utils.mode_inverse(q.x - p.x, self.prime)) % self.prime)
xr = int((math.pow(s, 2) - p.x - q.x) % self.prime)
yr = int(((s * (p.x - xr)) - p.y) % self.prime)
r = Point.Point(xr, yr)
return r
def double(self, p):
if p.is_infinite:
return p
if p.y == 0:
point = Point.Point(0, 0)
point.is_infinite = True
return point
s = int((((3 * math.pow(p.x, 2)) + self.a) * Utils.Utils.mode_inverse(2 * p.y, self.prime)) % self.prime)
xr = int((math.pow(s, 2) - p.x - p.x) % self.prime)
yr = int(((s * (p.x - xr)) - p.y) % self.prime)
r = Point.Point(xr, yr)
return r
def multiplication(self, p, t):
bin_t = bin(t)[2:]
Q = Point.Point(p.x, p.y)
Q.is_infinite = True
for i, digit in enumerate(bin_t):
Q = self.double(Q)
if digit == '1':
Q = self.addition(Q, p)
return Q
Here is my Util class:
class Utils(object):
#staticmethod
def mode_inverse(a, m):
return pow(a, m - 2, m)
Here is my Point class:
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.is_infinite = False
I use Curve P-224 parameters which are:
p = 26959946667150639794667015087019630673557916260026308143510066298881
a = -3
b = 18958286285566608000408668544493926415504680968679321075787234672564
Gx = 19277929113566293071110308034699488026831934219452440156649784352033
Gy = 19926808758034470970197974370888749184205991990603949537637343198772
according to calculator http://www.christelbach.com/eccalculator.aspx I should get this result for calculating 2G:
Px = 11838696407187388799350957250141035264678915751356546206913969278886
Py = 2966624012289393637077209076615926844583158638456025172915528198331
but what I actually get is:
Px = 15364035107168693070617763393106849380516103015030577748254379737088
Py = 7033137909116168824469040716130881489351924269422358605872723100109
Is there any way to fix this?
This was just a guess:
math.pow returns a floating point number (which has finite precision). I'd suggest using e.g. s.x * s.x instead of math.pow(s.x,2) in case the issue is hitting precision issues with bigger numbers.

Getting a particular value from a function

I am trying to extract a particular value from a function which deals with multiple values.
My simplified example:
def func(x, y):
a = ran(0,10) #creates random integer
b = ran(0,10)
return a*x + b*y
print a*x + b*y
So if I want to use the values of a and b that get created in func() in some other function, how would I extract them?
It's not so much that you need to extract them; you just need to save them somewhere.
If this is a top-level function in a module, consider a module variable:
import random
rand1 = random.randint(0, 10)
rand2 = random.randint(0, 10)
def f(x, y):
result = rand1*x + rand2*y
print result
return result
but these look suspiciously related so perhaps they belong in a class?
class YourClass(object):
def __init__(self):
self.rand1 = random.randint(0, 10)
self.rand2 = random.randint(0, 10)
def your_func_as_a_method(self, x, y):
result = self.rand1*x + self.rand2*y
print result
return result
Of course, you could also just make them part of the function's return definition:
def f(x, y):
rand1 = random.randint(0, 10)
rand2 = random.randint(0, 10)
result = rand1*x + rand2*y
print result
return (result, rand1, rand2)
fxy, a, b = f(x, y)
One way is to return them:
return (a*x + b*y, a, b)
and you can call your function as:
(result, a, b) = func(x,y)

How would you write test code for summer(n, f) and is there something wrong with this?

def noah(x):
return x*x*x
def summer(n, f):
"""precondition: n is a nonnegative number and f is a function
whose domain includes the nonnegative integres
postcondition: returns sum( f(k), k = 1..n) """
y = 0
for i in range (1, n + 1):
y = y + f(i)
return y
well ... seems easier with a generator:
sum(f(x) for x in range(1,n+1)) (Python 3)
sum(f(x) for x in xrange(1,n+1)) (Python 2)
recursion is your friend.
def summer(n, f):
if n == 0:
return 0
return f(n) + summer(n-1, f)
Another way is to map the range list with f:
sum(map(lambda x: f(x), range(1, n+1)))
# You should verify preconditions in the summer function's body
def summer(n, f):
if n < 0:
raise ValueError(str(n) + ' < 0') # check that n > 0
if not isinstance(f, types.FunctionType):
raise ValueError('f is not a function') # check f is a function
y = 0
for i in range (1, n + 1):
y = y + f(i)
return y
### Test part ###
import unittest
class TestSummer(unittest.TestCase):
def test_precond1(self):
"""Check that if n < 0, raise ValueError"""
self.assertRaises(ValueError, summer, -1, noah)
def test_precond2(self):
"""Check that if f is not a function, raise ValueError"""
self.assertRaises(ValueError, summer, 1, 'str')
def test_result(self):
"""Check that summer(3, noah) is equal to 32"""
self.assertTrue(summer(3, noah), 32) # f(1) + f(2) + f(3) = 1 + 8 + 27 = 32
unittest.main()

Categories

Resources