Make a MATLAB function which count on all the prime numbers from 0-1000.
• Sort the numbers in descending order.
• Sum all the even numbers in ascending order from 0-100. Â
• Use only While loop.
function CountPrimes()
Counter = 0; % initialize to zero.
for x = 0:1000 %x is numbers in between 0 and 1000
y = isprime(x); %y is true if x is prime
if y % did we find a prime at this value of x?
Counter = Counter + 1; % increment the count of primes
end
end
disp(['The total number of primes found was: ',num2str(Counter)])
end
Sum = 0;
Counter = 1;
% Increase counter by *2* (not by 1)
% Until Counter=100
while Counter <= 100
Sum = Sum + Counter; % Matlab is case-sensitive
Counter = Counter + 2; % Increase the Counter
end
disp(Sum)
Comments
Leave a comment