Write a function called Factors of Number that takes as input a positive integer
ID: 3806018 • Letter: W
Question
Write a function called Factors of Number that takes as input a positive integer and returns the factors of the positive integer as an array. For example, if the integer is 6, the output is an array consisting of the values [1 2 3 6]. Test your function on the following numbers: 26, 64, 97 and 187 1 and the number itself are always factors. To find other factors, check up to the floor of the squareroot of the number. For example, if the number is 10, floor (Squareroot 10) = 3. 10 mod 2 equals 0. That means 2 and 5 (which is 10/2) are factors. In this case, it is OK to dynamically grow your array. 10 mod 3 equals 1, which means 3 is not a factor. Since we check up to 3, this completes the function and we return [1 10 2 5] as the factors of 10 (order is not important).Explanation / Answer
Code
==========
function f=FactorsOfNumbers(n)
K=1:n;
f = K(rem(n,K)==0);
end
FactorsOfNumbers(187)
-----------------------
output
================
1 11 17 187
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.