VBS 正则判别素数(质数)
2014-05-09来源:

代码如下:

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

更多信息请查看IT技术专栏

推荐信息
Baidu
map