In the 'classical' Tribonacci sequence, each value is the sum of the previous three, like so:
T(n)= T(n-1) + T(n-2) + T(n-3) where n>2 with initial values: T(0) = 0, T(1) = 1, T(2) = 1
So the first few numbers in the sequence are 0,1,1,2,4,7,13,24,44,81,149...
But if instead we do this:
T(n) = T(n-1) + pT(n-2) - pT(n-3) where n>2 with initial values: T(0) = 0, T(1) = 1, T(2) = c where c and p are variables > 1
and then we take as the Output the modulus of T(n) with a variable m ie Output = T(n)%m where m>1, we get the following intriguing results upon plotting the Output for various p, c and m values:
1. A 'detailed' and a corresponding 'coarse'/'envelope resolution' waveform are obtained for c = r*(m)+2 and c = r*(m)+1 respectively, where r=1,2,3...
2. If both p and m are even, a 'detailed' and a corresponding 'coarse'/'envelope resolution' waveform are obtained for c = s*(m) and c = s*(m)+1 respectively, where s=0.5,1.5,2.5...
Examples:
Fo ther p=2, m=34 case, here is a zoomed in view for one period of the 'detailed' waveform at c=36 (blue), and corresponding 'coarse' waveform at c=35 (orange)
One period (period=16) of 'detailed' waveform: [4,6,10,14,22,30,12,28,26,24,20,16,8,0,18,2]
One period (period=16) of 'coarse' waveform: [3,3,7,7,15,15,31,31,29,29,25,25,17,17,1,1]
The coarse waveform is not in phase with the detailed one - the former leads the latter by one unit:
If we shift the coarse waveform back by one unit so that it is aligned with the detailed one, we can see better see how they track. The 'coarse' one does not pick up the sharp changes in the 'detailed' waveform but traces its envelope or 'trend':
For the sake of comparison, in yellow is an approximation of the waveform using the Haar wavelet transform -we see it does roughly 'outline' the sharp changes of the blue waveform unlike our orange one (despite the 'stepped' nature of them both) :
For p=2, the following code is enough to generate the sequence to 100 numbers (as a quad is large enough to hold the generated numbers):
Code: Select all
a.q = 0
b.q = 1
c.q=28 ;vary this
d.q
m=18 ;vary this
p = 2 ;vary this
For s = 0 To 100
d = -p*a + p*b + c
k = d%m
Scaledk.d = (9/m)*k ;this simply normalizes k to between 0 and 9 so that all the waveforms can be seen on the same scale, regardless of the value of m
Debug Scaledk
a = b
b = c
c = d
Next
SubtractNumbers: subtracts a a smaller from a larger positive number
MultiplyNumbers: multiplies a single digit positive number with a large positive number
Code: Select all
Procedure.s AddNumbers(str1.s, str2.s)
result.s = ""
carry.l = 0
addition.l
str1 = ReverseString(str1)
str2 = ReverseString(str2)
If Len(str1)=>Len(str2)
BigStr.s = str1
Else
BigStr = str2
EndIf
For t = 1 To Len(BigStr)
addition = Val(Mid(str1,t,1)) + Val(Mid(str2,t,1)) + carry
result = result + Str(addition%10)
carry = addition/10
Next
If carry>0
result = result + carry
EndIf
result = ReverseString(result)
ProcedureReturn result
EndProcedure
Procedure.l Modulus(num.s,a.l)
res = 0
For t = 1 To Len(num)
res = (res*10 + Val(Mid(num,t,1)))%a
Next
ProcedureReturn res
EndProcedure
Procedure.s SubractNumbers(str1.s, str2.s)
result.s = ""
carry.l = 0
addition.l
str1 = ReverseString(str1)
str2 = ReverseString(str2)
If Len(str1)=>Len(str2)
BigStr.s = str1
Else
BigStr = str2
EndIf
For t = 1 To Len(BigStr)
subtract = Val(Mid(str1,t,1)) - (Val(Mid(str2,t,1)) + carry)
If subtract<0
subtract = (Val(Mid(str1,t,1))+10) - (Val(Mid(str2,t,1)) + carry)
carry = 1
Else
carry = 0
EndIf
result = result + Str(subtract)
Next
result = ReverseString(result)
ProcedureReturn result
EndProcedure
Procedure.s MultiplyNumbers(str1.s, str2.s)
result.s = ""
carry.l = 0
multiplication.l
If Len(str1)=>Len(str2)
BigStr.s = str1
LittleStr.s = str2
Else
BigStr = str2
LittleStr = str1
EndIf
BigStr = ReverseString(BigStr)
For t = 1 To Len(BigStr)
multiplication = (Val(Mid(BigStr,t,1)) * Val(LittleStr)) + carry
If t<Len(BigStr)
result = result + Str(multiplication%10)
carry = multiplication/10
Else
result = result + ReverseString(Str(multiplication))
EndIf
Next
result = ReverseString(result)
ProcedureReturn result
EndProcedure
a.s = "0"
b.s = "1"
c.s = "39" ;vary this
d.s = "0"
m = 38 ;vary this
p.s = "3" ;vary this
For f = 0 To 100
;The below implements with strings, the following: d = -p*a + p*b + c
d = AddNumbers(SubractNumbers(MultiplyNumbers(p,b),MultiplyNumbers(p,a)),c)
k = Modulus(d,m)
Scaledk.d = (9/m)*k
Debug Scaledk
a = b
b = c
c = d
Next