General > Off Topic

Here is an interesting math challenge

<< < (3/3)

Prsn828:

--- Quote from: Botsareus ---Thank you , now I can read the mathematical notation.

In vb it looks like this:

Const e As Double = 2.71828
Private Sub UserForm_Click()
'A ^ A = e^(1/e^2).
Dim w As Double
Dim d As Double
w = 0.9 'input
d = Log(w) / Log(e)
If Abs(d) > 1 / e Then MsgBox "big" & " " & (Abs(d) - 1 / e)
If d = 0 Then q = 1 Else q = d / bigW(d) 'output
MsgBox q ^ q 'check this with input
End Sub

Function Fact(ByVal n As Long) As Long
If n = 1 Then GoTo b
Fact = n
Z = n
Do
Z = Z - 1
Fact = Fact * Z
Loop Until Z = 1
Exit Function
b:
Fact = 1
End Function

Function bigW(x As Double) As Double
For n = 1 To 12
bigW = bigW + ((-n) ^ (n - 1) / Fact(n) * x ^ n)
Next
End Function


Function Fact(ByVal n As Long) As Long
If n = 1 Then GoTo b
Fact = n
Z = n
Do
Z = Z - 1
Fact = Fact * Z
Loop Until Z = 1
Exit Function
b:
Fact = 1
End Function

Function bigW(x As Double) As Double
For n = 1 To 12
bigW = bigW + ((-n) ^ (n - 1) / Fact(n) * x ^ n)
Next
End Function
--- End quote ---
      ^
That | is why I hate writing code in BASIC :/

Basic is soooooooo ugly to look at, even if it does run really fast.  Oh well, I have to get used to it eventually I suppose.

Oh, and good job understanding Num's math help; he tries to explain Matrices to me for working on DB3, and all I have to say is "Come again?"

Numsgil:
Sorry, I was a math major after all

abyaly:

--- Quote ---the source code for that "Lagrange inversion theorem"
--- End quote ---

Botsareus:
Ok, ok, here we go, This is done the Russian way:


--- Code: ---VERSION 5.00
Begin VB.Form Form1
   Caption         =   "A ^ A = B"
   ClientHeight    =   5850
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   8295
   LinkTopic       =   "Form1"
   ScaleHeight     =   5850
   ScaleWidth      =   8295
   StartUpPosition =   3  'Windows Default
   Begin VB.TextBox outp2
      Height          =   375
      Left            =   3120
      Locked          =   -1  'True
      TabIndex        =   3
      Top             =   4440
      Visible         =   0   'False
      Width           =   3255
   End
   Begin VB.TextBox outp1
      Height          =   375
      Left            =   3120
      Locked          =   -1  'True
      TabIndex        =   2
      Top             =   3840
      Visible         =   0   'False
      Width           =   3255
   End
   Begin VB.CommandButton solve
      Caption         =   "Solve"
      Height          =   975
      Left            =   3120
      TabIndex        =   1
      Top             =   2640
      Width           =   3255
   End
   Begin VB.TextBox inp
      Height          =   375
      Left            =   3120
      TabIndex        =   0
      Top             =   1680
      Width           =   3255
   End
   Begin VB.Label AAA
      Caption         =   "A:"
      BeginProperty Font
         Name            =   "Arial"
         Size            =   72
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   1455
      Left            =   120
      TabIndex        =   9
      Top             =   3360
      Visible         =   0   'False
      Width           =   1455
   End
   Begin VB.Label BBB
      Caption         =   "B:"
      BeginProperty Font
         Name            =   "Arial"
         Size            =   72
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   1455
      Left            =   120
      TabIndex        =   8
      Top             =   1080
      Width           =   1455
   End
   Begin VB.Label sol2
      Caption         =   "Solution2:"
      Height          =   255
      Left            =   2160
      TabIndex        =   7
      Top             =   4440
      Visible         =   0   'False
      Width           =   855
   End
   Begin VB.Label sol1
      Caption         =   "Solution1:"
      Height          =   255
      Left            =   2160
      TabIndex        =   6
      Top             =   3960
      Visible         =   0   'False
      Width           =   855
   End
   Begin VB.Label aprox2
      Height          =   255
      Left            =   120
      TabIndex        =   5
      Top             =   720
      Width           =   8055
   End
   Begin VB.Label aprox1
      Height          =   255
      Left            =   120
      TabIndex        =   4
      Top             =   240
      Width           =   8055
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Const e As Double = 2.71828
Dim solution1 As Double
Dim solution2 As Double
Dim totsolutions As Byte

Private Sub solve_Click()
Dim mainin As Double
start:
mainin = Val(inp.Text)
If mainin > 1000000000000# Then
If MsgBox("The value you entered is too big. Press OK to put it back in range.", vbOKCancel, "A ^ A = B") = vbOK Then
inp.Text = 1000000000000#
GoTo start
Else
GoTo getout
End If
End If
If mainin < ((1 / e) ^ (1 / e)) Then
If MsgBox("The value you entered is too small. Press OK to put it back in range.", vbOKCancel, "A ^ A = B") = vbOK Then
inp.Text = ((1 / e) ^ (1 / e))
GoTo start
Else
GoTo getout
End If
End If
calc mainin
aprox1.Caption = ""
aprox2.Caption = ""
AAA.Visible = True
outp1.Visible = True
sol1.Visible = False
sol2.Visible = False
outp2.Visible = False
outp1.Text = solution1
If totsolutions = 1 Then
aprox1.Caption = "Approximation difference: " & Abs(solution1 ^ solution1 - mainin)
Else
aprox1.Caption = "Solution1 aproximation difference: " & Abs(solution1 ^ solution1 - mainin)
aprox2.Caption = "Solution2 aproximation difference: " & Abs(solution2 ^ solution2 - mainin)
sol1.Visible = True
sol2.Visible = True
outp2.Text = solution2
outp2.Visible = True
End If
Exit Sub
getout:
aprox1.Caption = ""
aprox2.Caption = ""
AAA.Visible = False
outp1.Visible = False
sol1.Visible = False
sol2.Visible = False
outp2.Visible = False
End Sub

Sub calc(c As Double)
Dim x As Double 'guess
Dim res As Double 'result
If c > 1 Then
 q = 1
 x = 0
 totsolutions = 1
Else
 totsolutions = 2
 q = 0.1
 x = (1 / e)
End If
Do
Do
x = x + q
res = x ^ x
Loop Until Round(res, 14 - Log( c )/ 2) >= Round(c, 14 - Log( c )/ 2)
x = x - q
q = q / 10
Loop Until Round(res, 14 - Log( c )/ 2) = Round(c, 14 - Log( c )/ 2)
x = x + q * 10
solution1 = Round(x, 15)
If c <= 1 Then
 q = 0.1
 x = (1 / e)
Do
Do
x = x - q
If x < 0 Then res = -1 Else res = x ^ x
Loop Until (Round(res, 14 - Log( c )/ 2) >= Round(c, 14 - Log( c )/ 2)) Or (x < 0)
x = x + q
q = q / 10
Loop Until Round(res, 14 - Log( c )/ 2) = Round(c, 14 - Log( c )/ 2)
x = x - q * 10
solution2 = Round(x, 15)
End If
End Sub
--- End code ---

Navigation

[0] Message Index

[*] Previous page

Go to full version