Thread Subject: print prime

Subject: print prime

From: John Wong

Date: 24 Sep, 2009 12:23:03

Message: 1 of 3

I wrote a program, but this is so buggy I guess?
I have this P4 2.6Ghz pc & 1GB Ram, I guess it's running out of memory or something.

Some numbers it will print not a prime / is a prime.

Let say primeTest(1)
it will say it's a prime

but when you do like primeTest(10)
nothing shows instead, on the bottom of the matlab (where the start menu is located), it will say "busy".

is there something i misunderstood my own program?


function primeTest(x)
n = 3;
R=rem(x,n);
while R ~= 0 & n < sqrt(x) & n < 10
    n+2;
    r=rem(x,n);
end
if R ~= 0
    disp('Is a prime')
else
    disp('Not a prime')

end
end

Subject: print prime

From: Aurelien Queffurust

Date: 1 Oct, 2009 14:19:03

Message: 2 of 3

"John Wong" <gokoproject@gmail.com> wrote in message <h9fob7$hs7$1@fred.mathworks.com>...
> I wrote a program, but this is so buggy I guess?
> I have this P4 2.6Ghz pc & 1GB Ram, I guess it's running out of memory or something.
>
> Some numbers it will print not a prime / is a prime.
>
> Let say primeTest(1)
> it will say it's a prime
>
> but when you do like primeTest(10)
> nothing shows instead, on the bottom of the matlab (where the start menu is located), it will say "busy".
>
> is there something i misunderstood my own program?
>
>
> function primeTest(x)
> n = 3;
> R=rem(x,n);
> while R ~= 0 & n < sqrt(x) & n < 10
> n+2;
> r=rem(x,n);
> end
> if R ~= 0
> disp('Is a prime')
> else
> disp('Not a prime')
>
> end
> end

In your while loop your forget to assign n+2 to n which explains why you stay in thsi while loop and MATLAB stays busy:
replace n+2 by n = n+2

Subject: print prime

From: us

Date: 1 Oct, 2009 14:30:21

Message: 3 of 3

"John Wong" <gokoproject@gmail.com> wrote in message <h9fob7$hs7$1@fred.mathworks.com>...
> I wrote a program, but this is so buggy I guess?
> I have this P4 2.6Ghz pc & 1GB Ram, I guess it's running out of memory or something.
>
> Some numbers it will print not a prime / is a prime.
>
> Let say primeTest(1)
> it will say it's a prime
>
> but when you do like primeTest(10)
> nothing shows instead, on the bottom of the matlab (where the start menu is located), it will say "busy".
>
> is there something i misunderstood my own program?
>
>
> function primeTest(x)
> n = 3;
> R=rem(x,n);
> while R ~= 0 & n < sqrt(x) & n < 10
> n+2;

should read

     n=n+2;

us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
syntax us 1 Oct, 2009 10:34:04
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com