Write a C++ program following the instructions below. Please follow the order of the tasks given...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a C++ program following the instructions below. Please follow the order of the tasks given below, and write one function a time, test it, and then move on to the next function. 1. Implement Euclidean Algorithm for calculating greatest common divisor First write and test a function that calculates the greatest common divisor of two non- negative integers, as follows: /* precondition: a>-b>=0 */ /* postcondition: return d-gcd (a,b) */ int EuclidAlgGCD (int a, int b); Then implement the extended Euclidean Algorithm to find not only the greatest common divisor of two integers, but also find the integer coefficients that expresses the gcd in terms of the integers, as illustrated below: /* precondition: a>=b>=0 */ /* postcondition: return d-gcd (a,b), s and t are set so that d=sa+tb */ int ExtendedEuclidAlgGCD (int a, int b, int & s, int & t) ; 2. Modular arithmetic Write a function that returns the result of any integer modulo of n (i.e., reduce the integer modulo n). Note that the C++ operator % can be used, but it returns a negative value, for example, -1 % 3 yields -1, so we need to do something adjustment (recall that-1 mod 3 = 2 because -1= (-1)×3+2). Therefore, we will use the mod() function from this step to implement the later encode() and decode() functions. /* precondition: n is greater than 1, a can be negative postcondition: return a mod n (as defined in class) a mod n =r if and only if a = nk+r, 0 =<r <n (note that r needs to be non-negative). int mod (int a, int n); 3. Find Relatively Prime Write a function to return a number that is relatively prime with the given number. /* return an integer that is relatively prime with n, and greater than 1 i.e., the gcd of the returned int and n is 1 Note: Although gcd (n,n-1)=1, we don't want to return n-1*/ int RelativelyPrime (int n) 4. Find Inverse Then implement the following function, which returns the inverse modulo, and test it. Note that you need to check whether a and n are relative prime before calling this function. Recall that a and n are relatively prime means that gcd(a,n)=1, i.e., their greatest common divisor is 1. Also recall that the extended Euclidean Algorithm can find the integer s and t for a and n such that as+nt=gcd(a,n), where s is the inverse of a modulo n. /* n>1, a is nonnegative */ /* a< n */ /* a and n are relative prime to each other */ /* return s such that a*s mod n is 1 */ int inverse (int a, int n). ( int s, t; int d ExtednedEuclidAlgGCD (n, a, s, t); if (d==1) ( } else ( } return (mod (t, n)); //t might be negative, use mod() to reduce to // an integer between 0 and n-1. cout <<"a and n are not relatively prime!\n"; 5. Practice with RSA algorithm 1. Pick two prime numbers p, q, for example, ● 2. Find a e that is relatively prime with (p-1)(q-1) Call RelativelyPrime () 3. Calculate the inverse modulo (p-1)(q-1) of e to be your d Use inverse () 4. Write a function Encode as foll ● ● const int P-23; const int Q=17; int PQ-P*Q; 5. Write a function Decode as follows: //Return C^d mod PQ ● int Decode (int C, int d, int PQ); ● // Return M^e mod PQ int Encode (int M, int e, int PQ); 6. Verify that RSA algorithm works, i.e., ● . int M; /* M is an integer that is smaller than PQ */ cout <<"Enter an integer that is smaller than" << PQ; cin <<M; C=1 code (M, e, PQ); M1-Decode (C, d, PQ) ; assert (M=-M1); //Note: include assert.h header file to use this //macro/function. NOTE: 1. Due to the c++ compiler issue, c++ might cannot handle the operation of raising an integer to a large power. Here is an instance of p and q that can work: p=3, q=7 ⇒ pq = 21, (p-1)(q-1) = 12, pick e = 5, then d = 5 (5*5 mod 12 = 1, so 5 is the inverse of 5 mod 12) 2. If we want to randomly generate two initial prime numbers, then we need a function to test primality. Use the definition of prime number to guide your implementation of the following function. For any positive integers n, r, s, if n=rs, then r<=sqrt(n) or s<=sqrt(n) /* return true if n is prime return false if n is not prime Precondition: n>1 bool IsPrime (int n) Write a C++ program following the instructions below. Please follow the order of the tasks given below, and write one function a time, test it, and then move on to the next function. 1. Implement Euclidean Algorithm for calculating greatest common divisor First write and test a function that calculates the greatest common divisor of two non- negative integers, as follows: /* precondition: a>-b>=0 */ /* postcondition: return d-gcd (a,b) */ int EuclidAlgGCD (int a, int b); Then implement the extended Euclidean Algorithm to find not only the greatest common divisor of two integers, but also find the integer coefficients that expresses the gcd in terms of the integers, as illustrated below: /* precondition: a>=b>=0 */ /* postcondition: return d-gcd (a,b), s and t are set so that d=sa+tb */ int ExtendedEuclidAlgGCD (int a, int b, int & s, int & t) ; 2. Modular arithmetic Write a function that returns the result of any integer modulo of n (i.e., reduce the integer modulo n). Note that the C++ operator % can be used, but it returns a negative value, for example, -1 % 3 yields -1, so we need to do something adjustment (recall that-1 mod 3 = 2 because -1= (-1)×3+2). Therefore, we will use the mod() function from this step to implement the later encode() and decode() functions. /* precondition: n is greater than 1, a can be negative postcondition: return a mod n (as defined in class) a mod n =r if and only if a = nk+r, 0 =<r <n (note that r needs to be non-negative). int mod (int a, int n); 3. Find Relatively Prime Write a function to return a number that is relatively prime with the given number. /* return an integer that is relatively prime with n, and greater than 1 i.e., the gcd of the returned int and n is 1 Note: Although gcd (n,n-1)=1, we don't want to return n-1*/ int RelativelyPrime (int n) 4. Find Inverse Then implement the following function, which returns the inverse modulo, and test it. Note that you need to check whether a and n are relative prime before calling this function. Recall that a and n are relatively prime means that gcd(a,n)=1, i.e., their greatest common divisor is 1. Also recall that the extended Euclidean Algorithm can find the integer s and t for a and n such that as+nt=gcd(a,n), where s is the inverse of a modulo n. /* n>1, a is nonnegative */ /* a< n */ /* a and n are relative prime to each other */ /* return s such that a*s mod n is 1 */ int inverse (int a, int n). ( int s, t; int d ExtednedEuclidAlgGCD (n, a, s, t); if (d==1) ( } else ( } return (mod (t, n)); //t might be negative, use mod() to reduce to // an integer between 0 and n-1. cout <<"a and n are not relatively prime!\n"; 5. Practice with RSA algorithm 1. Pick two prime numbers p, q, for example, ● 2. Find a e that is relatively prime with (p-1)(q-1) Call RelativelyPrime () 3. Calculate the inverse modulo (p-1)(q-1) of e to be your d Use inverse () 4. Write a function Encode as foll ● ● const int P-23; const int Q=17; int PQ-P*Q; 5. Write a function Decode as follows: //Return C^d mod PQ ● int Decode (int C, int d, int PQ); ● // Return M^e mod PQ int Encode (int M, int e, int PQ); 6. Verify that RSA algorithm works, i.e., ● . int M; /* M is an integer that is smaller than PQ */ cout <<"Enter an integer that is smaller than" << PQ; cin <<M; C=1 code (M, e, PQ); M1-Decode (C, d, PQ) ; assert (M=-M1); //Note: include assert.h header file to use this //macro/function. NOTE: 1. Due to the c++ compiler issue, c++ might cannot handle the operation of raising an integer to a large power. Here is an instance of p and q that can work: p=3, q=7 ⇒ pq = 21, (p-1)(q-1) = 12, pick e = 5, then d = 5 (5*5 mod 12 = 1, so 5 is the inverse of 5 mod 12) 2. If we want to randomly generate two initial prime numbers, then we need a function to test primality. Use the definition of prime number to guide your implementation of the following function. For any positive integers n, r, s, if n=rs, then r<=sqrt(n) or s<=sqrt(n) /* return true if n is prime return false if n is not prime Precondition: n>1 bool IsPrime (int n)
Expert Answer:
Answer rating: 100% (QA)
1 Euclidean Algorithm for GCD include int EuclidAlgGCDint a int b while b 0 int temp b b a b a temp ... View the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
Please do the following Matlab problem, 1.Write a for loop ranging from 3 to 5. In the loop, print the value of the loop index to the screen using fprintf in the following format: Value is n where n...
-
One end of a light, elastic string, of natural length 1.2m and modulus of elasticity 32N, is attached to a fixed point, B. A particle, P, of mass 1.5 kg, is then attached to the other end of the...
-
A 3-m3 rigid vessel contains steam at 10 MPa and 500C. The mass of the steam is (a) 3.0 kg (b) 19 kg (c) 84 kg (d) 91 kg (e) 130 kg
-
A study by the Pew Internet and American Life Project (pewinternet.org) found that Americans had a complex and ambivalent attitude toward technology. (Data extracted from M. Himowitz, "How to Tell...
-
Go to the St. Louis Federal Reserve FRED database, and find data on real GDP (GDPC1), potential GDP (GDPPOT), and the unemployment rate (UNRATE) from 1960 to the most recent period. For the...
-
Cole Corporation issued $400,000, 7%, 20-year bonds on January 1, 2014, for $360,727. This price resulted in an effective-interest rate of 8% on the bonds. Interest is payable annually on January 1....
-
Who developed the relational model, when, and why?
-
Write a program that produces calendars as output. Your program should have a method that outputs a single months calendar like the one below, given parameters to specify how many days are in the...
-
ABC Co. is in the process of preparing its Selling and Administrative Expense Budget for the last half of the year. The following budget data are available: Sales commissions Shipping Advertising...
-
Rajasthani Umbrella is a seasonal business that sells umbrellas. At the peak of its rainy selling season, the firm has INR 2,500,000 in cash, INR 3,600,000 in inventory, INR 400,000 in accounts...
-
Derwent Ltd. has announced that the earnings before income and taxes is going to be 300,000 for the current year. Assuming corporate tax rate for Derwent Ltd. is a flat 30%, compute the firms profit...
-
Jennifer has invested in two schemes. The first scheme has a required return of 12% and will produce a stream of 300 at the end of each year indefinitely. The second scheme has a required return of...
-
Ocean Terminal Company Ltd. started its business in 2019 with retained earnings of $68.45 million. It paid two semiannual dividends of $0.23 per share to 3.25 million preferred stockholders during...
-
Suppose you have been offered an investment opportunity that will pay you $500 at the end of every year, starting one year from now and continuing forever. Assume the relevant discount rate is 6%. a....
-
BEBE Co. has the following information relating to its shareholders equity as of January 1, 2020. Share capital, 200,000 authorized, P50 par value, 150,000 shares issued, 10,000 held in treasury...
-
Reconsider Prob. 1474. In order to drain the tank faster, a pump is installed near the tank exit as in Fig. P1475. Determine how much pump power input is necessary to establish an average water...
-
Dr. Ivan I. Incisor and his wife Irene are married and file a joint return for 2012. Ivan's Social Security number is 477-34-4321 and he is 48 years old. Irene I. Incisor's Social Security number is...
-
Janie graduates from high school in 2012 and enrolls in college in the fall. Her parents pay $4,000 for her tuition and fees. a. Assuming Janie's parents have AGI of $170,000, what is the American...
-
Ray and Maria Gomez have been married 3 years. They live at 1610 Quince Ave., McAllen, TX 78701. Ray works for Palm Oil Corporation and Maria works for the City of McAllen. Maria's Social Security...
-
Kwon Cellular provides cell phones and 1 year of cell service to students for an upfront, non-refundable fee of HK$300 and a usage fee of HK$5 per month. Students may renew the service for each year...
-
Show that the positive and negative real integers (including 0) form a group under the operation of addition.
-
Express the inverse \((\mathbf{A B})^{-1}\) of the product \(\mathbf{A B}\) in terms of \(\mathbf{A}\) and \(\mathbf{B}\).
Study smarter with the SolutionInn App