1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Sever21 [200]
4 years ago
11

Write a program where you implement Modular Exponentiation (ME) using the square and multiplyapproach as a function which is cal

led in main. ME calculates a^k mod n. The program should get valuesfor a, k and n from the user. This code requires two steps. First k must be converted to a binaryrepresentation K consisting of a list of 0s and 1s. Second, Modular Exponentiation must be performedusing a, n and K[] as arguments.procedureBinaryK(k)K = empty list //hint: make K a vectortmp = ki = 0while tmp > 0add y mod 2 to K //hint: use pushbacktmp = (tmp-K[i])/2i++return KprocedureModularExpo(a, K, n)if n = 1return 0b = 1if k = 0return bA = aif K[0] = 1b = afor i = 1 to length(K)-1A = A*A mod nif K[i] = 1b = A*b mod nreturn b
Engineering
1 answer:
Eddi Din [679]4 years ago
8 0

Answer:

Explanation:

#include <iostream>

#include<vector>

using namespace std;

//calculating k value in binary digits

vector<int> BinaryK(int k){

  vector<int> temp;

  int r;

 

  while(k!=0)

  {

  r = k%2; //getting binary value at every step

  temp.push_back(r);

  k /= 2;

  }

  return temp;

}

int ModularExpo(int a, vector<int> k, int n){

  if(n == 1){ //if denominator is 1

      return 0;

  }

  int b = 1;

 

  bool zeros = std::all_of(k.begin(), k.end(), [](int i) { return i==0; }); //checking denominator is zero vector

  if(zeros){

      return b;

  }

 

  int A = a;

  if(k[0] == 1){

      b = a;

  }

  for(int i=0;i<k.size();i++){

      A = (A*A) % n;

      if(k[i] == 1){

          b = (A*b)%n;

      }

  }

  return b;

}

int main(){

  //declaring variables

  int a,k,n;

 

  //reading variables

  cout<<"Reading a: ";

  cin>>a;

  cout<<"Reading k: ";

  cin>>k;

  cout<<"Enter n: ";

  cin>>n;

 

  //declaring vector

  vector<int> vec;

  //calling functions

  vec = BinaryK(k);

  cout<<"Result is: "<<ModularExpo(a,vec,n);

  return 0;

}

You might be interested in
Please read the short case at the end of Chapter 8, "Mary Barra of General Motors Values Culture". Use the following prompts to
Tom [10]

Answer: Incoherent question

Explanation: This is an act of plagiarism at subjecting the tutor to unnecessary stress at answering the purported question.

4 0
4 years ago
Mobility refers to the ability to?
aev [14]

Answer:

Drive

Explanation:

Drive is a great example

4 0
3 years ago
In a full duplex communication by using UTP cable, Pt =140 w, Pr =10 w and NEXTdb =14,47. According to this information which an
Paraphin [41]
The answer that is correct is c
4 0
3 years ago
Water at 60°F passes through 0.75-in-internal diameter copper tubes at a rate of 1.2 lbm/s. Determine the pumping power per ft
Lelu [443]

Answer:

The pumping power per ft of pipe length required to maintain this flow at the specified rate 0.370 Watts

Explanation:

See calculation attached.

- First obtain the properties of water at 60⁰F. Density of water, dynamic viscosity, roughness value of copper tubing.

- Calculate the cross-sectional flow area.

- Calculate the average velocity of water in the copper tubes.

- Calculate the frictional factor for the copper tubing for turbulent flow using Colebrook equation.

- Calculate the pressure drop in the copper tubes.

- Then finally calculate the power required for pumping.

8 0
3 years ago
A circuit contains four 100 ohm resistors connected in series. If you test the circuit with a DMM you should read a total resist
olga nikolaevna [1]
C is the correct answer
8 0
3 years ago
Other questions:
  • A 12-kN tensile load will be applied to a 50-m length of steel wire with E = 200 GPa. Determine the smallest diameter wire that
    6·1 answer
  • 2. The metal to be welded is called the
    6·1 answer
  • In a particular application involving airflow over a heated surface, the boundary layer temperature distribution may be approxim
    6·1 answer
  • Assume we have already defined a variable of type String called password with the following line of code: password' can have any
    10·1 answer
  • In plane stress, a prismatic bar of constant cross-section has an infinite length. a) True b) False
    14·1 answer
  • A BOD test is to be run on a sample of wastewater that has a five-day BOD of 230 mg/L. If the initial DO of a mix of distilled w
    15·1 answer
  • Explain Flags in ARM Processor​
    5·1 answer
  • What is working fluid and mention five example of working fluid.​
    14·2 answers
  • The employer rejected a contractor's tender and accepted a different
    15·2 answers
  • Why do giant stars become planetary nebulas while supergiant stars become supernovas when their nuclear fusion slows and is over
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!