Calculating pow(a,nCr) % m efficiently - combinatorics

I think there are some process for calculating pow(a,nCr)%b?
But I want to know how I can efficiently solve this problem in programming?

I can think of a way that's only ~O(n^2*log(m)) and doesn't require the use of big integers. I have a notion of a faster (something like O(k*log(m)*log(n))) approach if you can factor m and you can factor totient(m/gcd(a^k,m)) for some k, but it gets pretty hairy.
In any case for the O(n^2*log(m)) approach, we'll make use of the fact that nCr == (n-1)C(r-1) + (n-1)Cr
Here's a computation of nCr which exploits that:
def nCr(n0,r0):
memoized = {}
def go(n,r):
if r == 0 or r == n:
return 1
if (n,r) not in memoized:
memoized[(n,r)] = go(n-1,r-1) + go(n-1,r)
return memoized[(n,r)]
return go(n0,r0)
The code for your powChooseMod function would be almost identical:
def powChooseMod(a,n0,r0,m):
memoized = {}
def go(n,r):
if r == 0 or r == n:
return a
if (n,r) not in memoized:
memoized[(n,r)] = go(n-1,r-1) * go(n-1,r) % m
return memoized[(n,r)]
return go(n0,r0)

Related

finding the gcd without lists code not returning the desired output

I have been trying to learn algorithms and i have to do a basic program to find gcd of two numbers using python
so i wrote this code
def gcd_better(m, n):
i = min(m, m)
while i:
if (m % i) == 0 and (n % i) == 0:
return i
else:
i = i - 1
if __name__ == "__main__":
gcd_better(4, 20)
here I want to return i but the code is not doin that
can anyone please help me understand is there anything wrong in my code
Either print the answer in gcd or return a value, assign it to a variable in main, and print that
Since you're returning a value (i), do print(gcd_better(4, 20)) to see what is being returned.

what is the time complexity of this program

This code computes a^b.. But I am not sure about its complexity
def powerFunc(a,b):
if b==1:
return a
elif b==0:
return 1
else:
return a*powerFunc(a,b-1)
a=int(input())
b=int(input())
print(powerFunc(a,b))
With recursive functions, you can start with writing down the recurrence relation:
T(a,0) = O(1)
T(a,1) = O(1)
T(a,b) = T(a,b-1) + O(1) for b > 1
This is a very simple equation with solution
T(a,b) = O(b)
The space complexity is also O(b) since the function is not tail-recursive.

power (a, n) in PYTHON

POwer in Python. How to write code to display a ^ n using funсtion?
why doesn't this code working?
a = int(input())
n = int(input())
def power(a, n):
for i in range (n):
a=1
a *= n
print(power (a, n))
Few errors:
Changing a will lose your power parameter, use result (or something else).
Move setting result = 1 outside your loop to do so once.
Multiply by a not by n.
Use return to return a value from the function
def power(a, n):
result = 1 # 1 + 2
for _ in range (n):
result *= a # 3
return result # 4
Style notes:
Setting/mutating a parameter is considered bad practice (unless explicitly needed), even if it is immutable as is here.
If you're not going to use the loop variable, you can let the reader know by using the conventional _ to indicate it (_ is a legal variable name, but it is conventional to use it when not needing the variable).
Tip: you can simple use a**n
It doesn't work because your function doesn't return the end value. Add return a to the end of the function.
ALSO:
That is not how a to the power of n is is calculated.
A proper solution:
def power(a,n):
pow_a = a
if n is 0:
return 1
for _ in range(n-1): # Substracting 1 from the input variable n
pow_a *= a # because n==2 means a*a already.
return pow_a
and if you want to be really cool, recursion is the way:
def power_recursive(a,n):
if n is 0:
return 1
elif n is 1:
return a
else:
a *= power_recursive(a,n-1)
return a

Memory Error in Python Primality Testing program

def repeated(m, result, a, s, d):
check = True
r = 0
while r <= s - 1:
if result == m - 1:
check = False
return check
result = (result ** 2) % m
r = r + 1
return check
I need to write a primality testing python program to test very large numbers, like at least 100-digit numbers. The code above is part of the code for Miller Rabin deterministic primality test for repeated squaring. It works really slow for large numbers. How can I speed it up? It is for a project. Thanks!
your problem is probably the (result ** 2) % m, use the 3 argument version of pow that do the same but more efficiently because the algorithm use is the Modular exponentiation and that is much better than first doing x**n and then calculate its modulo. this way you are guaranty to never have a number bigger than m while if you do (x**n) % m you can have that x**n is very much bigger than m that may be the cause your problems
Also no need for the check variable and you don't use a.
Also as you go from 0 to s-1, better use range
def repeated(m, result, s, d):
for r in range(s):
if result == m - 1:
return False
result = pow(result, 2, m )
return True
Now for this part of the test
if
you need a, d, s, and n this is how I would do it
def mr_check(n,a,s,d):
result = pow(a,d,n)
if result == 1 :
return False
for r in range(s):
result = pow(result,2,n)
if result == n-1:
return False
return True

sum even integer in a list of integer (without loop)

I have a task: create a function that takes a integer list as parameter and returns the sum of all even integer in an integer list, without using any kinds of loop. All addition must be done by + operator only. Below is my solution
def sumTest(list_Of_Integers):
return sum(list(filter(lambda x: x%2 == 0, list_Of_Integers)))
I want to ask if there is any better solution, like without using the built-in sum() of python.
Thanks
As stated above in the comments, a more pythonic way of doing this is to use a list comprehension:
def sumTest(list_Of_Integers):
return sum([x for x in list_Of_Integers where x % 2 == 0])
As #UloPe states, however, this sounds very much like a homework question, in which case a more recursive method may be expected (rather than using the sum() function):
def sumTest2(xs):
if len(xs) == 0:
return 0
total = xs[0] if xs[0] % 2 == 0 else 0
return total + sumTest2(xs[1:])
This will generate a function stack dependent on the size of the list.
If you want to generate a shallower stack, then you can do the following:
def sumTest3(xs):
if len(xs) == 0:
return 0
midpoint = len(xs) / 2
total = xs[midpoint] if xs[midpoint] % 2 == 0 else 0
return sumTest3(xs[:midpoint]) + total + sumTest3(xs[midpoint + 1:])
The stack depth of this version will be log(size of list)

Resources