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
Paulo Silva
2011년 5월 5일
so you have doubts about your assignment and ask us instead of asking your teacher?
Cody
2011년 5월 5일
Cody
2011년 5월 5일
John D'Errico
2011년 5월 5일
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
2011년 5월 5일
Matt Fig
2011년 5월 5일
Nope. Counter example: sqrt(6)
francis
2022년 6월 22일
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
2022년 10월 18일
Ans plz
Steven Lord
2022년 10월 18일
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.
답변 (2개)
Paulo Silva
2011년 5월 5일
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
Cody
2011년 5월 5일
Paulo Silva
2011년 5월 5일
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
2022년 8월 3일
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
2022년 10월 18일
Ans plz
Matt Fig
2011년 5월 5일
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.
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!