Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

There are 2 parts in the question both are connected to each other. Part#1 is solved but can you please help solving part#2? Part#1Implement the

There are 2 parts in the question both are connected to each other. Part#1 is solved but can you please help solving part#2?

Part#1Implement the Consecutive Integer Checking algorithm for finding the greatest common divisor of two positive integers m and n as a procedure or method in a programming language C++ Include a counter in your implementation to record the number of integers that are checked before the answer is found for each pair of input. Write a main program to generate 100 pairs of integers randomly between 1000 and 20,000 and call the consecutive integer checking method for each pair. Determine which pair requires the most number of iterations, which pair requires the least number of iterations, and the average number of iterations overall. The most number of iterations used is for GCD(?, ?) = ?. The least number of iterations used is (?) for GCD(?, ?) = ?. The average number of iterations used for all 100 pairs is (?). Each question mark (?) above should show a specific number.

(Part#1 solution)

#include #include using namespace std;

int curr_counter=0; const int EXPERIMENTS=100;

int generateNum(){ int range=20000-1000+1; return 1000+rand()%range; }

int GCD(int a, int b) { curr_counter+=1; // Everything divides 0 if (a == 0) return b; else if (b == 0) return a; // base case else if (a == b) return a; // a is greater if (a > b) return GCD(a-b, b); else return GCD(a, b-a); } int main(){ double total_iterations=0; int highest_first_num=0;int highest_second_num=0; int highest_gcd=0,high_iter=0; int lowest_first_num=0;int lowest_second_num=0; int lowest_gcd=0,low_iter=0; int num_one, num_two, gcd; for(int exp=1; exp<=EXPERIMENTS; exp++){ num_one=generateNum(); num_two=generateNum(); curr_counter=0; gcd = GCD(num_one,num_two); if(exp==1){ highest_first_num=lowest_first_num=num_one; highest_second_num=lowest_second_num=num_two; highest_gcd=lowest_gcd=gcd; high_iter=low_iter=curr_counter; }else{ if (high_itercurr_counter){ lowest_first_num=num_one; lowest_second_num=num_two; lowest_gcd=gcd; low_iter=curr_counter; } } total_iterations+=curr_counter; }

// print output cout<<"The most number of iterations used is "<

Please help solving this part #2.

Part#2Implement the Euclids algorithm as a procedure or method in the same way and gather the output result for the same 100 pairs of randomly generated integers.

Compare the output results from the two solutions and describe the difference in the number of iteractions used.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions