水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)转载于百度百科。
注意点
水仙花数的计算,主要注意点是三位数的取模,Oracle数据库在计算除法的时候会有四舍五入的逻辑,所以在取模的时候需要运用到trunc语法,去除掉小数点后的数字。
主要代码如下:
declare
v_num number(3);
v_one number(1);
v_two number(1);
v_three number(1);
v_four number(5);
begin
v_num := &请输入一个三位数;
v_one := trunc(v_num / 100);
v_two := trunc(v_num / 10) mod 10;
v_three := v_num mod 10;
v_four := v_one * v_one * v_one + v_two * v_two * v_two +
v_three * v_three * v_three;
if (v_four = v_num) then
dbms_output.put_line('该数是水仙花数');
else
dbms_output.put_line('该数不是水仙花数');
end if;
end;