Advertisement
rockdrilla

'Twice Linear' kata thoughts

Mar 16th, 2017
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.98 KB | None | 0 0
  1. # https://www.codewars.com/kata/twice-linear/
  2.  
  3. def dbl_brute(n, x = 1)
  4.   return [] unless n.positive?
  5.   n -= 1
  6.   r = [x] + dbl_brute(n, 2*x+1) + dbl_brute(n, 3*x+1)
  7.  
  8.   r.sort! if (x == 1)
  9.   return r
  10. end
  11.  
  12. def ary2cmp(a, b)
  13.   s = -1
  14.   (0..[a.length, b.length].min-1).each{|i| s = i; break unless a[i] == b[i]; }
  15. end
  16.  
  17. a = [1]
  18. k = []
  19.  
  20. (1..26).each do |i|
  21.   x = dbl_brute(i)
  22.   k[0] = x.length
  23.  
  24.   x.sort!
  25.   x.uniq!
  26.   k[1] = x.length
  27.  
  28.   k[2] = k[0] - k[1]
  29.   k[3] = k[2]*100.0 / k[0]
  30.  
  31.   k[4] = ary2cmp(a, x)
  32.   k[5] = k[4]*100.0 / k[1]
  33.  
  34.   k[6] = x[k[4] - 1]
  35.  
  36.   a = x
  37.  
  38.   puts "%2d | %10d | %10d || %7d | %3.3f %% || %7d | %7.3f %% | % 12d" % [i, k].flatten
  39. end
  40.  
  41. #  1 |          1 |          1 ||       0 | 0.000 % ||       1 | 100.000 % |            1
  42. #  2 |          3 |          3 ||       0 | 0.000 % ||       1 |  33.333 % |            1
  43. #  3 |          7 |          7 ||       0 | 0.000 % ||       3 |  42.857 % |            4
  44. #  4 |         15 |         15 ||       0 | 0.000 % ||       7 |  46.667 % |           13
  45. #  5 |         31 |         30 ||       1 | 3.226 % ||      14 |  46.667 % |           31
  46. #  6 |         63 |         60 ||       3 | 4.762 % ||      25 |  41.667 % |           67
  47. #  7 |        127 |        119 ||       8 | 6.299 % ||      44 |  36.975 % |          139
  48. #  8 |        255 |        237 ||      18 | 7.059 % ||      76 |  32.068 % |          283
  49. #  9 |        511 |        472 ||      39 | 7.632 % ||     132 |  27.966 % |          607
  50. # 10 |       1023 |        940 ||      83 | 8.113 % ||     230 |  24.468 % |         1255
  51. # 11 |       2047 |       1874 ||     173 | 8.451 % ||     401 |  21.398 % |         2551
  52. # 12 |       4095 |       3740 ||     355 | 8.669 % ||     697 |  18.636 % |         5116
  53. # 13 |       8191 |       7469 ||     722 | 8.815 % ||    1204 |  16.120 % |        10237
  54. # 14 |      16383 |      14918 ||    1465 | 8.942 % ||    2258 |  15.136 % |        24043
  55. # 15 |      32767 |      29805 ||    2962 | 9.040 % ||    3891 |  13.055 % |        48091
  56. # 16 |      65535 |      59561 ||    5974 | 9.116 % ||    6726 |  11.293 % |        96187
  57. # 17 |     131071 |     119043 ||   12028 | 9.177 % ||   11645 |   9.782 % |       192379
  58. # 18 |     262143 |     237966 ||   24177 | 9.223 % ||   20186 |   8.483 % |       384763
  59. # 19 |     524287 |     475750 ||   48537 | 9.258 % ||   34995 |   7.356 % |       769531
  60. # 20 |    1048575 |     951213 ||   97362 | 9.285 % ||   60635 |   6.374 % |      1539067
  61. # 21 |    2097151 |    1901981 ||  195170 | 9.306 % ||  104947 |   5.518 % |      3078202
  62. # 22 |    4194303 |    3803271 ||  391032 | 9.323 % ||  181386 |   4.769 % |      6157498
  63. # 23 |    8388607 |    7605468 ||  783139 | 9.336 % ||  313014 |   4.116 % |     12316090
  64. # 24 |   16777215 |   15209251 || 1567964 | 9.346 % ||  539412 |   3.547 % |     24633274
  65. # 25 |   33554431 |   30415896 || 3138535 | 9.354 % ||  928548 |   3.053 % |     49267642
  66. # 26 |   67108863 |   60827703 || 6281160 | 9.360 % || 1597325 |   2.626 % |     98536378
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement