可以通过为IF语句提供一些布尔表达式来完成以下PL/SQL代码吗?
declare
N int := extract(year from sysdate);
begin
if INSERT_YOUR_BOOLEAN_EXPRESSION_HERE then
dbms_output.put_line('Current year is prime number.');
else
dbms_output.put_line('Current year is not prime number.');
end if;
end;
Run Code Online (Sandbox Code Playgroud)
显然,你无法定义自己的功能.
只允许使用标准的Oracle 11g PL/SQL函数.
该代码应在2014-9999年期间正常运行.
编辑:
答案被接受,但问题没有得到解决,因为它是所希望的.
那么,让我们继续吧.
让我们在不久的将来将时间间隔缩短到256年:2014-2269
您是否能够在Oracle服务器之外找到另一种解决方案,更聪明,更紧凑,没有外部依赖性?
在这项任务中取得成功需要一些好主意.
编辑#2:
目前的记录是41个符号长:not regexp_like(rpad(1,N,1),'^(11+)\1+$')
现在可能有人能提供更加出色的解决方案吗?
我会留下足够的印象,再给予一张特大号奖金.)
以至少1个符号击败当前记录.
好的,这是另一个解决方案:
declare
N int := extract(year from sysdate);
begin
if not regexp_like(rpad('1', N, '1'),'^1?$|^(11+?)\1+$')
then
dbms_output.put_line('Current year is prime number.');
else
dbms_output.put_line('Current year is not prime number.');
end if;
end;
Run Code Online (Sandbox Code Playgroud)
最好是32767(PL/SQL中varchar2的大小)