This function will check whether the number is prime or not. It should be noted that this function is valid for real numbers greater than 2.
Explanation: In the start of the function a variable 'tf' is created assuming that the number is a prime number. Now a for loop is created to check if the number is divisible by any numbers other than 1 and iteself. If it is divisible then the variable 'tf' becomes False and the loop is breaked. The final result will be true if the number is prime or else it will be false if the number is not prime.
The above code was formatted using hilite.me
Run the program and comment below the output you are getting for a given input.
Keywords: prime, matlab function, if else, isprim function
Some one may ask, why not use the built in isprime function. The simple answer is the isprime function of the MATLAB is useful for numbers which are less than 2^32. If the numbers are greater than 2^32 then this function can be used instead of isprime function. This program is very simple.
The program is as follows:
function tf = isprim(n) %this function will check whether the number is prime or not tf = true; for i = 2:n-1 if rem(n,i) == 0 tf = false; break end end
Explanation: In the start of the function a variable 'tf' is created assuming that the number is a prime number. Now a for loop is created to check if the number is divisible by any numbers other than 1 and iteself. If it is divisible then the variable 'tf' becomes False and the loop is breaked. The final result will be true if the number is prime or else it will be false if the number is not prime.
The above code was formatted using hilite.me
Run the program and comment below the output you are getting for a given input.
Keywords: prime, matlab function, if else, isprim function