當前位置:首頁 » 編程語言 » 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;