The natural logarithm of 3 is not a particularly important mathematical constant, but there is a formula for it that allows it to be computed with a spigot algorithm (this formula can be derived from the Taylor series for ln(1+x)):
Code:
1+x x^3 x^5 x^7
ln(---) = 2 * (x + --- + --- + --- + ...)
1-x 3 5 7
Plugging in x = 1/2, we get:
Code:
2 2 2 2
ln(3) = - + ------- + ------- + ------- + ...
2 3 * 2^3 5 * 2^5 7 * 2^7
We can write this in "spigot" form (for lack of a better phrase), as follows (note that the 3,5,... in the numerators cancel out the 3,5,... from the previous denominator):
Code:
1 3 5
ln(3) = 1 + --- (1 + --- (1 + --- (1 + ...
3*4 5*4 7*4
Here is the ln(3) spigot program (actually, it outputs 1. and then the spigot is only on the fractional part), similar to the pi spigot, but of course with different terms (as usual, update OUTPUT for your system to output the character in A):
Code:
{ 8000 } ORG $8000
{ 8000 } :LN3
{ 8000 D8 } CLD
{ 8001 20 61 80 } JSR =.I
{ 8004 A9 31 } LDA # $31
{ 8006 20 BD 80 } JSR =OUTPUT
{ 8009 A9 2E } LDA # $2E
{ 800B 20 BD 80 } JSR =OUTPUT
{ 800E A2 14 } LDX # 21 1 -
{ 8010 8A } .1 TXA
{ 8011 48 } PHA
{ 8012 A9 00 } LDA # 0
{ 8014 8D BB 80 } STA =Q
{ 8017 A2 1F } LDX # 31
{ 8019 8A } .2 TXA
{ 801A 38 } SEC
{ 801B 2A } ROL A
{ 801C 20 A3 80 } JSR =MUL
{ 801F 48 } PHA
{ 8020 AD BB 80 } LDA =Q
{ 8023 48 } PHA
{ 8024 A9 0A } LDA # 10
{ 8026 8D BB 80 } STA =Q
{ 8029 BD 6B 80 } LDA =N 1 - ,X
{ 802C 20 A3 80 } JSR =MUL
{ 802F 8D BC 80 } STA =R
{ 8032 68 } PLA
{ 8033 6D BB 80 } ADC =Q
{ 8036 8D BB 80 } STA =Q
{ 8039 68 } PLA
{ 803A 6D BC 80 } ADC =R
{ 803D 8E BC 80 } STX =R
{ 8040 38 } SEC
{ 8041 2E BC 80 } ROL =R
{ 8044 0E BC 80 } ASL =R
{ 8047 0E BC 80 } ASL =R
{ 804A 20 8B 80 } JSR =DIV
{ 804D 9D 6B 80 } STA =N 1 - ,X
{ 8050 CA } DEX
{ 8051 D0 C6 } BNE =.2
{ 8053 AD BB 80 } LDA =Q
{ 8056 49 30 } EOR # $30
{ 8058 20 BD 80 } JSR =OUTPUT
{ 805B 68 } PLA
{ 805C AA } TAX
{ 805D CA } DEX
{ 805E D0 B0 } BNE =.1
{ 8060 60 } RTS
{ 8061 A2 1E } .I LDX # 30
{ 8063 A9 01 } LDA # 1
{ 8065 9D 6C 80 } .J STA =N ,X
{ 8068 CA } DEX
{ 8069 10 FA } BPL =.J
{ 806B 60 } RTS
{ 806C } :N DS 31
{ 808B } :DIV ; 256*a+q / r -> t = b * q + a
{ 808B A0 08 } LDY # 8
{ 808D 0E BB 80 } ASL =Q
{ 8090 2A } .1 ROL A
{ 8091 B0 05 } BCS =.2
{ 8093 CD BC 80 } CMP =R
{ 8096 90 04 } BCC =.3
{ 8098 ED BC 80 } .2 SBC =R
{ 809B 38 } SEC
{ 809C 2E BB 80 } .3 ROL =Q
{ 809F 88 } DEY
{ 80A0 D0 EE } BNE =.1
{ 80A2 60 } RTS
{ 80A3 } :MUL ; a * q -> 256*a+q
{ 80A3 8D BC 80 } STA =R
{ 80A6 A9 00 } LDA # 0
{ 80A8 A0 08 } LDY # 8
{ 80AA 4E BB 80 } LSR =Q
{ 80AD 90 04 } .1 BCC =.2
{ 80AF 18 } CLC
{ 80B0 6D BC 80 } ADC =R
{ 80B3 6A } .2 ROR A
{ 80B4 6E BB 80 } ROR =Q
{ 80B7 88 } DEY
{ 80B8 D0 F3 } BNE =.1
{ 80BA 60 } RTS
{ 80BB } :Q DS 1
{ 80BC } :R DS 1
{ 80BD } :OUTPUT
{ 80BD 49 80 } EOR # $80
{ 80BF 4C ED FD } .A JMP $FDED