資料庫中如何求1 100以內的素數

2021-07-14 22:51:04 字數 1777 閱讀 5262

100以內的素數。 在

vb中程式可以這樣寫:

private sub form_click()

dim i as integer, j as integer, x as integer

for i = 1 to 100

x = 0

『i是素數的標記

for j = 2 to i - 1

『如在此範圍內都不能整除,

i就是素數

if i mod j = 0 then x = 1

『如整除了,就不是素數

next j                                    『對1和i本身之外的所有數進行取餘運算

if x = 0 then print i

』如是素數,就輸出i

next i

end sub

declare @i int

declare @j int

--宣告變數及型別

set @i=

1                                   

while @i<=100

begin

declare @bol int     --宣告變數作為標記

set @bol=1           --為1是素數,0不是素數

set @j=2               

while@j<=sqrt(@i)       』j每一次迴圈都是從2開始,i的平方根,所以只有當i=1、 2、3時不滿足條件直接跳過begin……end迴圈,輸出i

begin

if @i

%@j=0                   --如果可以整除,執行下面第乙個begin……end語句,@bol=0,i不是素數

begin

set @bol=0

break

end

set @j=@j+1                   --對每乙個不能整除的i都要執行除了1和i本身的之間的每乙個數取餘

end

if @bol=1 print @i

set @i=@i+1

end————

100以內的素數

對上面的運算進行修改,可以完善結果顯示的方式

declare @t table ( 結果 int )  

declare @i int 

set @i = 1  

while ( @i <= 100 )   

begin  

declare @j int

set @j = sqrt(@i)                            --對j每一次賦值為i的平方根

while ( @j >= 2 )   

begin  

if (@i % @j = 0 )

break

set @j = @j - 1  

end  

if ( @j = 1 ) insert  into @t 

select  @i  

set @i = @i + 1  

end  

select * from @t     

%在資料庫中

意思表示兩種情況

,一種是 取餘數

,一種是 模糊比較

like

裡面的 萬用字元, 匹配 乙個或者多個字元。

資料庫中求所有資料總數

在黑視窗中很容易實現 select count from article 但是如果你要把它在php中顯示出來單純的使用 sql select count from article num mysqli query link,sql 你會發現這個這並不是個數字而是乙個鍵值對陣列 所以你使用它的時候記得...

如何保障sql server資料庫中的資料安全

剛才有人在 中問我如何保障筆記本中sql server資料 例如存放著核彈發射密碼,哈哈 的安全,要點記錄下 筆記本安全 廢話,筆記本都丟了,還搞什麼搞啊 系統安全 無毒無木馬無惡意程式 sql server自身安全 資料加密 牛吧,衝破重重障礙,發現獲取的資料加密了,1 筆記本安全 管理制度 高移...

如何刪除資料庫中的資料?

php mysql delete delete 語句用於從資料庫表中刪除行。刪除資料庫中的資料 delete from 語句用於從資料庫表中刪除記錄。語法 delete from table name where some column some value 注釋 請注意 delete 語法中的 w...