%
% A10.m
%
% Find the first N prime numbers
%
% Programming Concepts: nested if statement inside a while loop,
% use of isprime() command
%
% INPUT: User is prompted to enter the number of primes to find
% OUTPUT: The first N primes are printed to the command window
%
% Matlab-Monkey.com
% 12/30/2014
% tidy-up your workspace
clc % clear command window
clear % clear all variables
close all % close all graphics windows
% get info from user
N = input('How many prime numbers do you want to find? ');
n = 1; % n will step through every integer until N primes are found
nPrimes = 0; % nPrimes will count the number of prime numbers found
while nPrimes < N % while loop goes until nPrimes = N, then it stops
if isprime(n)
fprintf('%i\n',n) % display prime numbers to command
% window as they are found
nPrimes = nPrimes + 1; % increment the number of primes found
end
n = n + 1; % increment the number counter
end