dimanche 28 décembre 2014

What is wrong with my MATLAB code?

I have the following code, i'm trying to get an output that will give me the primes until the number with the power needed to make that number below it. i.e $ n = p_1^{\alpha_1}p_2^{\alpha_2}p_3^{\alpha_3}... p_k^{\alpha_k} $



function [answer] = pfm( n )
assert( n == floor(n) && n>1 , 'This is only defined for naturals greater than 1!!' )
prim = primes(n);
alph = zeros(size(prim));
for i = 1:size(prim)
for j = 1:ceil(sqrt(n));
if mod(n,prim(i)^j) == 0
alph(i) = j;
end
end
end
answer = [prim; alph];
end


The problem i'm having is, this only works for 2, and i don't know why.


e.g. pfm(6) = 2 3 5 \1 0 0


i've tried re-writing it a few times, and also going through the algorithm part with pen and paper, and still not found my error. Please help =( (sorry i didn't know how to write the question properly)


Aucun commentaire:

Enregistrer un commentaire