calculating big numbers in VB.Net - vb.net

i want to calculate x in this formula :
r=g^x mod n = B
and variables are :
Dim g As Double
Dim x As Double
Dim n As Double
Dim b As Double
Dim m As Double
Dim r As Double
x = 1
b = 9789467
g = 10895499
n = 16777216
m = 1
and here my codes :
Dim i As Integer
For i = 1 To 16777215
m = i
r = (g ^ m) Mod n
If r = b Then
MsgBox("result = " + i)
End If
Next
MsgBox("not found")
but , it's just works on small numbers
and with my numbers... not working :(
i would appreciate any solution guys :)
and even if u got the x value from any other languages ... it's fine :)
thank you :)

Add a reference to System.Numerics and use BigInteger

Related

What is different between Microsoft.VisualBasic.VBMath.Rnd and the old VB Rnd() function?

I am trying to convert some old VB code to .Net but am having a problem with the Rnd function.
Old Code
Private Function Decode() As String
Dim r As Integer
Dim x As Integer
Dim c As Integer
Dom Code As String = "m[n-Msr0Xn*ca8qiGeIL""7'&;,_*EV{M;[{2bEmg8u!^s*+O37!692{-Y4IS"
x = Int(Rnd(-7))
For r = 1 To Len(Code)
x = Int(Rnd() * 96)
c = Asc(Mid(Code, r, 1))
c = c + x
If c >= 126 Then c = c - 126 + 32
Decode = Decode & Chr$(c)
Next
End Function
The decoded text is "Bet you needed more than a pencil and paper to get this one!"
This is what I have done:
Private Function Decode() As String
Dim r As Integer
Dim x As Integer
Dim c As Integer
Dim Answer As String
Dim Code As String = "m[n-Msr0Xn*ca8qiGeIL""7'&;,_*EV{M;[{2bEmg8u!^s*+O37!692{-Y4IS"
x = CType(Microsoft.VisualBasic.VBMath.Rnd(-7), Integer)
For r = 0 To sList.Length - 1
x = CType(Microsoft.VisualBasic.VBMath.Rnd() * 96 - 0.5, Integer)
c = Asc(sList.Substring(r, 1))
c = c + x
If c >= 126 Then c = c - 126 + 32
Answer &= Chr(c)
Next
Return Answer
End Function
but this is what I get "Bet you needed morB th(n a pencil and paper to get this one!"
I suspect its how I am castng to an int but I can't figure out how.
When I run your "Old Code" (after correcting the typos) under Office VBA (which should have the same Rnd implementation as VB6), I get the same result as you say you get from VB.Net. Therefore, there must be an error in the string assigned to Code.
Public Function Decode() As String
Dim r As Integer
Dim x As Integer
Dim c As Integer
Dim Code As String
Code = "m[n-Msr0Xn*ca8qiGeIL""7'&;,_*EV{M;[{2bEmg8u!^s*+O37!692{-Y4IS"
x = Int(Rnd(-7))
For r = 1 To Len(Code)
x = Int(Rnd() * 96)
c = Asc(Mid(Code, r, 1))
c = c + x
If c >= 126 Then c = c - 126 + 32
Decode = Decode & Chr$(c)
Next
End Function
Thanks to TnTinMan for leading me to the solution. The problem was created when I copied the string. I used a I instead of an l. The font that was used has these two characters looking identical. I also mistook ' for `.
so basically all I had to do was subtract .5

VBA - Modular exponentiation

I've been trying to do Modular exponentiation in VBA for use in MS excel, but there seems to be a logical error which crashes Excel everytime i try to use the formula.
Function expmod(ax As Integer, bx As Integer, cx As Integer)
' Declare a, b, and c
Dim a As Integer
Dim b As Integer
Dim c As Integer
' Declare new values
Dim a1 As Integer
Dim p As Integer
' Set variables
a = ax
b = bx
c = cx
a1 = a Mod c
p = 1
' Loop to use Modular exponentiation
While b > 0
a = ax
If (b Mod 2 <> 0) Then
p = p * a1
b = b / 2
End If
a1 = (a1 * a1) Mod c
Wend
expmod = a1
End Function
I used the pseudocode which was provided here.
Here is an implementation I wrote a while back. Using Long rather than Integer enables it to handle higher exponents:
Function mod_exp(alpha As Long, exponent As Long, modulus As Long) As Long
Dim y As Long, z As Long, n As Long
y = 1
z = alpha Mod modulus
n = exponent
'Main Loop:
Do While n > 0
If n Mod 2 = 1 Then y = (y * z) Mod modulus
n = Int(n / 2)
If n > 0 Then z = (z * z) Mod modulus
Loop
mod_exp = y
End Function

