# Determining if a number is prime without using a for loop

#### ckuylen

Joined Feb 1, 2012
8
Hello all. I'm new to this forum and I am amazed at the number and density of topics. I am a beginner to MATLAB and I am trying to learn the basics of matlab so I can code my own experiments. I am having trouble understanding one of our homework problems and thought someone here might be able to help me out.

Specifically, we are asked to create a function that will determine if a number is prime without using a for loop. In this circumstance your function should be vectorized. I have figured out how to determine if a number is prime with a for loop, but I am unsure how to determine if a number is prime by writing a function using a vector. Any help would be greatly appreciated.

function [noloop]=isPrime_noloop(n)
n = ones(1,40000000);
j = 2;
while(j<(40000000/2))
n((j*2):j:end) = 0;
j = j+1;
while(n(j)==0) % find next prime
j = j+1; end
end

As I said, I am not sure if I am on the right path or not, this is all new to me. I appreciate any and all responses.

Cheers!

#### hgmjr

Joined Jan 28, 2005
9,027
If this is a homework problem then I need to move it to the homework section of the forum.

Here at AAC, we prefer that students show their efforts to solve a problem on their own. You have done that nicely.

Our member are here to assist you with hints and suggestions. Someone with MATLAB experience should be along shortly to assist you,

Good luck,
hgmjr

#### ckuylen

Joined Feb 1, 2012
8
Well thank you super mod for filling me in. I tweaked this a little and it spits out columns from 1-40000000 and tells me where in these columns a prime number is located. This is not what I want!! I want to determine if a number that I select is prime using a vector (i.e. all I want to do is type in a value, lets say 37 and have matlab spit out a 1 or 0 to tell me if this number is prime).

Here is the tweaked version of the original code that now returns this massive matrix(?) of 1s and 0s.

function z = isPrime_noloop(x)
z = ones(1,40000000);
j = 2;
while(j<(40000000/2))
z((j*2):j:end) = 0;
j = j+1;
while(z(j)==0) % find next prime
j = j+1; end
end

#### t_n_k

Joined Mar 6, 2009
5,455
Are you allowed to use the Matlab inbuilt "primes()" function?

If so you could have something like

Rich (BB code):

x=input("Enter value to check if prime: ")
y=primes(x)
s=size(y)
if y(s(2))==x then result="T"
else result="F"
end

Last edited:

#### t_n_k

Joined Mar 6, 2009
5,455
Or more simply ....

Rich (BB code):
x=input("Enter value to check if prime: ")
y=primes(x)
if max(y)==x then result="T"
else result="F"
end

• ckuylen

#### ckuylen

Joined Feb 1, 2012
8
Well the instructions just said to use a vector without using a forloop so maybe this will work. I'll shoot him an email and see what he says. Thanks for your input.

#### ckuylen

Joined Feb 1, 2012
8
Well primes is out of the question, it is too easy or so I'm told .

Here is what I have done now which gives me the correct answer:

function [y]=isPrime_noloop(x)
z=[2:x-1];
if x./z==x
x=1
else x=0
end
end

The only question remaining is when I divide x by the numbers in z, what tells meif x isprime? Is it that the quotient is equal to x (like I wrote)?

Any help on this last point would be greatly appreciated.

#### t_n_k

Joined Mar 6, 2009
5,455
I've not convinced myself your code works - as shown.

I would do something like this for the code body as a command window script. Result = 'T' indicates the entered no. is prime.

Rich (BB code):
x=input("Enter number to test if prime:")
z=[2:x-1]
A=x./z
B=A-int(A)
if nnz(B)==nnz(A) then result='T'
else result='F'
end

#### stahta01

Joined Jun 9, 2011
133
In C, when not using loop is a requirement it often means using recursion instead.
Does, Matlab do recursion?
If yes, you might ask if that is needed in the answer.

I have no idea how using recursion can solve your problem.

Tim S.

#### ckuylen

Joined Feb 1, 2012
8
Well this is due in a few hours, so I'm not sure if anyone will get to it on time. I appreciate all of the advice so far and as I said I'm new so this is like trying to learn Mandarin for me.

Anyway this is what I have done now:
function [noloop]=isPrime_noloop(R)
%Determines if a number is prime without using a for loop
%Syntax: out=isPrime_noloop(x);
%Import: x=integer to check;
%Export: [noloop]=0 if not prime, 1 if prime
%
%My Name
%February 3 2012
Integer=y==round(y);
Vector=[2:R-1];
Divisor=R./vector;
Check=Divisor-Integer(Divisor);
if nnz(Check)==nnz(divisor)
[noloop]=1;
else [noloop]=0;
end

**Now instead of returning a 1 or a zero to tell me if a single answer is prime. It checks all values between 2 and 1 less than my input and returns something like this in the command window:
>> isPrime_noloop(47)

ans =

Columns 1 through 21
23 15 11 9 7 6 5 5 4 4 3 3 3 3 2 2 2 2 2 2 2

Columns 22 through 42
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Columns 43 through 45

1 1 1

**Although all I need is for it to tell me is if 47 is prime than all I should get for my output is a 1, but if it is not prime all I should get for my output is 0. I am unsure where all of these other values are coming from.

Again thank you all for your time and patience.

#### t_n_k

Joined Mar 6, 2009
5,455
Disregarding all the trimmings the core code lines below worked fine for me.

My Matlab clone [Scilab] uses int() for integer operation but round() also works OK. Why did you define the function Integer instead of just using the inbuilt round()?

You may need to watch your variable names if they are case sensitive in Matlab. You've used both upper and lower case for the same variable Vector / vector.

Rich (BB code):
function [noloop]=isPrime_noloop(R)
Vector=[2:R-1];
Divisor=R./Vector;
Check=Divisor-round(Divisor);
if nnz(Check)==nnz(Divisor) [noloop]=1;
else [noloop]=0;
end
end

• ckuylen

#### t_n_k

Joined Mar 6, 2009
5,455
Matlab fix() seems to have the same functionality as that of my earlier use of int() in Scilab ...