|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
通用算法:
- Dim a&(), b(), c() As Boolean, k&, m&, n&, cnt&, tms#
- Sub 偶数矩阵相邻数相加为素数的递归排列计算()
- Dim i&
- [a:d] = ""
- tms = Timer
-
- m = 6
- n = m * m / 2 'm=4 实际范围为1-16
- ReDim a(3, n - 1), b(m - 1, m - 1)
- For i = 1 To n
- a(2, i - 1) = i * 2: a(3, i - 1) = i * 2 - 1
- Next
- c = GetPrime(4 * n - 1)
-
- k = 0: cnt = 0: Call dgPL2(1, 0): Call dgPL2(0, 0)
- MsgBox Format(Timer - tms, "0.000s ") & k & "/" & cnt
- End Sub
- Sub dgPL2(i&, t&)
- Dim i1&, j1&, j&, r&, f As Boolean
- cnt = cnt + 1: If cnt Mod 10000 = 0 Then DoEvents: Application.StatusBar = Format(Timer - tms, "0.000s ") & k & "/" & cnt
- If t = m * m Then If k > 2 Then k = k + 1 Else Cells(k * (m + 1) + 1, 1).Resize(m, m) = b: k = k + 1
- i1 = t \ m: j1 = t Mod m
- For j = 0 To n - 1
- If a(i, j) = 0 Then
- f = False: r = a(i + 2, j)
- If i1 = 0 Then f = True Else If c(b(i1 - 1, j1) + r) Then f = True
- If f Then If j1 Then f = c(b(i1, j1 - 1) + r)
- If f Then
- a(i, j) = 1: b(i1, j1) = r
- Call dgPL2(IIf(j1 = m - 1, i, IIf(i, 0, 1)), t + 1)
- a(i, j) = 0: b(i1, j1) = ""
- End If
- End If
- Next
- End Sub
- Function GetPrime(n&) '计算素数数列
- Dim a&(), b() As Boolean, i&, j&, k&, m&, s&
- m = n \ 2: ReDim a&(m), b(3 To n) As Boolean
- For i = 1 To Sqr(n) \ 2
- If a(i) = 0 Then
- s = i * 2 + 1: b(s) = True: k = k + 1: a(k) = s
- For j = (i * 3 + 1) To m Step s
- a(j) = 1
- Next
- End If
- Next
- For i = (a(k) + 1) / 2 To m
- If a(i) = 0 Then s = i * 2 + 1: b(s) = True ': k = k + 1: a(k) = s
- Next
- GetPrime = b
- End Function
复制代码 |
评分
-
2
查看全部评分
-
|