Question

In: Advanced Math

Discrete math 1) Using MatLab (any language is fine just say what language is used). function...

Discrete math

1) Using MatLab (any language is fine just say what language is used).

function d=lemma_gcd(a,b)
%This program will return the greatest common divisor for input variables a
%and b where a and b are integers such that they are not both 0. It uses
%Lemma 4.8.3 on page 225.

%Note: gcd(a,b)=gcd(-a,b)=gcd(a,-b)=gcd(-a,-b), so
a=abs(a);
b=abs(b);

%This following section sets up the arrays we will use to store the
%changing values of our variables as a sequence. "c" will be the count
%variabe, stored in "C", "a" and "b" will also bechanging and stored in "A"
%and "B" arrays through the iterations of the loops.

c=0;
C(1)=c; %Stores c=0 in the array, C
%Next find the initial a and b to kick the program into gear


if a==0 & b==0
fprintf('You cannot input these variables\n')
d=inf;
else
if floor(a)~=a
fprintf('You cannot input these variables\n')
d=inf;
if floor(b)~=b
fprintf('You cannot input these variables\n')
d=inf;
  
  
  
  
  
  
  
  
end
end
end

Solutions

Expert Solution

MATLAB CODE:-

function d=lemma_gcd(a,b)

%This program will return the greatest common divisor for input variables a

%and b where a and b are integers such that they are not both 0. It uses

%Lemma 4.8.3 on page 225.

%Note: gcd(a,b)=gcd(-a,b)=gcd(a,-b)=gcd(-a,-b), so

a=abs(a);

b=abs(b);

%This following section sets up the arrays we will use to store the

%changing values of our variables as a sequence. "c" will be the count

%variabe, stored in "C", "a" and "b" will also be changing and stored in "A"

%and "B" arrays through the iterations of the loops.

c=0;

C(1)=c; %Stores c=0 in the array, C

%Next find the initial a and b to kick the program into gear

if a==0 & b==0

fprintf('You cannot input these variables\n')

d=inf;

return

else

if floor(a)~=a

fprintf('You cannot input these variables\n')

d=inf;

return

end

if floor(b)~=b

fprintf('You cannot input these variables\n')

d=inf;

return

end

end

% Swap m and n if m less than n

% to allow the algorithm to function properly

if a==0

d=b;

return

end

if b==0

d=a;

return

end

if a < b

tmp = a;

a = b;

b = tmp;

end

% Result of modulus is zero so we have found the gcd

if mod(a,b) == 0

d = b;

else

d = lemma_gcd(b,mod(a,b)); % Euclid's algorithm

end

end


Related Solutions

Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the...
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the Weightlifting Walrus is training for a contest where it will have to lift 1000 kg. Wallace has some weight plates lying around, possibly of different weights, and its goal is to add some of the plates to a bar so that it can train with a weight as close as possible to 1000 kg. In case there exist two such numbers which are equally...
it is a question of discrete math RSA is the most widely used public key cryptosystem....
it is a question of discrete math RSA is the most widely used public key cryptosystem. In this discussion, you will apply RSA to post and read messages. For this reflection discussion, use the prime numbers p = 3 and q = 11. Using the public key e = 3, post a phrase about something that you found interesting or relevant in this course. Include only letters and spaces in your phrase. Represent the letters A through Z by using...
[Discrete math] Show that it is possible to arrange the numbers 1, 2, . . ....
[Discrete math] Show that it is possible to arrange the numbers 1, 2, . . . , n in a row so that the average of any two of these numbers never appears between them. [Hint: Show that it suffices to prove this fact when n is a power of 2. Then use mathematical induction to prove the result when n is a power of 2.] I saw the solution but I don't understand why permutation pi is using here.....
The language is MATLAB Write a function that will generate three random integers, each in the...
The language is MATLAB Write a function that will generate three random integers, each in the inclusive range from 10 to 80. It will then return a string consisting of the three integers joined together, and also a character vector consisting of the three integers joined together. For example, if the random integers are 11, 29, and 76, the string that is returned will be "112976" and the character vector that is returned will be '112976'. I'm really confused on...
(Using Matlab) and "while" function 1.   Write a program that prompts the User for if they...
(Using Matlab) and "while" function 1.   Write a program that prompts the User for if they would like to enter a real number. If yes, prompt the User for the real number. Continue to do this until the User enters “no” to the first question. After the User enters “no”, display the average of all the numbers entered. (Using Matlab) and "while" function 2.   Write a program that prompts the User for if they would like to enter a real...
1. What is the skip ahead method on discrete math? 2. How can I encrypt a...
1. What is the skip ahead method on discrete math? 2. How can I encrypt a message in a skip ahead method? 3. Some examples of encryption on a skip head method?
6-10 sentences and please include any references if used. What would Travis Hirschi say about using...
6-10 sentences and please include any references if used. What would Travis Hirschi say about using incarceration as a form of punishment? Based on his control theory, would he be for or against incarceration? Support your position.
DISCRETE MATH 1.Prove that the set of all integers that are not multiples of three is...
DISCRETE MATH 1.Prove that the set of all integers that are not multiples of three is countable.
Discrete Math. Problem 1. Consider the statement: “If an animal is an rhinoceros, then it has...
Discrete Math. Problem 1. Consider the statement: “If an animal is an rhinoceros, then it has a horn.” (a) Write down the CONVERSE of this statement. (b) Write down the CONTRAPOSITIVE of this statement Problem 2. Let x be a positive real number. Using the definition of rational number, write a proof by contraposition of the following: If x is irrational, then √ x + 6 is also irrational. Problem 3 Let n be an integer. Using the definition of...
Give me a MATLAB code which integrates any function using Romberg integration The code must work...
Give me a MATLAB code which integrates any function using Romberg integration The code must work fine or else I will down vote the answer badly Dont post if you dont know
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT