フィルターのクリア

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by