Excel VBA #Value! Error

I have the following function that when I run it says #value! error.
I would appreciate any help.
Function Bootstrap(S As Object, Z As Object, L As Double)
Dim j As Integer
Dim a() As Double
Dim b() As Double
Dim n As Integer
Dim Q() As Double
Dim sum As Double
Dim P As Double
ReDim a(1 To n)
ReDim b(1 To n)
ReDim Q(1 To n)
dt = 1
sum = 0
Q(0) = 0
For j = 1 To n - 1
S.Cells(j, 1).Value = a(j)
Z.Cells(j, 2).Value = b(j)
P = Z(j) * (L * Q(j-1) - (L + dt * a(n) * Q(j))
sum = sum + P
Next j
Bootstrap = sum
End Function
Bootstrapping function calculates the following value
In fact I am trying to calculate this formula
Q(t,Tn)=(∑(j=1)to(n-1) Z(t,Tj)[LQ(t,Tj-1)-(L+dtSn)Q(t,Tj)]/[Z(t,Tn)(L+dt*Sn)] +(Q(t,Tn-1)L)/(L+dtSn)
Inputs given are[S1 ,S2,….Sn ],[Z(t,T1),Z(t,T2)…..Z(t,Tn)]and and L=0.4
Try this code : entered as =Bootstrap(A1:B1,A2:B2,0.4)
I have corrected the following
- Assigning the ranges to variants
- defining dt as double
- Dim Q() as 0 to n
- using A() and b() in the formula
- the input ranges are rows not columns
Function Bootstrap(S As Range, Z As Range, L As Double) As Double
Dim j As Integer
Dim a As Variant
Dim b As Variant
Dim n As Integer
Dim Q() As Double
Dim sum As Double
Dim P As Double
Dim dt As Double
n = Application.WorksheetFunction.Max(S.Columns.Count, Z.Columns.Count)
a = S.Value
b = Z.Value
dt = 1
sum = 0
ReDim Q(0 To n)
Q(0) = 0
For j = 1 To n - 1
P = b(1, j) * (L * Q(j - 1)) - (L + dt * a(1, j) * Q(j - 1))
sum = sum + P
Q(j) = sum
Next j
Bootstrap = sum
End Function
Take the habit to format and increment your code, especially before posting it!
You need to type the output of the function (on the line of the function name)
A parenthesis is missing from the line P = Z(j) * (L*Q(j-1)-(L+ dt * a(n) * Q(j))
n is empty (and so are a, b and Q) when you try to redim your arrays, so you need to define them!
Z(j) will also give you an error, because it is a Range, you need Z.Cells(i,j)
Try this :
Function Bootstrap(S As Range, Z As Range, L As Double) As Double
Dim j As Integer
Dim a() As Double
Dim b() As Double
Dim n As Integer
Dim Q() As Double
Dim sum As Double
Dim P As Double
n = Application.WorksheetFunction.Max(S.Columns.count, Z.Columns.count)
a = S.Value
b = Z.Value
dt = 1
sum = 0
ReDim Q(1 To n)
Q(0) = 0
'Q(1) = "??"
For j = 1 To n - 1
P = b(1, j) * (L * Q(j - 1)) - (L + dt * a(1, j) * Q(j - 1))
sum = sum + P
Q(j) = sum
Next j
Bootstrap = sum
End Function

Type mismatch error when generating random numbers

When I generate random numbers, I sometimes get (not always) the following error:
Run-time error '13': type mismatch.
on line Z = Sqr(time) * Application.NormSInv(Rnd()) (and the end of the second for loop).
Why do I get this error?
I think it has something to do with the fact that it contains Rnd().
Sub asiancall()
'defining variables
Dim spot As Double
Dim phi As Integer
Dim rd_cont As Double
Dim rf_cont As Double
Dim lambda As Double
Dim muY As Double
Dim sigmaY As Double
Dim vol As Double
Dim implied_vol As Double
Dim spotnext As Double
Dim time As Double
Dim sum As Double
Dim i As Long
Dim mean As Double
Dim payoff_mean As Double
Dim StDev As Double
Dim K As Double
Dim Egamma0 As Double
Dim mulTv As Double
Dim prod As Double
Dim U As Double
Dim Pois As Double
Dim Q As Double
Dim Z As Long
Dim gamma As Double
Dim payoff As Double
Dim payoff_sum As Double
Dim secondmoment As Double
Dim j As Long
Dim N As Long
Dim mu As Double
Dim sum1 As Double
'read input data
spot = Range("B3")
rd_cont = Range("C5")
rf_cont = Range("C4")
muY = Range("B17")
sigmaY = Range("B18")
lambda = Range("B16")
K = Range("F33")
implied_vol = Range("F35")
N = Range("F34")
vol = Range("B6")
'calculations
sum_BS = 0
payoff_BS = 0
mean_BS = 0
secondmoment_BS = 0
For j = 1 To N
spotnext = spot
spotnext_BS = spot
time = 0
sum1 = 0
time = 184 / (360 * 6)
For i = 1 To 6
' 'Merton uitvoeren
Egamma0 = Exp(muY + sigmaY * sigmaY * 0.5) - 1
mu = rd_cont - rf_cont
mulTv = (mu - lambda * Egamma0 - implied_vol * implied_vol * 0.5) * time
sum = 0
prod = 1
Do While sum <= time
U = Rnd()
Pois = -Log(U) / lambda
sum = sum + Pois
Q = Application.NormInv(Rnd(), muY, sigmaY)
gamma = Exp(Q) - 1
prod = prod * (1 + gamma)
Loop
prod = prod / (1 + gamma)
Z = Sqr(time) * Application.NormSInv(Rnd())
spotnext = spotnext * Exp(mulTv + implied_vol * Z) * prod
sum1 = sum1 + spotnext
Next i
mean = sum1 / 6
payoff = Application.Max(mean - K, 0)
payoff_sum = payoff_sum + payoff
secondmoment = secondmoment + payoff * payoff
Next j
Following up on the community wiki answer I posted, a possible solution is this:
Function RndExcludingZero()
Do
RndExcludingZero = Rnd()
Loop While RndExcludingZero = 0
End Function
Usage:
Z = Sqr(time) * Application.NormSInv(RndExcludingZero())
Rnd() returns values >=0 and <1.
At some point it is bound to return 0. When given 0 as input in Excel, NormSInv returns the #NUM!
Excel error.* When called in VBA via Application.NormSInv(0), it returns a Variant of subtype Error with value "Error 2036" (equivalent to the #NUM! Excel error).
Such Variant/Errors cannot be implicitly coerced to a numerical value (which is what the * operator expects) and thus in this case, you will get the type mismatch error.
You will only get this error when Rnd() happens to return 0, which is consistent with your observation that the error occurs only sometimes.
* This was first remarked by user3964075 in a now defunct comment to the question.

Function to insert a specific formula excel VBA (decimal to fractional inches)

I have a list of distances that I would like to display like you would read off a tape measure, for example 144.125 would display as 144 1/8". I have the following formula
=TEXT(A1,"0"&IF(ABS(A1-ROUND(A1,0))>1/32,"0/"&CHOOSE(ROUND(MOD(A1,1)*16,0),16,8,16,4,16,8,16,2,16,8,16,4,16,8,16),""))&""""
I'd like to simplify it to a 1 argument function (for A1) so I could use it throughout the workbook, but the amount of " quotes and vba keywords is causing problems. Is there an easier way to get a UDF to insert a complicated formula?
If you want to use a UDF with visual basic then try this:
Public Function Fraction(ByVal x As Double, Optional ByVal tol As Double = 1 / 64#) As String
Dim s As Long, w As Long, d As Long, n As Long, f As Double
s = Sgn(x): x = Abs(x)
If s = 0 Then
Fraction = "0"
Exit Function
End If
w = CInt(WorksheetFunction.Floor_Precise(x)): f = x - w
d = CInt(WorksheetFunction.Floor_Precise(1 / tol)): n = WorksheetFunction.Round(f * d, 0)
Dim g As Long
Do
g = WorksheetFunction.Gcd(n, d)
n = n / g
d = d / g
Loop While Abs(g) > 1
Fraction = Trim(IIf(s < 0, "-", vbNullString) + CStr(w) + IIf(n > 0, " " + CStr(n) + "/" + CStr(d), vbNullString))
End Function
With results:
The TEXT function can do this directly:
A B
1 144,1250 144 1/8 "
Formula in B1:
=TEXT(A1;"# ??/??\""")
Greetings
Axel