列出1-100的所有质数的两种方法

方法一

用dbms_output.put_line 打印出来

---打印1-100以内的质数(素数)
declare
  v_number number;
  v_temp   number;

begin
  for v_number in 1 .. 100 loop
    v_temp := 1;
  
    while v_temp < v_number loop
          v_temp := v_temp + 1;  --加1放在这里才能保证不被后面exit剔除哦
     if v_temp = v_number then      
        dbms_output.put_line(v_number);   --只有没有被任何数整除才会来到这一步
      end if;
      if mod(v_number, v_temp) = 0 then    --如被比自身小的数整除,则不是质数(素数),退出循环,否则,继续走到等于本身
        exit;
      end if;
    end loop;
  
  end loop;
end ;

方法二

建立表格,出入表中

create table T_TABLE
(
  prime NUMBER
)
tablespace SYSTEM   --不指定表空间则自动使用这个表空间
  pctfree 10
  pctused 40
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
create or replace procedure P_PRIME is   --INTEGER整数类型
begin
  DELETE FROM T_TABLE;
  DELETE FROM T_TEMP_TABLE;
  COMMIT;
  for a in 1 .. 100 loop
    insert into t_temp_table (temp_prime) values (a);
  end loop;
  insert into t_table
    select *
      from t_temp_table t
     where not exists (select *
              from t_temp_table t1, t_temp_table t2
             where t1.temp_prime * t2.temp_prime = t.temp_prime
               and t1.temp_prime <> 1
               and t2.temp_prime <> 1)
               and t.temp_prime != 1;
  COMMIT;
end P_PRIME;
原文地址:https://www.cnblogs.com/yhoralce/p/7599732.html