
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find out the value of a given equation in Python
Suppose we are given five integer numbers a, b, c, d, n. We have to find out ((ab)(cd)) mod n. The output value is an integer number.
So, if the input is like a = 2, b = 3, c = 2, d = 4, n = 10, then the output will be 6.
2^3 = 8 2^4 = 16 8^16 = 281474976710656 281474976710656 mod 10 = 6
To solve this, we will follow these steps −
- Define a function helper() . This will take n
- p := n
- i := 2
- while i * i <= n, do
- if n mod i is same as 0, then
- p := p - floor value of (p / i)
- while n mod i is same as 0, do
- n := floor value of (n / i)
- if i is not same as 2, then
- i := i + 2
- otherwise,
- i := i + 1
- if n mod i is same as 0, then
- if n > 1, then
- p := p - floor value of (p / n)
- return p
- if b is same as 0 or (c is same as 0 and d is not same as 0) , then
- return (a ^ 0) mod n
- if c is same as 1 or d is same as 0, then
- return (a ^ b) mod n
- if a is same as 0 or a mod n is same as 0, then
- return 0
- if d is same as 1, then
- return (a ^ b * c) mod n
- p := helper(n)
- e := (c ^ d) mod p + p
- return (((a ^ b) mod n) ^ e) mod n
Example
Let us see the following implementation to get better understanding −
def helper(n): p = n i = 2 while i * i <= n: if n % i == 0: p -= p // i while n % i == 0: n = n // i if i != 2: i += 2 else: i += 1 if n > 1: p -= p // n return p def solve(a, b, c, d, n): if b == 0 or (c == 0 and d != 0): return pow(a, 0, n) if c == 1 or d == 0: return pow(a, b, n) if a == 0 or a % n == 0: return 0 if d == 1: return pow(a, b * c, n) p = helper(n) e = pow(c, d, p) + p return pow(pow(a, b, n), e, n) print(solve(2, 3, 2, 4, 10))
Input
2, 3, 2, 4, 10
Output
6
- Related Articles
- Python Program to find out the number of sets greater than a given value
- Program to find expected value of given equation for random numbers in Python
- Program to find out the largest sum value of a BST in a given binary tree in Python
- Program to find out the value of a power of 2 in Python
- Program to find max value of an equation in Python
- Program to find out the greatest subarray of a given length in python
- Python Program to find out the determinant of a given special matrix
- Program to Find Out the Occurrence of a Digit from a Given Range in Python
- Find the missing value from the given equation a + b = c in Python
- Program to find out the number of special numbers in a given range in Python
- Program to find out the maximum value of a 'valid' array in Python
- Program to find out number of distinct substrings in a given string in python
- Program to find out special types of subgraphs in a given graph in Python
- Program to Find Out the Number of Corrections to be Done to Fix an Equation in Python
- Program to find out the cells containing maximum value in a matrix in Python

Advertisements