while loop, user-defined function, to find prime numbers

15 views (last 30 days)
Here is my problem. I understand how while loops work but the additional details are messing me up. Use a while loop to check divisors up to sqrt(n) ????? I don't know where to start.
I don't think I can use the isprime function.
Write a user-defined function that finds all the prime numbers between 1 and n. Name the function pr=prime(n), where the input argument n is a positive integer, and the output argument pr is a vector with the prime numbers. If a negative number or a number that is not an integer is entered when the function is called, an error message "The input argument must be a positive integer." is displayed.
Additional Details Use a while loop to check divisors up to sqrt(n) ?????
Thanks for the help
  9 Comments
Steven Lord
Steven Lord on 18 Oct 2022
@Podilapu If this is not a homework question, just use the primes or isprime functions.
If it is a homework question, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.

Sign in to comment.

Answers (2)

Paulo Silva
Paulo Silva on 5 May 2011
Vectorized versions:
function pr=prime(n)
%just makes a vector with all prime numbers from 1 to n
%no error check, I leave that simple task to you
v=1:n;
pr=v(isprime(v));
----------------------
function pr=ndivs(n)
%just makes a vector with all dividers from 1 to the floor of sqrt of n
%no error check, I leave that simple task to you
%floor(sqrt(n)); %this discards all the decimal part of the positive number
v=1:floor(sqrt(n));
pr=v(~rem(n,v));
----------------------
While loop versions:
function pr=prime(n)
%just makes a vector with all prime numbers from 1 to n
%no error check, I leave that simple task to you
np=1;c=0;
pr=zeros(1,n);
while np<=n
if isprime(np)
c=c+1;
pr(c)=np;
end
np=np+1;
end
pr=pr(1:c);
-------------------------
function pr=ndivs(n)
np=1;c=0;
pr=zeros(1,floor(sqrt(n)));
while np<=floor(sqrt(n))
if ~rem(n,np)
c=c+1;
pr(c)=np;
end
np=np+1;
end
pr=pr(1:c);
  4 Comments
Alok
Alok on 3 Aug 2022
Prompt the user for a number and check whether it is prime or not. Collect these prime numbers and write it to an output text file.
  1. Define a variable continue_flag to use in the while loop
  2. Also, create an empty vector called prime_numbers
  3. In the while loop, prompt the user for an integer input and determine whether it is a prime number or not
  4. Update the vector prime_numbers accordingly
  5. Also, prompt the user to continue or terminate the session and update the continue_flag variable accordingly
  6. Write the prime_numbers to an output file called prime_numbers_output.txt or a file of choice

Sign in to comment.


Matt Fig
Matt Fig on 5 May 2011
If the idea is to use a WHILE loop, you should think about what can be checked one at a time.
Take N = 9; SN = floor(sqrt(N)) = 3;
is N/2 an integer? No, so:
is N/3 an integer? Yes, so we know that N is not prime.
Take N = 17; SN = floor(sqrt(N)) = 4;
is N/2 an integer? No, so:
is N/3 an integer? No, so:
is N/4 an integer? No, so 17 is prime.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!