当前位置:首页 » 编程语言 » sql计算素数的个数
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql计算素数的个数

发布时间: 2022-09-05 02:52:38

⑴ 用T-sql语句求出1到100的素数

declare @num int,@flag int,@i int
set @num=1
while @num<=100
begin
set @flag=1 --flag=1 素数,flag=0 非素数
set @i=2
while @i<@num
begin
if @num%@i=0
begin
set @flag=0
break
end
set @i=@i+1
end
if @flag=1 and @num >2 --去掉1,2
print @num
set @num=@num+1
end

⑵ 使用SQL编写程序,输出100以内所有的素数


05以后可以这样写

withtemp1(col1)as
(select1col1
unionall
selectcol1+2col1
fromtemp1
wherecol1+2<=100
)
selectsum(col1)fromtemp1;

没测试,有问题你再追问吧,

***************************好吧,忘记素数是什么了,,写成了1,3,5,7这样的和了,,,

selectsum(a.number)
frommaster..spt_valuesa
wherea.numberbetween2and100anda.type='p'
andnotexists(select*frommaster..spt_valuesb
whereb.numberbetween2and100andb.type='p'
anda.number>b.number
anda.number%b.number=0)



⑶ 用 oracle中PL/SQL算法 求100内的素数

本过程输入参数inp,计算1到inp之间的素数

算法:
拿出1-inp之间的每个数i,用2到i的平方根之间的每个数去除,全部除不尽的即为素数,有一个能除尽的为非素数

set serverout on
create or replace procere is_prime(inp number)
as
i number;
j number;
is_prim boolean;
begin
dbms_output.new_line;
dbms_output.put(to_char(2)||' ');
for i in 3..inp loop
begin
is_prim:=true;
for j in 2..trunc(sqrt(i)) loop
if mod(i,j)=0 then
begin
is_prim:=false;
exit;
end;
end if;
end loop;
if is_prim then dbms_output.put(to_char(i)||' '); end if;
end;
end loop;
dbms_output.new_line;
end;
/

exec is_prime(100)

⑷ sql求2000~500内的素数的个数

selectcount(A.number)frommaster..spt_valuesA
wheretype='p'andnumberbetween200and500
andnotexists(select1frommaster..spt_valuesB
whereB.type='p'
andB.numberbetween2andsqrt(A.number)
andA.number%B.number=0
)

⑸ SQL /ACCESS 素数个数的求法

mn取最大,i>mn mn=i就是把最新的i值赋给i,依次递推,mn就是最大的素数值

⑹ 用SQL语句编写程序:求2~50内的素数

declare @a int,@b int,@i int
set @i=2
while @i<50
begin
set @a=2
set @b=0
while @a<=@i/2
begin
if @i % @a=0
begin
set @b=1
break
end
set @a=@a+1
end
if @b=0
print @i --@b
set @i=@i+1
end