필터 지우기
필터 지우기

Finding Largest Factor Using While Loop

조회 수: 6 (최근 30일)
Brandon Tsang
Brandon Tsang 2020년 5월 21일
편집: KSSV 2020년 5월 21일
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;

채택된 답변

KSSV
KSSV 2020년 5월 21일
편집: KSSV 2020년 5월 21일
n = 15 ;
largestfactor=floor(n/2);
while largestfactor>0
if mod(n,largestfactor)==0
break
end
largestfactor = largestfactor-1;
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by