Skip to content

Instantly share code, notes, and snippets.

@amitk
Created May 22, 2020 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitk/9b6bd6ff063bbf787b41e53f90c3e24b to your computer and use it in GitHub Desktop.
Save amitk/9b6bd6ff063bbf787b41e53f90c3e24b to your computer and use it in GitHub Desktop.
function to check whether a number is prime or not
-- 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