利用正則判別素數,來源於網絡,神人!
復制代碼 代碼如下:
Set regex = New RegExp
regex.Pattern = "^1?$¦^(11+?)1+$"
For i = 1 To 100
If Not regex.Test(String(i,"1")) Then
WScript.Echo i
End If
Next
看到標題你一定會不屑一顧,枚舉質數誰不會?
復制代碼 代碼如下:
For i = 1 To 100
For j = 2 To i
If i Mod j = 0 Then Exit For
Next
If j = i Then WScript.Echo i
Next
幾行代碼就能搞定,如此簡單。那就看看下面這個程序吧!
復制代碼 代碼如下:
Set regex = New RegExp
regex.Pattern = "^1?$|^(11+?)1+$"
For i = 1 To 100
If Not regex.Test(String(i,"1")) Then
WScript.Echo i
End If
Next
用正則表達式判斷質數,神一樣的人物。
來源:
http://demon.tw/programming/vbs-prime-number.html
http://www.enun.net/?p=533