while loop, user-defined function, to find prime numbers
Show older comments
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
Paulo Silva
on 5 May 2011
so you have doubts about your assignment and ask us instead of asking your teacher?
Cody
on 5 May 2011
Cody
on 5 May 2011
John D'Errico
on 5 May 2011
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.
Cody
on 5 May 2011
Matt Fig
on 5 May 2011
Nope. Counter example: sqrt(6)
francis
on 22 Jun 2022
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
on 18 Oct 2022
Ans plz
Steven Lord
on 18 Oct 2022
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.
Answers (2)
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
Cody
on 5 May 2011
Paulo Silva
on 5 May 2011
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;
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.
- 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 or not
- Update the vector prime_numbers accordingly
- Also, prompt the user to continue or terminate the session and update the continue_flag variable accordingly
- Write the prime_numbers to an output file called prime_numbers_output.txt or a file of choice
Podilapu
on 18 Oct 2022
Ans plz
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 MATLAB 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!