clear % This scripts attempts to find the sum of the first n primes
n=input('Enter a value for n');i = 1;prime_count=0;sum = 0;while prime_count <= n if ~isprime(i) % isprime returns TRUE if i is a prime
prime_count = prime_count + 1; sum = sum + i; end i = i + 1;endfprintf('The sum of the first %d primes is %d\n', n, sum);
MATLAB: Finding the sum of the first n primes and to use a while function
n primessumwhilewhile loop
Best Answer