Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 358
- Office Version
- 2003 or older
- Platform
- Windows
I want to find the recurring period string of a recurring decimal.
That is, for example:
1/3 = 0.(3) = 0.333333... with a period "3" of length 1.
1/7 = 0.(142857) = 0.142857142857... with a period "142857" of length 6.
1/15 = 0.0(6) = 0.066666... with a period "6" of length 1.
So, if my input string is x=0.142857142857142857142, the code should output 142857.
I have attempted a VBA function with a reference Java code at Periods and the code is:
But, this code is not giving the required output. Any suggestions?
That is, for example:
1/3 = 0.(3) = 0.333333... with a period "3" of length 1.
1/7 = 0.(142857) = 0.142857142857... with a period "142857" of length 6.
1/15 = 0.0(6) = 0.066666... with a period "6" of length 1.
So, if my input string is x=0.142857142857142857142, the code should output 142857.
I have attempted a VBA function with a reference Java code at Periods and the code is:
VBA Code:
Function findSequence(x As String) As String
Dim n As Long, i As Long, j As Long, k As Long
n = Len(x)
For i = 1 To n - 1
For j = i To n - 1
k = j Mod i: If k = 0 Then k = 1 Else k = k
If Mid(x, k, 1) <> Mid(x, j, 1) Then
i = j
Else
End If
Next j
findSequence = Mid(x, 1, i + 1) ' (The issue appears to be at this line.)
GoTo x1
Next i
findSequence = "Impossible"
GoTo x1
x1:
Exit Function
End Function
But, this code is not giving the required output. Any suggestions?