Guest User

Untitled

a guest
Jan 23rd, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. 0: 0 : 1 bit
  2. 1: 1 : 1 bit
  3. 2: 10 : 2 bits
  4. 3: 11 : 2 bits
  5. 4: 100 : 3 bits
  6. 5: 101 : 3 bits
  7.  
  8. 1 => 1
  9. 2 => 1
  10. 3 => 1.3333333
  11. 4 => 1.5
  12. 5 => 1.8
  13. 6 => 2
  14. 7 => 2.1428571
  15.  
  16. .Oml.B
  17.  
  18. .Oml.BdUQ Filling in implict vars
  19.  
  20. .O Average of list
  21. m UQ Map over [0..input)
  22. l Length of
  23. .B Binary string representation of int
  24. d Lambda var
  25.  
  26. R’BFL÷
  27.  
  28. R’BFL÷ Main monadic chain. Argument: n
  29.  
  30. R yield [1, 2, ..., n]
  31. ’ decrement; yield [0, 1, ..., n-1]
  32. B convert to binary; yield [[0], [1], [1,0], [1,1], ...]
  33. F flatten list; yield [0, 1, 1, 0, 1, 1, ...]
  34. L length of list
  35. ÷ divide [by n]
  36.  
  37. @(n)1+sum(fix(log2(1:n-1)))/n
  38.  
  39. log2(1:n-1) % log2 of numbers in range [1..n-1]
  40. % why no 0? because log2(0) = -Inf :/
  41. fix( ) % floor (more or less, for positive numbers)
  42. sum( ) % sum... wait, didn't we miss a +1 somewhere?
  43. % and what about that missing 0?
  44. /n % divide by n for the mean
  45. 1+ % and add (1/n) for each of the n bit lengths
  46. % (including 0!)
  47.  
  48. n->mean(ceil(log2([2;2:n])))
  49.  
  50. q:ZlksG/Q
  51.  
  52. % Implicitly grab input (N)
  53. q: % Create array from 1:N-1
  54. Zl % Compute log2 for each element of the array
  55. k % Round down to the nearest integer
  56. s % Sum all values in the array
  57. G % Explicitly grab input again
  58. / % Divide by the input
  59. Q % Add 1 to account for 0 in [0, ... N - 1]
  60. % Implicitly display the result
  61.  
  62. :qBYszQG/
  63.  
  64. :qBYszQG/
  65. : % take vector [1..n]
  66. q % decrement by 1 to get [0..n-1]
  67. B % convert from decimal to binary
  68. Ys % cumulative sum (fills in 0's after first 1)
  69. z % number of nonzero elements
  70. Q % increment by 1 to account for zero
  71. G % paste original input (n)
  72. / % divide for the mean
  73.  
  74. Rl2Ċ»1S÷
  75.  
  76. R 1 to n
  77. l2 log2
  78. Ċ ceiling
  79. »1 max of 1 and...
  80. S sum
  81. ÷ divided by n
  82.  
  83. BL©2*2_÷+®
  84.  
  85. æḟ2’Ḥ÷_BL$N
  86.  
  87. float a(int n){int i=0,t=0;for(;i<n;)t+=Integer.toString(i++,2).length();return t/(n+0f);}
  88.  
  89. lambda x:sum(len(bin(i))-2for i in range(x))/x
  90.  
  91. f = lambda x: sum(len(bin(i))-2for i in range(x))/x
  92. print(f(6))
  93. # 2.0
  94.  
  95. L<bJg¹/
  96.  
  97. L< # range from 0..input-1
  98. b # convert numbers to binary
  99. J # join list of binary numbers into a string
  100. g # get length of string (number of bits)
  101. ¹/ # divide by input
  102.  
  103. double f(int n){return Enumerable.Range(0,n).Average(i=>Convert.ToString(i,2).Length);}
  104.  
  105. +/@:%~#@#:"0@i.
  106.  
  107. range =: i.
  108. length =: #
  109. binary =: #:
  110. sum =: +/
  111. divide =: %
  112. itself =: ~
  113. of =: @
  114. ofall =: @:
  115. binarylength =: length of binary "0
  116. average =: sum ofall divide itself
  117. f =: average binarylength of range
  118.  
  119. (+/1⌈(⌈2⍟⍳))÷⊢
  120.  
  121. range ← ⍳
  122. log ← ⍟
  123. log2 ← 2 log range
  124. ceil ← ⌈
  125. bits ← ceil log2
  126. max ← ⌈
  127. fix0 ← 1 max bits
  128. sum ← +/
  129. total ← sum fix0
  130. self ← ⊢
  131. div ← ÷
  132. mean ← sum div self
  133.  
  134. n$z1z[i1+2l$M$Y+]kz$:N.
  135.  
  136. n$z Take number from input and store it in register (n)
  137. 1 Push 1 onto the stack
  138. z[ For loop that repeats n times
  139. i1+ Loop counter + 1
  140. 2l$M log_2
  141. $Y Ceiling
  142. + Add top two elements of stack
  143. ] Close for loop
  144. z$: Float divide by n
  145. N. Output as number and stop.
  146.  
  147. n=>eval(`for(o=0,p=n;n--;o+=n.toString(2).length/p);o`)
  148.  
  149. n => // anonymous function w/ arg `n`
  150. for( // loop
  151. o=0, // initalize bit counter to zero
  152. p=n // copy the input
  153. ;n-- // will decrease input every iteration, will decrease until it's zero
  154. ;o+= // add to the bitcounter
  155. n.toString(2) // the binary representation of the current itearations's
  156. .length // length
  157. /p // divided by input copy (to avergage)
  158. );o // return o variable
  159.  
  160. |=
  161. r/@
  162. (^div (sun (roll (turn (gulf 0 (dec r)) xeb) add)) (sun r)):.^rq
  163.  
  164. > %. 7
  165. |=
  166. r/@
  167. (^div (sun (roll (turn (gulf 0 (dec r)) xeb) add)) (sun r)):.^rq
  168. .~~~2.1428571428571428571428571428571428
  169.  
  170. n=>(l=-~Math.log2(n))-(2**l-2)/n
  171.  
  172. n=>(l=-~Math.log2(n))-((1<<l)-2)/n
  173.  
  174. 11111111111111111111111
  175. 111111111111111100000000000000001111111
  176. 11111111000000001111111100000000111111110000000
  177. 111100001111000011110000111100001111000011110000111
  178. 11001100110011001100110011001100110011001100110011001
  179. 0101010101010101010101010101010101010101010101010101010
  180.  
  181. ;r♂├Σl/
  182.  
  183. ;r♂├Σl/
  184. ; duplicate input
  185. r push range(0, n) ([0, n-1])
  186. ♂├ map binary representation
  187. Σ sum (concatenate strings)
  188. l/ divide length of string (total # of bits) by n
  189.  
  190. r♂├♂læ
  191.  
  192. 0vVV1HV1-[2L_1+v+v]v1+V/N
  193.  
  194. param($n)0..($n-1)|%{$o+=[convert]::ToString($_,2).Length};$o/$n
  195.  
  196. for(1..<>){$u+=length sprintf"%b",$_;$n++}$u/=$n;say$u
  197.  
  198. rd_,2fbs,/
  199.  
  200. rd e# read input and convert to double
  201. _ e# duplicate
  202. , e# range from 0 to input minus 1
  203. 2fb e# convert each element of the array to binary
  204. s e# convert to string. This flattens the array
  205. , e# length of array
  206. e# swap
  207. / e# divide
  208.  
  209. /uΜr0xdlBH
  210.  
  211. /uΜr0xdlBH
  212. Μr0x map range 0..x
  213. dlBH over lengths of binary elements
  214. /u divide sum of this
  215. by implicit input (x)
  216.  
  217. func f(n:Double)->Double{return n<1 ?1:f(n-1)+1+floor(log2(n))}
  218. f(N-1)/N
  219.  
  220. %~[:+/#@#:"0@i.
  221.  
  222. f =: %~[:+/#@#:"0@i.
  223. f 7
  224. 2.14286
  225.  
  226. %~[:+/#@#:"0@i. Input is y
  227. i. Range from 0 to y-1.
  228. "0@ For each number in this range:
  229. #@ Compute the length of
  230. #: its base-2 representation.
  231. [:+/ Take the sum of the lengths, and
  232. %~ divide by y.
  233.  
  234. (fn [n]
  235. (->
  236. (->>
  237. (range n) ;;Get numbers from 0 to N
  238. (map #(.bitLength (bigint %))) ;;Cast numbers to BigInt so bitLength can be used
  239. (apply +) ;;Sum the results of the mapping
  240. (inc)) ;;Increment by 1 since bitLength of 0 is 0
  241. (/ n))) ;;Divide the sum by N
Add Comment
Please, Sign In to add comment