This is in MATLAB !! Need help with this. I know how to do MATLAB prime numbers
ID: 3807558 • Letter: T
Question
This is in MATLAB!! Need help with this. I know how to do MATLAB prime numbers normally, but I do not know how to do the prime numbers with the required functions and method, (ex. the NaN function, and floorsqrt(n) function, etc...) Im not sure how to store the non prime numbers in an array either. Please make sure to fully read the prompt for all specific requirements, they are highlighted. Please run your code through MATLAB to make sure it works, it doesnt help if I get incomplete or partial script. Thank you.
Question 2 You will improve on the previous C++ primes assignment by implementing a different algorithm. Find prime numbers in this assignment and store them within an array. Assume you are finding all the prime number 20. Starting at 2, set all multiples of 2 larger than 2 to NaN. 2 3 4 5 Y6 7 8 9 N00 11 12 13 14 15 M6 17 N8 19 20 Then do that again for the next higher number, 3 2 3 4 5 6 7 8 9 No 11 12 13 H4 15 16 17 18 19 20 And then again for 4 and so on up to floor (sart (n) using a nested for loop At the end of this, all values that are not NaN at some stage is a prime on n. Then you must use isnan to identify and filter NaN values, and restore the prime array that contains only numbers. Write this code into a script named hw10b.m. Sample output: Enter a number: 20 Prime numbers are: 13 17 19 11Explanation / Answer
Matlab code
n = input('Enter a number: '); % Prompting the user to enter a number
X = 2:n; % Creating a vector x 2,3,4...20
for k = 2:floor(sqrt(n)) % Loop to generate a numbers from 2 to floor(sqrt(n))
for j = 2:floor(n/k) % loop to generate numbers from 2 to floor(n/k)
X(j*k-1) = NaN; % Changing the multiples to NAN
end
end
PrimeNumbers = X(~isnan(X)); % Constructing a vectro of prime numbers
disp('Prime numbers are: ');
disp(PrimeNumbers); % displaying the result
Result
Enter a number: 20
Prime numbers are:
2 3 5 7 11 13 17 19
Enter a number: 200
Prime numbers are:
Columns 1 through 13
2 3 5 7 11 13 17 19 23 29 31 37 41
Columns 14 through 26
43 47 53 59 61 67 71 73 79 83 89 97 101
Columns 27 through 39
103 107 109 113 127 131 137 139 149 151 157 163 167
Columns 40 through 46
173 179 181 191 193 197 199
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.