Search Program on this blog

Wednesday 19 August 2015

Write a function myprime that takes n, a positive integer, as an input and returns true if n is prime or returns false otherwise. Do not use the isprime or primes or factor built-­‐in functions. Hint: you can use the rem or fix functions.

function p = myprime(n)
%Input n is a positive integer
%Output p is a boolean value
%Example: p = myprime(281)
if n==2
    p=1;
elseif n==3
    p=1;
else
    for i=2:fix(n/2) %check number divisible till n/2
        if mod(n,i)==0
            p=0;
            return;
        end
    end
    p=1;
end

2 comments:

  1. Simply want to say your article is as astonishing. The clarity in your post is just cool and i can assume you’re an expert on this subject. Fine with your permission allow me to grab your feed to keep up to date with forthcoming post. Thanks a million and please continue the rewarding work.
    ld hardas

    ReplyDelete
  2. Thanks for your positive feedback.

    ReplyDelete