thaohothi: căn gì đó -> a^x -> dạng e^y | lny (lớp 11) -> chuỗi lũy thừa (Toán A?) -> còn lại là phép + - * / trên số thực -> + - * / số nhị phân.
Bó tay lun. Nói vậy hju chjt ljn.
megaownage: Giải bài toán
Y = x^n
Bài này viết bằng C hay hơn, nhưng ở đây lười biếng quá nên viết VBA (Access, Excel, vv…) cho lẹ
Private Const EPSILON = 1E-16
Sub test()
MsgBox (NthRoot(125, 3))
End Sub
Public Function Power(num As Double, index As Integer) As Double
' this looks messy but it actually gives a better precision than a simple num^index
Select Case index
Case 0
Power = 1
Case 1
Power = num
Case 2
Power = num * num
Case 3
Power = num * num * num
Case Else
Power = num ^ index
End Select
End Function
Public Function NthRoot(num As Double, index As Integer) As Double
' this uses binary root method to solve the equation
' check for some undesirable values first
If num < 0 Then
NthRoot = -1 ' can not work
Exit Function
ElseIf num = 0 Or num = 1 Then
NthRoot = num
Exit Function
End If
' this is where it really works
' first, get the two end numbers
Dim a1 As Double ' lower end value
Dim a2 As Double ' upper end value
Dim a3 As Double ' temporary register
Dim off As Double ' off value
Dim i As Integer
a1 = 0
a2 = num * 2
If num < 1 Then a2 = 2
' now its the trial and error section
Do ' start a loop
a3 = (a1 + a2) / 2
off = Power(a3, index) - num
If Abs(off) < EPSILON Then
Exit Do
ElseIf off > 0 Then
a2 = a3
Else
a1 = a3
End If
Loop ' end of do loop
NthRoot = a3
End Function
Tui nói là chia số nhị phân chứ đâu phải làm cái này. Cái này gọi là giải phương trình = cưa đôi khoảng nghiệm chứ nhị phân j (phần in đậm). Nghiệm mà ngoài 2 khoảng đó hay nghiệm phức thì có đến tết Công Gô cũng chưa ra. Với lại máy chạy cái này có ra cũng sẽ giật như súng Đột Kích.