Oracle-judge whether a number is prime ____oracle

Source: Internet
Author: User

The method of judging the prime number, starting from 2, all except itself and 1, can not be divisible, is the prime number.

Createor REPLACE

FUNCTION IsPrime (

Num in number)

Return BOOLEAN

Is

BEGIN

IF (num<=1) THEN

return FALSE;

End IF;

For I in 2. (num-1)

LOOP

IF (mod (num,i) =0) THEN

return FALSE;

End IF;

End LOOP;

return TRUE;

End;



Call this function

Declare

Aboolean;

Begin

A: =isprime (27);

If a then

Dbms_output.put_line (' prime number ');

Else

Dbms_output.put_line (' Non prime number ');

End If;

End

Return Result: false

Explanation: Cannot direct selectisprime from dual Oracle does not have a Boolean data type, but there are some in PLQSL, so LS has a problem, and in function write no problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.