Prime Factorization Function Script

8 Ansichten (letzte 30 Tage)
Anthony Bartsch
Anthony Bartsch am 26 Apr. 2020
Kommentiert: David Hill am 26 Apr. 2020
Write function script that accepts a value which should be prime factorized. The function script should output the respective factors of the input value.
Could someone help me with what this would look like in terms of code using loops/conditional statements?

Antworten (1)

David Hill
David Hill am 26 Apr. 2020
function pfactors=pfactor(x)
pfactors=[];
for k=primes(x)
while mod(x,k)==0
pfactors=[pfactors,k];
x=x/k;
end
end
end
  2 Kommentare
Anthony Bartsch
Anthony Bartsch am 26 Apr. 2020
Thank you! Could you rewrite your code without using primes() command by any chance?
David Hill
David Hill am 26 Apr. 2020
function pfactors=pfactor(x)
pfactors=[];
for k=2:x
while mod(x,k)==0
pfactors=[pfactors,k];
x=x/k;
end
end
end

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by