Created
May 22, 2020 13:13
-
-
Save amitk/9b6bd6ff063bbf787b41e53f90c3e24b to your computer and use it in GitHub Desktop.
function to check whether a number is prime or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 1 is treated as prime as per this function. | |
create function is_prime(num integer) returns boolean as $$ | |
begin -- begin block; | |
for i in 2..(num/2) loop | |
if num % i = 0 then | |
return false; -- return false if it's get divided by any number smaller than half of it or half itself. | |
end if; | |
end loop; | |
return true; | |
end; $$ | |
language plpgsql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment