用Oracle打印100以内所有的质数的代码
时间: 2024-04-11 19:27:06 浏览: 64
Oracle 社会信用代码效验 SQL语句
Sure! Here is an example code snippet in Oracle SQL to print all prime numbers within 100:
```sql
DECLARE
v_num NUMBER;
v_is_prime BOOLEAN;
BEGIN
FOR i IN 2..100 LOOP
v_num := i;
v_is_prime := TRUE;
FOR j IN 2..FLOOR(SQRT(v_num)) LOOP
IF MOD(v_num, j) = 0 THEN
v_is_prime := FALSE;
EXIT;
END IF;
END LOOP;
IF v_is_prime THEN
DBMS_OUTPUT.PUT_LINE(v_num);
END IF;
END LOOP;
END;
/
```
In this code, we use a nested loop to iterate through numbers from 2 to 100. For each number, we check if it is divisible by any number from 2 to its square root. If it is divisible, it is not a prime number and we set `v_is_prime` to FALSE. Otherwise, it is a prime number and `v_is_prime` remains TRUE. Finally, we use `DBMS_OUTPUT.PUT_LINE` to print the prime numbers.
阅读全文