Effacer les filtres
Effacer les filtres

Finding Largest Factor Using While Loop

6 vues (au cours des 30 derniers jours)
Brandon Tsang
Brandon Tsang le 21 Mai 2020
Modifié(e) : KSSV le 21 Mai 2020
Objective: Find largest factor of any input
For example:
>> out = largestfactor(15)
out = 5
I am using a while loop and if statement to perform this. I cannot figure out how to grab the largest value from the highest iteration (i). Thank you for looking over.
function [a] = largestfactor(number)
i = 2;
while(number > 1)
if mod(number,i) == 0
number = (number / i);
else
i = i + 1;
end
end
a = number * 5;

Réponse acceptée

KSSV
KSSV le 21 Mai 2020
Modifié(e) : KSSV le 21 Mai 2020
n = 15 ;
largestfactor=floor(n/2);
while largestfactor>0
if mod(n,largestfactor)==0
break
end
largestfactor = largestfactor-1;
end

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by