Guest User

Untitled

a guest
Nov 20th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. 0 -> 0
  2. 1 -> 2
  3. 9 -> 6
  4. 85 -> 170
  5. 220 -> 236
  6. 1827 -> 2835
  7. 47525 -> 30298
  8.  
  9. b4d2UFḄ
  10.  
  11. b4d2UFḄ Main link. Argument: n (integer)
  12.  
  13. b4 Convert n to base 4.
  14. d2 Divmod each quaternary digit x by 2, yielding [x : 2, x % 2].
  15. U Upend; reverse each pair, yielding [x % 2, x : 2].
  16. F Flatten the 2D list of binary digits.
  17. Ḅ Convert from binary to integer.
  18.  
  19. n = o + 2*e
  20. n = ...hgfedcba
  21. o = ...0g0e0c0a
  22. 2*e = ...h0f0d0b0
  23.  
  24. s = 2*o + e
  25. s = ...ghefcdab
  26. 2*o = ...g0e0c0a0
  27. e = ...0h0f0d0b
  28.  
  29. s=2*(n-2*e)+e = 2*n-3*e
  30.  
  31. n/2 = ...hgfedcb
  32. M = ...1010101
  33. n/2 & M = ...h0f0d0b = e
  34.  
  35. ri4b4e!2=f=4b
  36.  
  37. ri e# Read input and convert to integer.
  38. 4b e# Get base-4 digits.
  39. 4e! e# Push all permutations of [0 1 2 3].
  40. 2= e# Select the third one which happens to be [0 2 1 3].
  41. f= e# For each base-4 digit select the value at that position in the previous
  42. e# list, which swaps 1s and 2s.
  43. 4b e# Convert back from base 4.
  44.  
  45. iXjQ4S2)4
  46.  
  47. jQ4 Convert the input (Q) to base 4.
  48. X S2) Translate [1, 2] to [2, 1].
  49. i 4 COnvert from base 4 to integer.
  50.  
  51. (n,m=0x55555555)=>n*2&~m|n/2&m
  52.  
  53. (n,m=0x55555555)=>(n*2&~m|n/2&m)>>>0
  54.  
  55. n=>parseInt(n.toString(4).replace(/1|2/g,n=>3-n),4)
  56.  
  57. n4÷axxn4÷xxe+3120
  58.  
  59. n4÷axxn4÷xxe+3120
  60. 0 a(0) = 0
  61. 2 a(1) = 2
  62. 1 a(2) = 1
  63. 3 a(3) = 3
  64.  
  65. n push n (input)
  66. 4÷ integer-divide by 4
  67. a a(n/4)
  68. xx double twice; multiply by 4
  69. now we have 4a(n/4)
  70. n push n (input)
  71. 4÷xx integer-divide by 4 and then multiply by 4
  72. since there is no modulo currently, n%4
  73. is built as n-(n/4*4)
  74. e we should have done a(n-(n/4*4)), but this
  75. is a shortcut for a(n-x) where x is the top
  76. of stack. Therefore, we now have a(n-n/4*4)
  77. which is a(n%4).
  78. + add.
  79.  
  80. BP2eP1ePXB
  81.  
  82. B % Take input implicitly. Convert to binary array
  83. P % Flip
  84. 2e % Convert to two-row 2D array, padding with a trailing zero if needed.
  85. % Because of the previous flip, this really corresponds to a leading zero
  86. P % Flip each column. This corresponds to swapping the bits
  87. 1e % Reshape into a row
  88. P % Flip, to undo the initial flipping
  89. XB % Convert from binary array to number. Display implicitly
  90.  
  91. 0000000000000040 <onemask_even>:
  92. 40: 89 f8 mov eax,edi
  93. 42: 25 55 55 55 55 and eax,0x55555555
  94. 47: 29 c7 sub edi,eax
  95. 49: d1 ef shr edi,1
  96. 4b: 8d 04 47 lea eax,[rdi+rax*2]
  97. 4e: c3 ret
  98. 4f: <end>
  99.  
  100. unsigned onemask_even(unsigned x) {
  101. unsigned emask = ~0U/3;
  102. unsigned e = (x & emask);
  103. return e*2 + ((x - e) >> 1);
  104. }
  105.  
  106. # since 0x55 has its low bit set, shifting it out the top of RAX will set CF
  107. 0000000000000000 <swap_bitpairs64>:
  108. 0: 31 c0 xor eax,eax ; old garbage in rax could end the loop early
  109. 0000000000000002 <swap_bitpairs64.loop>:
  110. 2: 48 c1 e0 08 shl rax,0x8
  111. 6: b0 55 mov al,0x55 ; set the low byte
  112. 8: 73 f8 jnc 2 <swap_bitpairs64.loop> ; loop until CF is set
  113. 000000000000000a <swap_bitpairs64.rest_of_function_as_normal>:
  114. # 10 bytes, same as mov rax, 0x5555555555555555
  115. # rax = 0x5555...
  116. a: 48 21 f8 and rax,rdi
  117. ...
  118.  
  119. <<<$[`tr 12 21<<<$[[#4]$1]`]
  120.  
  121. $1 input (first command line argument)
  122. $[ ] arithmetic expansion
  123. [#4] output in base 4
  124. <<< pass the result of this to...
  125. tr the `tr' command
  126. 12 21 and replace 1s with 2s, 2s with 1s
  127. ` ` evaluate the result...
  128. $[ ] in another arithmetic expansion, to convert back
  129. to base 10
  130. <<< output the result on STDOUT
  131.  
  132. f(x){return(x&~0U/3)*3+x>>1;} // 30 bit version, see below
  133.  
  134. // less golfed:
  135. f(x){return ((x & 0x55555555)*3 + x) >>1;} // >> is lower precedence than +
  136.  
  137. g(x){return 2*x-3*(x/2U&~0U/3);} // safe 32bit version, works for all x
  138.  
  139. n=>+('0b'+/(..)+$/.exec('0'+n.toString`2`)[0].split``.reduce((p,c)=>p.length-1?[p.join(c)]:[p[0],c],[''])[0],2)
  140.  
  141. n=>+('0b'+ //parse as binary literal
  142. /(..)+$/.exec('0'+n.toString`2`)[0] //convert to binary string with an even number of digits
  143. .split`` //convert to array
  144. .reduce((p,c)=>p.length-1?[p.join(c)]:[p[0],c],[''])
  145. //swap all numbers
  146. )
  147.  
  148. T`12`21
  149.  
  150. T`12`21
  151.  
  152. 4B12‡4ö
  153.  
  154. 4B - Take input and convert to base 4.
  155. 12Â - Push 12 bifurcated.
  156. ‡ - Transliterate [1, 2] to [2, 1].
  157. 4ö - Convert to base 10.
  158.  
  159. 4#.0 2 1 3{~4#.^:_1]
  160.  
  161. >> f =: 4#.0 2 1 3{~4#.^:_1]
  162. >> f 85
  163. << 170
  164.  
  165. to_base =: 4 #.^:_1 ]
  166. transpose =: 0 2 1 3 {~ to_base
  167. from_base =: 4 #. transpose
  168.  
  169. Fold[3#+##&,#~IntegerDigits~4/.{1->2,2->1}]&
  170.  
  171. 4@¡"21""12"(t4@¿
  172.  
  173. 4@¡"21""12"(t4@¿
  174. 4@¡ base 4 representation of n
  175. "21""12"(t translate (swap 1s and 2s)
  176. 4@¿ base 4 to decimal
  177.  
  178. ([:,_2|.,&0)&.(|.@#:)
  179.  
  180. f =: ([:,_2|.,&0)&.(|.@#:)
  181. (,.f"0) 0 1 9 85 220 1827 47525
  182. 0 0
  183. 1 2
  184. 9 6
  185. 85 170
  186. 220 236
  187. 1827 2835
  188. 47525 30298
  189.  
  190. ([:,_2|.,&0)&.(|.@#:) Input: n
  191. #: Get the value as a list of base 2 digits
  192. |.@ Reverse it
  193. ( )&. Apply to the list of base 2 digits
  194. ,&0 Append a zero to the end of the list
  195. _2 Split the list into nonoverlapping sublists of size 2
  196. |. Reverse each sublist
  197. [:, Flatten the list of sublists into a list
  198. &.( ) Apply the inverse of (reversed base 2 digits)
  199. to convert back to a number and return it
  200.  
  201. n=x2b(d2x(arg(1)))
  202. o=0
  203. do while n>''
  204. parse var n a+1 b+1 n
  205. o=o||b||a
  206. end
  207. say x2d(b2x(o))
Add Comment
Please, Sign In to add comment