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