k C(k,2)
3 3 2^1+1
4 6 5^1+1
5 10 3^2+1
8 28 3^3+1
from
DefDbl A-Z
Dim crlf$, fct(20, 1)
Private Sub Form_Load()
Form1.Visible = True
Text1.Text = ""
crlf = Chr(13) & Chr(10)
For k = 3 To 1000000
v = k * (k - 1) / 2
f = factor(v - 1)
If f = 1 Then
Text1.Text = Text1.Text & k & Str(v) & crlf
End If
Next
Text1.Text = Text1.Text & crlf & " done"
End Sub
Function factor(num)
diffCt = 0: good = 1
n = Abs(num): If n > 0 Then limit = Sqr(n) Else limit = 0
If limit <> Int(limit) Then limit = Int(limit + 1)
dv = 2: GoSub DivideIt
dv = 3: GoSub DivideIt
dv = 5: GoSub DivideIt
dv = 7
Do Until dv > limit
GoSub DivideIt: dv = dv + 4 '11
GoSub DivideIt: dv = dv + 2 '13
GoSub DivideIt: dv = dv + 4 '17
GoSub DivideIt: dv = dv + 2 '19
GoSub DivideIt: dv = dv + 4 '23
GoSub DivideIt: dv = dv + 6 '29
GoSub DivideIt: dv = dv + 2 '31
GoSub DivideIt: dv = dv + 6 '37
If INKEY$ = Chr$(27) Then s$ = Chr$(27): Exit Function
Loop
If n > 1 Then diffCt = diffCt + 1: fct(diffCt, 0) = n: fct(diffCt, 1) = 1
factor = diffCt
Exit Function
DivideIt:
cnt = 0
Do
q = Int(n / dv)
If q * dv = n And n > 0 Then
n = q: cnt = cnt + 1: If n > 0 Then limit = Sqr(n) Else limit = 0
If limit <> Int(limit) Then limit = Int(limit + 1)
Else
Exit Do
End If
Loop
If cnt > 0 Then
diffCt = diffCt + 1
fct(diffCt, 0) = dv
fct(diffCt, 1) = cnt
End If
Return
End Function
I could have speeded up runtime if I had modified the factor function to stop after finding a second prime factor, but that would have required extra coding time.
|
Posted by Charlie
on 2018-03-29 10:11:46 |