Im trying to figure out how to write a program that finds prime factors of a number. I have the following but it only works for the number 12 for some reason. % this program will determine if an integer is prime and determine
% its prime factors function MAIN clear; clc; % define number(s) to be checked, call function, and print output
N = 12; [answer,primefactors] = primey(N); fprintf('Is %i a prime? %s \n', N, answer) fprintf('The primefactors of %i are: \n' , N) fprintf('%i \n' , primefactors) N = 12; [answer,primefactors] = primey(N); fprintf('Is %i a prime? %s \n', N, answer) fprintf('The primefactors of %i are: \n' , N) fprintf('%i \n' , primefactors) end % end of function MAIN function [answer,primefactors] = primey(N); if mod(N,1)==0 & N/N==1 answer= 'Yes'; else answer= 'No'; end for k= 2:N-1 if mod(N,k)==0 factors(k)=k; end [a,b]= find(factors>0); b primefactors= [1:numel(b)-1] numel(b) for j= 1:numel(b)-1 if mod(b(j),j)==0 primefactors(j)=b(j) end end end end