Related
I made a program to multiply two strings and I expected 1000*10 = 10000, but I am getting 100000. I don't know where my logic is wrong. I also tried replacing m2 with m1 in the expression ((val3+val4) * 10**(m2)) but nothing works, and when I try to multiply 120 * 10, I get 300.
def Multiply_Recursive(a, b):
if len(a) == 1 or len(b) == 1:
return str(int(a)*int(b))
else:
m = max(len(a),len(b))
m2 = m // 2
m1 = len(b) // 2
A = int(a[0:m2])
B = int(a[m2:len(a)])
C = int(b[0:m1])
D = int(b[m1:len(b)])
val1 = int(Multiply_Recursive(str(A),str(C)))
val2 = int(Multiply_Recursive(str(B),str(D)))
val3 = int(Multiply_Recursive(str(A),str(D)))
val4 = int(Multiply_Recursive(str(B),str(C)))
return str(val1 * 10**(2*m2) + ((val3+val4) * 10**(m2)) + val2)
num = Multiply_Recursive("1000","10")
print(num)
In the final formula you assume that m2 digits occur at the right of the split point, and that this number of digits is the same for both splits. Neither is generally true.
Also, as the definition of m depends on the larger input, it could lead to an out-of-range number of digits represented by m2, when a is much smaller than b.
You could fix this like this:
Define m2 like this:
m2 = len(a) // 2
Split the input in such a way that m1 and m2 are the number of digits after the split point, not before. So split like this:
A = int(a[:-m2])
B = int(a[-m2:])
C = int(b[:-m1])
D = int(b[-m1:])
Change the final formula taking into account that m1 and m2 can be different. So for instance there should not be 2*m2, but m1+m2, ...Etc:
return str(val1 * 10**(m1+m2) + val3 * 10**m2 + val4 * 10**m1 + val2)
The real Karatsuba algorithm will make sure to choose m1 and m2 so they are the same, and it will not convert strings to integer unless it is certain the size of the strings is limited. It also needs one less recursive call.
The naming is not helpful - just using ah, al, bh, bl:
def multiply_recursive(a, b):
""" Return the product of a and b.
All numbers are passed as their decimal string representations.
"""
if not a or not b:
return "0"
if len(a) == 1 or len(b) == 1:
return str(int(a)*int(b))
#
m = max(len(a), len(b))
m2 = m // 2
# different split points not suitable for common scaling of "mixed products" below
# m1 = len(b) // 2
# low parts are remainders: split from least significant end!
ah, al = a[0:-m2], a[-m2:].lstrip("0")
bh, bl = b[0:-m2], b[-m2:].lstrip("0")
# print(ah, al, bh, bl, sep=',')
ahbh = int(multiply_recursive(ah, bh))
albl = int(multiply_recursive(al, bl))
ahbl = int(multiply_recursive(ah, bl))
albh = int(multiply_recursive(al, bh))
product = str(ahbh * 100**m2 + (ahbl+albh) * 10**m2 + albl)
# print(product)
return product
num = multiply_recursive("1000","10")
print(num)
I am trying to implement the Karatsuba Algorithm as mentioned in the course here in python 2.7. Here is the code that I have got currently:
# Karatsuba multiplication implementation in python
import numpy as np
import sys
# x = 10^(n/2)*a + b and y = 10^(n/2)*c + d
# x.y = 10^n*(ac) + 10^(n/2)*(ad + bc) + bd
# now recursively compute ac, ad, bc and bd
sys.setrecursionlimit(15000)
def algo_recurs(val1, val2):
# Assuming that the length of both the multiplier and multiplicand is
same
# Currently employing numbers which are of length 2^n
n = len(str(val1)) # n = 4
print(n)
divVal = 10**(n/2)
a = val1 / divVal # a = 12
b = val1 % divVal # b = 34
c = val2 / divVal # c = 43
d = val2 % divVal # d = 21
# let the example case be 1234 * 4321
if(len(str(val1)) == 2):
prob1 = a * c
prob2 = b * d
prob3 = (a+b)*(c+d) - prob1 - prob2
finalResult = prob1*(divVal*divVal)+prob3*divVal+prob2
return(finalResult)
else:
prob1 = algo_recurs(a,c)
prob2 = algo_recurs(b,d)
prob3 = algo_recurs((a+b),(c+d)) - prob1 -prob2
finalResult = prob1*(divVal*divVal)+prob3*divVal+prob2
#print(finalResult)
return(finalResult)
#Enter the inputs
multiplicand = input("Enter the multiplicand:")
multiplier = input("Enter the multiplier:")
output = algo_recurs(multiplicand, multiplier)
print(output)
The above code works well with the numbers of length 4 or less. But the moment I go beyond that, it throws the following error:
File "Karatsuba.py", line 31, in algo_recurs
prob1 = algo_recurs(a,c)
File "Karatsuba.py", line 31, in algo_recurs
prob1 = algo_recurs(a,c)
File "Karatsuba.py", line 31, in algo_recurs
prob1 = algo_recurs(a,c)
File "Karatsuba.py", line 15, in algo_recurs
n = len(str(val1)) # n = 4
RuntimeError: maximum recursion depth exceeded while getting the str of an object
I increased the recursion limit too, thinking that it might have been the issue. But that didn't solve it either.
I would appreciate if you can point out what I might be doing wrong in the implementation.
Your algorithm will never terminate, no matter how high you set the recursion limit. This is because the parameters a and c will always stay the same once val1 gets to single digits, because then n is 1 and 10**(n/2) is also 1.
It's dangerous to change the recursion limit because typically when you have exceeded the recursion limit, it's because your program contains an error or a poor design decision. Recursion can always be replaced with iteration at equal or lower memory cost.
Unless your algorithm insists you to do so knowing that at some point you will receive a result, you can change the maximum recursion depth every time you call your function, but again I wouldn't recommend since your program exceeded 1500 recursive calls when you set it to that and that is quite excessive.
# Karatsuba multiplication implementation in python
import numpy as np
import sys
def algo_recurs(val1, val2):
sys.setrecursionlimit(sys.getrecursionlimit() + 1) # Changes the recursion limit every time
n = len(str(val1))
#print(n)
divVal = 10**(n/2)
a = val1 / divVal # a = 12
b = val1 % divVal # b = 34
c = val2 / divVal # c = 43
d = val2 % divVal # d = 21
if(len(str(val1)) == 2):
prob1 = a * c
prob2 = b * d
prob3 = (a+b)*(c+d) - prob1 - prob2
finalResult = prob1*(divVal*divVal)+prob3*divVal+prob2
return(finalResult)
else:
prob1 = algo_recurs(a,c)
prob2 = algo_recurs(b,d)
prob3 = algo_recurs((a+b),(c+d)) - prob1 -prob2
finalResult = prob1*(divVal*divVal)+prob3*divVal+prob2
return(finalResult)
multiplicand = int(input("Enter the multiplicand:"))
multiplier = int(input("Enter the multiplier:"))
output = algo_recurs(multiplicand, multiplier)
print(output)
Define a function that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.
For example, given 6,7,8, the function that I defined should return 113
When I gave my code, it solves most of the problems but apparently there is some possibility that I haven't tried?? I think my code is flawed but not sure what other possibilities are there. Would really appreciate some help thank you so much!
def bigger_sum(a,b,c):
if(a+b>b+c):
return(a*a+b*b)
if(a+c>b+c):
return(a*a+c*c)
if(b+c>a+c):
return(b*b+c*c)
You can use min for this problem:
def big2_sqrsum(a,b,c):
x = min(a,b,c)
return (a*a + b*b + c*c) - (x*x)
print(big2_sqrsum(6,7,8))
Output:
113
Alternate solution with if-else
def big2_sqrsum2(a,b,c):
if a < b and a <c:
return b*b + c*c
elif b < a and b < c:
return a*a + c*c
elif c < a and c < b:
return a*a + b*b
Just check for the smallest number. That known, assign the values to two new variables that will hold the largest and second largest value and sum their squares.
Something like this :
big1 = 0
big2 = 0
if ([a is smallest]):
big1 = b
big2 = c
elif ([b is smallest]):
big1 = a
big2 = c
elif ([c is smallest]):
big1 = a
big2 = b
allows you to have only one place to calculate your formula :
return big1 * big1 + big2 * big2
Let's take a look at why your code is flawed. Given a comparison like if a + b > b + c:, the implication that both a and b are both greater than c is false. b can be the smallest number. All you know is that a > c, since you can subtract b from both sides of the inequality.
You need to find and discard the smallest number. The simplest way is to compute the minimum with min and subtract it off, as #Sociopath's answer suggests.
If you want to keep your if-elsestructure, you have to compare numbers individually:
if a > b:
n1= a
n2 = b if b > c else c
elif a > c:
n1, n2 = a, b
else:
n1, n2 = b, c
You can Simply Define Function With Using min()
def two_bigger_sum(num1,num2,num3):
min_num = min(num1,num2,num3) # it returns minimum number
return ((num1**2 + num2**2 + num3**2)-(min_num**2)) # num**2 = square of num
print(two_bigger_sum(6,7,8))
Output = 113
Sociopath's answer works, but is inefficient since it requires two extra floating point multiplies. If you're doing this for a large number of items, it will take twice as long! Instead, you can find the two largest numbers directly. Basically, we're sorting the list and taking the two largest, this can be directly as follows:
def sumsquare(a,b,c):
# Strategy: swap, and make sure c is the smallest by the end
if c > b:
b, c = c, b
if c > a:
a, c = c, a
return a**2 + b**2
# Test:
print(sumsquare(3,1,2))
print(sumsquare(3,2,1))
print(sumsquare(1,2,3))
print(sumsquare(1,3,2))
print(sumsquare(2,1,3))
print(sumsquare(2,3,2))
I have tried to use list comprehension & list slicing with sorting method.
def b2(l):
return sum([x**2 for x in sorted(l)[1:]])
print(b2([1,2,3]))
OP:-
13
This is my code:
def sum_even(a, b):
count = 0
for i in range(a, b, 1):
if(i % 2 == 0):
count += [i]
return count
An example I put was print(sum_even(3,7)) and the output is 0. I cannot figure out what is wrong.
Your indentation is off, it should be:
def sum_even(a, b):
count = 0
for i in range(a, b, 1):
if(i % 2 == 0):
count += i
return count
so that return count doesn't get scoped to your for loop (in which case it would return on the 1st iteration, causing it to return 0)
(And change [i] to i)
NOTE: another problem - you should be careful about using range:
>>> range(3,7)
[3, 4, 5, 6]
so if you were to do calls to:
sum_even(3,7)
sum_even(3,8)
right now, they would both output 10, which is incorrect for sum of even integers between 3 and 8, inclusive.
What you really want is probably this instead:
def sum_even(a, b):
return sum(i for i in range(a, b + 1) if i % 2 == 0)
Move the return statement out of the scope of the for loop (otherwise you will return on the first loop iteration).
Change count += [i] to count += i.
Also (not sure if you knew this), range(a, b, 1) will contain all the numbers from a to b - 1 (not b). Moreover, you don't need the 1 argument: range(a,b) will have the same effect. So to contain all the numbers from a to b you should use range(a, b+1).
Probably the quickest way to add all the even numbers from a to b is
sum(i for i in xrange(a, b + 1) if not i % 2)
You can make it far simpler than that, by properly using the step argument to the range function.
def sum_even(a, b):
return sum(range(a + a%2, b + 1, 2))
You don't need the loop; you can use simple algebra:
def sum_even(a, b):
if (a % 2 == 1):
a += 1
if (b % 2 == 1):
b -= 1
return a * (0.5 - 0.25 * a) + b * (0.25 * b + 0.5)
Edit:
As NPE pointed out, my original solution above uses floating-point maths. I wasn't too concerned, since the overhead of floating-point maths is negligible compared with the removal of the looping (e.g. if calling sum_even(10, 10000)). Furthermore, the calculations use (negative) powers of two, so shouldn't be subject by rounding errors.
Anyhow, with the simple trick of multiplying everything by 4 and then dividing again at the end we can use integers throughout, which is preferable.
def sum_even(a, b):
if (a % 2 == 1):
a += 1
if (b % 2 == 1):
b -= 1
return (a * (2 - a) + b * (2 + b)) // 4
I'd like you see how your loops work if b is close to 2^32 ;-)
As Matthew said there is no loop needed but he does not explain why.
The problem is just simple arithmetic sequence wiki. Sum of all items in such sequence is:
(a+b)
Sn = ------- * n
2
where 'a' is a first item, 'b' is last and 'n' is number if items.
If we make 'a' and b' even numbers we can easily solve given problem.
So making 'a' and 'b' even is just:
if ((a & 1)==1):
a = a + 1
if ((b & 1)==1):
b = b - 1
Now think how many items do we have between two even numbers - it is:
b-a
n = --- + 1
2
Put it into equation and you get:
a+b b-a
Sn = ----- * ( ------ + 1)
2 2
so your code looks like:
def sum_even(a,b):
if ((a & 1)==1):
a = a + 1
if ((b & 1)==1):
b = b - 1
return ((a+b)/2) * (1+((b-a)/2))
Of course you may add some code to prevent a be equal or bigger than b etc.
Indentation matters in Python. The code you write returns after the first item processed.
This might be a simple way of doing it using the range function.
the third number in range is a step number, i.e, 0, 2, 4, 6...100
sum = 0
for even_number in range(0,102,2):
sum += even_number
print (sum)
def sum_even(a,b):
count = 0
for i in range(a, b):
if(i % 2 == 0):
count += i
return count
Two mistakes here :
add i instead of [i]
you return the value directly at the first iteration. Move the return count out of the for loop
The sum of all the even numbers between the start and end number (inclusive).
def addEvenNumbers(start,end):
total = 0
if end%2==0:
for x in range(start,end):
if x%2==0:
total+=x
return total+end
else:
for x in range(start,end):
if x%2==0:
total+=x
return total
print addEvenNumbers(4,12)
little bit more fancy with advanced python feature.
def sum(a,b):
return a + b
def evensum(a,b):
a = reduce(sum,[x for x in range(a,b) if x %2 ==0])
return a
SUM of even numbers including min and max numbers:
def sum_evens(minimum, maximum):
sum=0
for i in range(minimum, maximum+1):
if i%2==0:
sum = sum +i
i= i+1
return sum
print(sum_evens(2, 6))
OUTPUT is : 12
sum_evens(2, 6) -> 12 (2 + 4 + 6 = 12)
List based approach,
Use b+1 if you want to include last value.
def sum_even(a, b):
even = [x for x in range (a, b) if x%2 ==0 ]
return sum(even)
print(sum_even(3,6))
4
[Program finished]
This will add up all your even values between 1 and 10 and output the answer which is stored in the variable x
x = 0
for i in range (1,10):
if i %2 == 0:
x = x+1
print(x)
I am attempting to script a short code to figure out the number of days it takes to reach a given principal in the bank due to daily interest. Using my code below does not yield any errors when run in IDLE, but the counter returns 0. Any ideas what I missed?
def main():
# irrelevant code elided by msw, Bal, Int and Tar are numeric
counter = 0
for i in range(0):
if (Bal * Int) == Tar:
print '1'
else:
counter + 1
print counter
I'm not sure what you're getting at with this loop:
for i in range(0):
if (Bal * Int) == Tar:
print '1'
else:
counter + 1
range(0) is an empty list, so the loop won't execute at all.
counter + 1 simply calculates one more than counter, it won't increment counter, you probably mean counter += 1
There's nothing in the loop that changes at each iteration, so if you ever get into it, it will be an infinite loop.
I believe the formula to calculate final balance with interest is:
Final = Principal * ( 1 + interest ) ** interest_period
Assuming I got this correct, then you can find out how many interest periods it will take by:
def how_long(start_money, interest_rate, final_money):
day = 0
money = start_money
while True:
if money >= final_money:
break
day += 1
money = start_money * (1 + interest_rate)**day
return day, money
In [5]: def test():
...: for i in range(0):
...: return '1'
...:
...:
In [6]: x = test()
In [7]: print x
------> print(x)
None
See the return value is 'None'.
I don't know what are you trying to do. But The basic mistake is the Argument of range(x) function. The range(0) always return empty list.
That's because you put range(0) which is an empty loop. Perhaps you could consider a while loop?
Your loop for i in range(0) doesn't actually execute. range(0) returns an empty list [] which will skip the body of your for loop.
Please explain what you think this does? Please update your question with an English-language explanation of how many times you think this look will work.
counter = 0
for i in range(0):
if (Bal * Int) == Tar:
print '1'
else:
counter + 1
Hint. The answer is zero. The question is "why?" and "what were you trying to do?"
You have been told the three or more problems with your code. If there's no particular reason to use a loop, it's better calculated with a formula:
future_value = present_value * (1 + interest_rate_per_period) ** number_of periods
or, for short,
f = p * (1 + i) ** n
f / p = (1 + i) ** n
log(f / p) = n * log(1 + i)
n = log(f / p) / log(i + i)
Example: I have $5000; how many years will it take to grow to $10000 at 10% per annum?
>>> from math import log
>>> f = 10000.0
>>> p = 5000.0
>>> i = 0.1
>>> n = log(f / p) / log(1 + i)
>>> n
7.272540897341713
>>>