Question

In: Electrical Engineering

Write a recursive function (using Matlab) moreFactors(a,b,fact) that does the following: 1. Takes as an input...

Write a recursive function (using Matlab) moreFactors(a,b,fact) that does the following:
1. Takes as an input 3 positive integers.
2. Of the two integers a and b, the function returns the integer that has the most factors fact.
3. If both integers a and b have the same amount of factors fact, the function will return the larger integer.
Test your function with the following:
>> result=moreFactors(24,32,3)
result = 24
(24 = 3^1 · 2^3 , 32 = 2^5 )
>> result=moreFactors(32,24,3)
result = 24
>> result=moreFactors(80,168,2)
result = 80
(80 = 2^4 · 5, 168 = 2^3 · 3 · 7)
>> result=moreFactors(100,50,5)
result = 100
(100 = 2^2 · 5^2 , 50 = 2 · 5^2 )
Note: function must be recursive!

Solutions

Expert Solution

Code for the function is given in the following image-

The results after testing the function-
  

You can copy following code in Matlab and test it.

function [result] = moreFactors(a,b,fact)
if(rem(a,fact)==0)
m=0;
else
m=1;
end
if(rem(b,fact)==0)
n=0;
else
n=1;
end
if(m==0&&n~=0)
result=a;
elseif(m~=0&&n==0)
result=b;
elseif(m~=0&&n~=0)
result=max([a,b]);
elseif(m==0&&n==0)
fact=fact*fact;
result=moreFactors(a,b,fact); %Recursion
end
end


Related Solutions

B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix...
B. Write a function in MATLAB called findInverseOf2x2Matrix that takes in as input any square matrix of size 2 × 2 and returns back the inverse of that matrix if one exists. If no inverse exists, print back a suitable error message. You can safely assume that we will test your code on square matrices of size 2 × 2 only. You can always verify your answer by using the inbuilt inverse function in MATLAB, however, you cannot use the...
Using Matlab, write a function that takes numeric data as its input argument and prints a...
Using Matlab, write a function that takes numeric data as its input argument and prints a message to the Command Window stating if the number is positive, or negative, or 0. This function should transfer an output value to the Workspace ONLY when the input value is negative. Please include a copiable code and the associated screenshots.
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or any vector and simply returns back the transpose of that input. You can always verify your answer by using the inbuilt transpose function in MATLAB, however, you cannot use the transpose function directly when writing your code. Here is the starter code that we are providing to help you get started %x is the input vector or matrix. You can find the %size that...
Write a function in Matlab that takes as input the number n and a symmetric tridiagonal...
Write a function in Matlab that takes as input the number n and a symmetric tridiagonal matrix given as two vectors: n×1 vector v representing the main diagonal and (n−1)×1 vector w representing the upper diagonal. Have this function output the Cholesky factor of the matrix as a vector for the main diagonal and a vector for the upper diagonal and output the number of flops and, separately, the number of square roots used as well. Use only basic programming....
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
Matlab Create/write a function that takes an input of x and y data, and a string...
Matlab Create/write a function that takes an input of x and y data, and a string (either linear? or quadratic), sets up a linear system of equations (Ax = b), and solves and plots the model.
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
write a matlab function for frequency analysis using DFT. the function should take as input a...
write a matlab function for frequency analysis using DFT. the function should take as input a signal, and as output the number of sinusoids and their frequencies.
Write a recursive function in C++ named multiplyNumbers, which takes one int argument n as input...
Write a recursive function in C++ named multiplyNumbers, which takes one int argument n as input and returns the product of numbers from 1 to n.
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT