回复 30楼 jklqwe111
似乎第一组不符合题意。mod3是否为限制条件。那个N=8时,六个自变量,自变量不能取3的倍数,不定方程有多少组解?第二组,粗略看符合题意,四个自变量,自变量不能取3的倍数,当N=14时,是那90组解吗?
素数问题的解决是我学习编程永恒的动力。
Sub inc(a() As Integer, l As Integer, m As Integer) '6位任意进制计数器,调用一次递增一下 Dim i As Integer i = l a(i) = a(i) + 1 While i > 0 And a(i) > m a(i) = 0 i = i - 1 a(i) = a(i) + 1 Wend End Sub Sub 按钮1_Click() Dim i As Integer, j As Integer, k As Integer, l As Integer, ll As Integer, n As Integer, m As Integer, inc_n(6) As Integer, add_n(100) As Integer, sum As Long Dim tt As Double tt = Timer n = Val(Sheet2.Range("A2")) m = Val(Sheet2.Range("B2")) For i = 0 To 6 inc_n(i) = 0 Next j = n - 5 k = 0 If m = 0 Then m = n + 1 '如果模值为0表示所有元素参与运算 For i = 1 To j If (i Mod m) > 0 Then '获取参与运算的所有元素(1至n-5,排除能整除m的元素) add_n(k) = i k = k + 1 End If Next sum = 0 While inc_n(0) = 0 l = 0 For i = 1 To 6 l = l + add_n(inc_n(i)) '根据计数器每位值对应的元素相加,获取实际x+y+z+...和值 Next ll = 0 If l = n Then sum = sum + 1 If k > 0 Then ll = ll * 6 inc inc_n, 6, k - 1 Wend Sheet2.Range("C2") = sum Sheet2.Range("D2") = Timer - tt End Sub Sub 按钮3_Click() Dim i As Integer, j As Integer, k As Integer, l As Integer, ll As Integer, n As Integer, m As Integer, inc_n(6) As Integer, add_n(100) As Integer, sum As Long Dim tt As Double '优化后的遍历算法 tt = Timer n = Val(Sheet2.Range("A8")) m = Val(Sheet2.Range("B8")) For i = 0 To 6 inc_n(i) = 0 Next j = n - 5 If m = 0 Then m = n + 1 For i = 0 To j add_n(i) = 0 If (i Mod m) > 0 Then add_n(i) = 1 Next sum = 0 While inc_n(0) = 0 l = 0 For i = 1 To 5 l = l + inc_n(i) + 1 If add_n(inc_n(i) + 1) = 0 Or l > n Then l = n Exit For End If Next sum = sum + add_n(n - l) inc inc_n, 5, j Wend Sheet2.Range("C8") = sum Sheet2.Range("D8") = Timer - tt End Sub
Sub 按钮4_Click() Dim i As Integer, j As Integer, k As Integer, l As Integer, ll As Integer, n As Integer, m As Integer, inc_n(6) As Integer, add_n(500) As Integer, sum As Long Dim tt As Double '再次优化后的遍历算法,速度提升了一个量级 tt = Timer n = Val(Sheet2.Range("A13")) m = Val(Sheet2.Range("B13")) For i = 0 To 6 inc_n(i) = 0 Next j = n - 5 If m = 0 Then m = n + 1 For i = 0 To j add_n(i) = 0 If (i Mod m) > 0 Then add_n(i) = 1 Next sum = 0 While inc_n(0) = 0 l = 0 For i = 1 To 5 If add_n(inc_n(i) + 1) = 0 Then '碰到无效元素的处理 l = n + 1 i = i + 1 End If If l < n Then l = l + inc_n(i) + 1 Else inc_n(i) = j '无效元素(能被整除的)或前5个未知数和大于N的处理 End If Next If l < n Then sum = sum + add_n(n - l) inc inc_n, 5, j Wend Sheet2.Range("C13") = sum Sheet2.Range("D13") = Timer - tt End Sub
Function Sol(ByVal sum&, ByVal n&, Optional ByVal mo&) As Long Dim s&, i& If sum < 1 Or n < 1 Or sum < n Then Exit Function If mo < 2 Then mo = sum + 1 If sum = n Or (n = 1 And (sum Mod mo)) Then Sol = 1: Exit Function For i = 1 To sum - n + 1 If i Mod mo Then Sol = Sol + Sol(sum - i, n - 1, mo) Next End Function Sub Calc() Dim t! t = Timer MsgBox Sol(60, 6, 3), , Timer - t & " seconds" End Sub
Function Sol(sum&, n&, ParamArray mo()) As Long Dim s&, i&, j&, b() If sum < 1 Or n < 1 Or sum < n Then Exit Function If sum = n Then Sol = 1: Exit Function ReDim b(sum) For j = 1 To sum b(j) = 1 For i = 0 To UBound(mo) - 1 Step 2 If j Mod mo(i) = mo(i + 1) Then b(j) = 0: Exit For Next Next If n = 1 Then If b(sum) = 1 Then Sol = 1: Exit Function End If For j = 1 To sum - n + 1 If b(j) = 1 Then Sol = Sol + Sol(sum - j, n - 1, mo) Next End Function Sub Calc() Dim t! t = Timer MsgBox Sol(60, 6, 3, 0, 5, 0, 5, 2, 7, 1, 7, 5, 11, 3), , Timer - t & " seconds" End Sub