Prime Factorization Function Script

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?

Respuestas (1)

David Hill
David Hill el 26 de Abr. de 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 comentarios

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

Iniciar sesión para comentar.

Categorías

Más información sobre Performance and Memory en Centro de ayuda y File Exchange.

Productos

Versión

R2020a

Preguntada:

el 26 de Abr. de 2020

Comentada:

el 26 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by