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

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

so you have doubts about your assignment and ask us instead of asking your teacher?
We had 3 problems didn't see this till I got home.
I know you can't take the square root of a prime number. But unsure how to make that a conditional statement.
We always get additional details to make the problems harder.
Why can't you take the square root? sqrt(3) exists. I'm pretty sure that sqrt(5) and sqrt(7) do also. They are simply not integers. But can you deal with that? Yes.
John, your right. So the condition would be primes appear when the square root is not an integer.
Nope. Counter example: sqrt(6)
1.Define a variable continue_flag to use in the while loop
  • Also, create an empty vector called prime_numbers
  • In the while loop, prompt the user for an integer input and determine whether it is a prime number of not
  • Update the vector prime_numbers accordingly
  • Also, prompt the user to continue or terminate the session
  • Write the prime_numbers to an output file called prime_numbers_output.txt or a file of choice
@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)

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

This is what I have came up with. It works but I'm not using a while loop or square root.
t=input('Enter a positive integer between 1 and N: ');
a = [1:t];
b = a.*isprime(a);
c=[1 b];
c=c(c>0);
disp(c)
ps. I'm not sure how to make my code show up special when I reply.
good work, the idea is good but can be improved like I did in the code above, the a = [1:t]; can be just a = 1:t;
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.

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 MATLAB in Help Center and File Exchange

Asked:

on 5 May 2011

Commented:

on 18 Oct 2022

Community Treasure Hunt

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

Start Hunting!