Encrypt the code using two key values
Bob has to send a secret code S to his boss. He designs a method to encrypt the code using two key values N and M. The formula that he uses to develop the encrypted code is shown below:
(((S^N)^M)00000007)
Write an algorithm to help Bob to encrypt the code.
Input:
The input to the function/method consists of three arguments secret code, an integer representing the secret code (S) value N, an integer representing the key value N and value M, an integer representing the key value M.
Output:
Return an integer representing the code encrypted by BOB.
Constraints:
1<=secret code<=10^9
0<=value N<=10^10
0<=value M<=10^10
Logic:
This is just an arithmetic calculation which is easy. But the important point to be noted is the constraints given. The secret code is can be up to 10^9, whereas the N and M values are up to 10^10. A normal int cannot hold such big values.
In C++, Unsigned long long int used which can hold from 0 to 18,446,744,073,709,551,615.
In Java, Big Integer is used which can hold from - 9,223,372,036,854,775,808 to 9,223,372,036,854,755,807.
The size of the input need not be considered in Python.
Program to encrypt the code using two key values
Asked in recruitment drive of Wipro.
C
#include
int main()
{
long int s,n,m,ans;
scanf(“%ld %ld %ld”,&s,&n, &m);
ans=pow(s,n);
ans=ans;
ans=pow(ans,m);
ans=ans00000007;
printf(“%ld”,ans);
return 0;
}
C++
#include
#include
#include<math.h>
using namespace std;
int main()
{
unsigned long long int S, N, M;
unsigned long long int ans;
cout<<“Enter the values of S, N, M:”< cin>>S>>N>>M; ans = pow(S,N); ans = ans; ans = pow(ans,M); ans = ans00000007; cout<<“Answer: “< return 0; } JAVA import java.util.Scanner; import java.math.BigInteger; public class Main { public static void main(String[] args) { BigInteger S, N, M, ans; System.out.println(“Enter the values of S, N, M: “); Scanner sc = new Scanner(System.in); S = sc.nextBigInteger(); N = sc.nextBigInteger(); M = sc.nextBigInteger(); BigInteger b1, b2; b1 = new BigInteger(“10”); b2 = new BigInteger(“1000000007”); ans = S.pow(N.intValue()); ans = ans.mod(b1); ans = ans.pow(M.intValue()); ans = ans.mod(b2); System.out.println(ans); } } PYTHON 3 S = int(input(“Enter the value of S: “)) N = int(input(“Enter the value of N: “)) M = int(input(“Enter the value of M: “)) ans = S ** N ans = ans ans = ans ** M ans = ans 00000007 print(ans) Output Input- Enter the value of S:2 Enter the value of N:3 Enter the value of M:4 Output- Answer:4096
Write a public review