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

sql写出判断是否素数

发布时间: 2022-12-21 01:19:22

‘壹’ 用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

‘贰’ 用SQL判断1050是否是素数

setserveroutputon
DECLARE
BEGIN
FORiIN2..1049
LOOP
IFmod(1050,i)=0THEN
dbms_output.put_line(1050||'不是素数');
return;
ENDIF;
ENDLOOP;
dbms_output.put_line(1050||'是素数');
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)



‘肆’ sql判断一个数是否为素数

我来回答吧:
先建立个判断函数,然后执行该函数,具体如下:

create function ChkIntIsSuShu(@No int)
returns tinyint
as
begin
if @No <=1
return 0
declare @maxV int, @Index int
set @maxV = @No -1
set @Index = 2

while @Index < @maxV
begin
declare @maxV2 int,@Index2 int
set @maxV2 = @maxV
set @Index2 = @Index
while @Index2 < @maxV2
begin
if @Index2 * @Index = @No
return 0
set @Index2 = @Index2 + 1
end
set @Index = @Index + 1
end
return 1
end

select dbo.ChkIntIsSuShu(13) -- 返回值1,表示素数,0表示非素数。

‘伍’ 用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 SERVER 2005中 如何判断一个数是素数

Hi我,我来回答吧:
先建立个判断函数,然后执行该函数,具体如下:

create function ChkIntIsSuShu(@No int)
returns tinyint
as
begin
if @No <=1
return 0
declare @maxV int, @Index int
set @maxV = @No -1
set @Index = 2

declare @maxV2 int,@Index2 int
set @maxV2 = @maxV
set @Index2 = @Index

while @Index < @maxV
begin
while @Index2 < @maxV2
begin
if @Index2 * @Index = @No
return 0
set @Index2 = @Index2 + 1
end
set @Index = @Index + 1
end
return 1
end

select dbo.ChkIntIsSuShu(13) -- 返回值1,表示素数,0表示非素数。

‘柒’ 用SQL判断1050是否是素数,写出程序,用SQL语言

declare @reg int;
declare @num int;

begin
set @num=11;
SET @reg=2;
while @reg <1050
begin
if(@num%@reg) = 0
PRINT CONVERT(VARCHAR(20),@num)+'不是素数';
ELSE
PRINT CONVERT(VARCHAR(20),@num)+'是素数';
BREAK;
END;
SET @reg=@reg+1;
END;