yangyuhan12138
2023-01-12 11:20:01 +08:00
所以最后我还是自己实现了一下
create or replace function multiply(num1 varchar2, num2 varchar2) return varchar2 as
TYPE number_array IS VARRAY(4000) OF number;
temp_array number_array;
len1 number;
len2 number;
p1 number;
p2 number;
mul number;
ss number;
final_result varchar2(4000);
begin
temp_array := number_array();
len1 := length(num1);
len2 := length(num2);
temp_array.extend(len1 + len2);
FOR i IN 1..temp_array.count
LOOP
temp_array(i) := 0;
END LOOP;
-- Loop through the digits of the first number
FOR i IN REVERSE 1..len1
LOOP
-- Loop through the digits of the second number
FOR j IN REVERSE 1..len2
LOOP
mul := TO_NUMBER(SUBSTR(num1, i, 1)) * TO_NUMBER(SUBSTR(num2, j, 1));
p1 := i + j - 1;
p2 := i + j;
ss := mul + temp_array(p2);
temp_array(p1) := temp_array(p1) + floor(ss / 10);
temp_array(p2) := mod(ss, 10);
end loop;
end loop;
FOR i IN 1..temp_array.count
LOOP
if temp_array(i) = 0 and length(final_result) is null
then
continue ;
end if;
final_result := final_result|| temp_array(i);
END LOOP;
return final_result;
end;
create or replace function factorial(n number) return varchar2 as
result varchar2(32767) := '1';
begin
for i in 1..n
loop
result := multiply(result, to_char(i));
end loop;
return result;
end;
select factorial(1000)
from DUAL;