This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibo_c(n) | |
return 1 if n == 0 | |
a=fibo_c(n/2)*b=1i**n[0] | |
b.*a.abs2+(a.*2+1i).real*a.imag.i | |
end | |
10.times.map{fibo_c(_1).real} | |
# => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Integer | |
def self.isqrt(n) | |
return 0 if n == 0 | |
return 1 if n < 4 | |
shift = [n.bit_length / 4, 1].max | |
x = Integer.isqrt(n >> (shift * 2)) | |
x = (x << (shift - 1)) + (n >> (shift + 1)) / x | |
xx = x * x | |
while xx > n | |
xx -= 2 * x - 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def big_divmod(numerator, denominator) | |
numerator_bits = numerator.bit_length | |
denominator_bits = denominator.bit_length | |
inv = 1 | |
accuracy_bits = 1 | |
target_bits = [numerator_bits - denominator_bits] | |
while target_bits[0] > 1 | |
target_bits.unshift (target_bits[0] * 4 + 1)/ 7 | |
end | |
accuracy_bits = target_bits.shift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def string_radix(s) | |
if s.match?(/0x/i) | |
[16, 2] | |
elsif s.match?(/0o/i) | |
[8, 2] | |
elsif s.match?(/0b/i) | |
[2, 2] | |
elsif s.start_with?('0') | |
[8, 0] | |
else |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sum_reg(a, b, fallback = nil) | |
fragments = (0..18).map do |sum| | |
".*\\k<#{a}>.{#{sum/2+5}}_.{#{sum-sum/2+5}}\\k<#{b}>.*-.{#{sum%10}}" | |
end | |
fragments << ".*-.{#{fallback}}" if fallback | |
"(?:#{fragments.join('|')})" | |
end | |
S = '9876543210___________0123456789-0123456789' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pty' | |
require 'io/console' | |
if ARGV.empty? | |
puts <<~EOS | |
Reline Visualizer (Minimal terminal emulator that only supports Reline's escape sequences) | |
Usage: ruby #{__FILE__} <command>' | |
ruby #{__FILE__} irb | |
ruby #{__FILE__} bash | |
ruby #{__FILE__} zsh | |
ruby #{__FILE__} ruby -I path/to/reline/lib -I path/to/irb/lib path/to/irb/exe/irb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class << ($colored_stdout = STDOUT.dup) | |
# Flip this if you are using bright theme | |
BACKGROUND = [0, 0, 0] | |
FOREGROUND = [255, 255, 255] | |
RANDOMIZE_RANGE = -32..32 | |
def rand_color(rgb, bg: false) | |
"#{bg ? 48 : 38};2;#{rgb.map { (_1 + rand(RANDOMIZE_RANGE)).clamp(0, 0xff) }.join(';')}" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TemrinationChecker | |
def initialize | |
@prelaunched = nil | |
@code = '' | |
@current = new_session | |
end | |
def new_session | |
IO.popen(['ruby', '-c'], 'r+', err: [:child, :out]) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
raise 'COLORTERM required' unless ENV['COLORTERM'] | |
# Color table from https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit | |
env_names = %w[VGA WinXP PowerShell6 VSCode Win10 Terminal.app PuTTY mIRC xterm Ubuntu Eclipse] | |
env_colors = <<COLORS.lines.map{|l|l.split(' ').map{_1.scan(/\d+/).join(';')}}.transpose | |
0,0,0 0,0,0 0,0,0 12,12,12 0,0,0 0,0,0 0,0,0 0,0,0 1,1,1 0,0,0 | |
170,0,0 128,0,0 205,49,49 197,15,31 153,0,0 187,0,0 127,0,0 205,0,0 222,56,43 205,0,0 | |
0,170,0 0,128,0 13,188,121 19,161,14 0,166,0 0,187,0 0,147,0 0,205,0 57,181,74 0,205,0 | |
170,85,0 128,128,0 229,229,16 193,156,0 153,153,0 187,187,0 252,127,0 205,205,0 255,199,6 205,205,0 | |
0,0,170 0,0,128 36,114,200 0,55,218 0,0,178 0,0,187 0,0,127 0,0,238 0,111,184 0,0,238 |