In MATLAB define a function named primes that takes a non-negative integer, ?, as its only argument and returns a row vector containing the first ? prime numbers in order. Assume that the first prime number is 2. Other than the zeros function, the ones function, and the colon (:) operator, you may not use any Matlab built-in array functions.
function [v] = primes(num) v = []; n = 2; while length(v) < num result = true; for i=2:n-1 if mod(n, i) == 0 result = false; end end if result v(1+length(v)) = n; end n = n+1; end end
Get Answers For Free
Most questions answered within 1 hours.