% Project Euler Problem 7
% https://projecteuler.net/problem=7

primecount = 2; % assume we've found the first 2 already
i = 3;
while (primecount < 10001)
    i = i + 2; % skip even numbers they're never prime
    if is_prime(i)
        primecount = primecount + 1;
    end
end
fprintf('the 10001st prime number is %d\n', i);


% support functions

function b = is_prime(x)

if (x==2)
    b = true;
elseif (x < 2)
    b = false;
elseif (mod(x,2) == 0)
    b = true;
else
    b = true;
    csx = floor(sqrt(x));
    for i=3:2:csx
        if (mod(x,i) == 0)
            b = false;
        end
    end
end

end
