CODE_TOLD_FAST

JS/LOGBOW.JS

May 2nd, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //:TODO: Get more help from girlfriend for
  3. //:      mul2, mul3, mul4, mul5 functions.
  4.  
  5. //:To solve an algebra equation, you must always do the
  6. //:same thing to both sides of the equation to keep it
  7. //:balanced.
  8.  
  9. //:Solving equations involving a power of 2 are easy because ://
  10. //:it has a natural inverse function, the square root:       ://
  11. //:                                                          ://
  12. //:           6^2 = 36                                       ://
  13. //:     sqrt( 6^2 ) = sqrt( 36 )                             ://
  14. //:                                                          ://
  15. //:     sqrt cancels out power ( ^ )                         ://
  16. //:                                                          ://
  17. //:            6    = sqrt( 36 )                             ://
  18. //:                                                          ://
  19. //:     I find doing something similiar with log to be       ://
  20. //:     really hard to remember. So I created some inverse   ://
  21. //:     functions to help solve them.                        ://
  22. //:                                                          ://
  23. //:     EXAMPLE: The inverse of log2 is bow2.                ://
  24. //:                                                          ://
  25. //:     SO:                                                  ://
  26. //:     log2( 512 ) = 9                                      ://
  27. //:     bow2( log2( 512) ) = bow2( 9 )                       ://
  28. //:                                                          ://
  29. //:     bow2 cancels out log2 leaving 512                    ://
  30. //:                                                          ://
  31. //:     512 = bow2( 9 )                                      ://
  32. //:                                                          ://
  33. //:     bow2( 9 ) to source code instructions is: pow(2,9)   ://
  34. //:                                                          ://
  35. //:     512 = pow( 2 , 9 )                                   ://
  36. //:     512 = 2 ^ 9                                          ://
  37. //:                                                          ://
  38. //:----------------------------------------------------------://
  39.  
  40. //:Another thing I need to solve:                            ://
  41. //:   512 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2  === 1         ://
  42. //:         1   2   3   4   5   6   7   8   9                ://
  43. //:                                                          ://
  44. //:    Because: 2^9 === 512                                  ://
  45. //:                                                          ://
  46. //:    Dividing in a loop like this is stupid though.        ://
  47. //:    So how do we express:                                 ://
  48. //:    DivideBy_2_Ntimes( 512 , 9 ) === 1                    ://
  49. //:                                                          ://
  50. //:    512 / ( pow(2,9) ) === 1                              ://
  51. //:                                                          ://
  52. //:    In function call terms:                               ://
  53. //:                                                          ://
  54. //:    div2(512,9) === 1                                     ://
  55. //:                                                          ://
  56. //:    So... logically the inverse function of "div2"        ://
  57. //:    would be "mul2"                                       ://
  58. //:                                                          ://
  59. //:    mul2(1,9) === 512                                     ://
  60. //:                                                          ://
  61. //:    The source code?                                      ://
  62. //:    mul2(1,9) === ? ? ? ? ?                               ://
  63. //:                                                          ://
  64.  
  65.  
  66. //: pow: Power Function
  67. //: log: Natural logarithm function
  68. var pow = Math.pow; //:Shorthand function reference.
  69. var log = Math.log; //:Shorthand function reference.
  70.  
  71. //://////////////////////////////////////////////////////////://
  72. //:  mul2 and div2 are inverses of each other:               ://
  73. //:  mul2( div2( whatever ) ) === whatever                   ://
  74. //:  div2( mul2( whatever ) ) === whatever                   ://
  75. //://////////////////////////////////////////////////////////://
  76. //:                                                          ://
  77. //: Credit for Math Logic in div* and mul* functions:        ://
  78. //: Frankie Lampson                                          ://
  79. //:                                                          ://
  80. //://////////////////////////////////////////////////////////://
  81. //:B: Base
  82. //:N: N number of times.
  83. function div2( B, N ){ return(  B / (pow(2,N) ) ); }
  84. function div3( B, N ){ return(  B / (pow(3,N) ) ); }
  85. function div4( B, N ){ return(  B / (pow(4,N) ) ); }
  86. function div5( B, N ){ return(  B / (pow(5,N) ) ); }
  87.  
  88. //:B: Base
  89. //:N: N number of times.
  90. function mul2( B, N ){ return( 0 /** TODO:FIGURE_OUT **/ ); };
  91. function mul3( B, N ){ return( 0 /** TODO:FIGURE_OUT **/ ); };
  92. function mul4( B, N ){ return( 0 /** TODO:FIGURE_OUT **/ ); };
  93. function mul5( B, N ){ return( 0 /** TODO:FIGURE_OUT **/ ); };
  94. //://////////////////////////////////////////////////////////://
  95.  
  96.  
  97.  
  98. //:log functions with different bases:
  99. function log2( x ){ return( log(x) / log(2) ); };
  100. function log3( x ){ return( log(x) / log(3) ); };
  101. function log4( x ){ return( log(x) / log(4) ); };
  102. function log5( x ){ return( log(x) / log(5) ); };
  103.  
  104. //:corresponding inverse log functions:
  105. //:     "bow2" is the inverse of "log2"
  106. //:     "bow3" is the inverse of "log3"
  107. //:     "bow4" is the inverse of "log4"
  108. //:     "bow5" is the inverse of "log5"
  109. function bow2( y ){ return  pow(2,y); /**( 2^y )**/ };
  110. function bow3( y ){ return  pow(3,y); /**( 3^y )**/ };  
  111. function bow4( y ){ return  pow(4,y); /**( 4^y )**/ };  
  112. function bow5( y ){ return  pow(5,y); /**( 5^y )**/ };  
  113.  
  114.  
  115. //: SOLVE: log2( x ) = 7
  116. //:
  117. //:--------------------------------------------://
  118. //:
  119. //:  1:
  120. //:  We have an inverse of log2, it is bow2.
  121. //:  We can use bow2 to cancel out log2.
  122. //:  Apply bow2 to both sides:
  123. //:
  124. //:
  125. //:     bow2( log2( x ) ) = bow2( 7 )
  126. //:
  127. //:
  128. //:  2:
  129. //:     x is now isolated, because "bow2"
  130. //:     undid the effects of "log2"
  131. //:
  132. //:     x = bow2( 7 )
  133. //:
  134. //:
  135. //:  3:
  136. //:  Remove "bow2" token by substituting the
  137. //:  function call with the function body:
  138. //:
  139. //:     Function With Formal Parameter "y"
  140. //:
  141. //:         bow2( y ){ return pow(2,y); }
  142. //:
  143. //:     Function With ACTUAL Parameter "7"
  144. //:
  145. //:         bow2( 7 ){ return pow(2,7); }
  146. //:
  147. //:     THUS:
  148. //:         bow2( 7 ) === pow(2,7) === 2^7
  149. //:  
  150. //:  x=bow2( 7 )
  151. //:  x=2^7
  152. //:  x=128
  153. //:
  154. //:  4:
  155. //:  Check Work:
  156. //:  log2( 128 ) = 7
  157. //:
  158.  
  159. //:If our input is {y:7}
  160. //:We are saying:
  161. //:
  162. //:Solve For x: log2( x ) = 7
  163. function solve_for_x_log2_x_equals_y( arg ){
  164.  
  165.     y = arg.y; //:Extract argument from object.
  166.  
  167.     x = bow2( y );
  168.  
  169.     y_check = log2( x );
  170.  
  171.     if( y != y_check ){
  172.        
  173.         alert("[Checking_Work_Failed]");
  174.         throw("[Checking_Work_Failed]");
  175.    
  176.     };;
  177.  
  178.     return( x );
  179.  
  180. };;
Add Comment
Please, Sign In to add comment