Prime Factorization Function Script
4 views (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
David Hill
on 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 Comments
David Hill
on 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
See Also
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!