Skip to content

Instantly share code, notes, and snippets.

# https://github.com/ruby/reline/pull/632
sizes=[]
CHUNK_LAST.zip(CHUNK_WIDTH).each{sizes << _2 while sizes.size <= [_1, 0x10ffff].min };
BITCHUNKS = sizes.each_slice(2048).map do |seg|
next seg.first if seg.uniq.size == 1
seg.each_slice(64).map do |sub|
next sub.first if sub.uniq.size == 1
s = sub.map { _1 == -1 ? 3 : _1 }.reverse.join
num = s.to_i(4)
@tompng
tompng / chudnovsky.rb
Last active April 5, 2024 04:26 — forked from fetburner/chudnovsky.rb
Chudnovskyの公式で円周率を計算するプログラム
def series(l, r)
if r - l <= 1
p = (2 * r - 1) * (6 * r - 5) * (6 * r - 1)
q = r * r * r * 10939058860032000
a = (r % 2 == 0 ? 1 : -1) * (13591409 + 545140134 * r)
[p, q, a * p]
else
m = (l + r) / 2
p0, q0, t0 = series(l, m)
@tompng
tompng / get_cchars.rb
Last active April 3, 2024 08:55
cchars memo for reline
MAYBE_TIOCGETC = [
0x40067412, # M2 Mac
2, # docker rubyfarm? something is wrong
# unknown, found in github search
('T'.ord << 8) | 2,
('t'.ord << 8) | 18
]
cchars = "\0" * 256
STDIN.ioctl(TIOCGETC, cchars)
@tompng
tompng / fibo.rb
Last active March 29, 2024 15:00
recursive fibonacci with complex
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]
⠁⠠⠐⢀⢀⠄⠁⠄⠐⠂⠁⢀⠁⢀⠈⢀⢀⠈
⠠⠈⠈⠄⠁⡁⡁⠃⢅⡤⠴⡔⢉⢄⢁⡄⠙⡐⢃⠬⠎⢁⡠⠢⡂⠌⠈⢀⢀⠈
⠁⠠⠐⡈⠆⢁⢅⢅⡰⣐⠔⡖⠍⢑⢕⢩⡲⠃⠌⡆⡊⠴⣔⠔⣐⠢⡜⡜⠠⠏⢀⠁⠆⠢⠊⠁⠐⡀
⠐⢀⡀⠰⠦⠉⢆⠖⠞⢸⠅⢒⢑⠺⡜⢴⣖⡞⢈⠏⡯⣚⠰⠃⣄⡻⢮⢞⣖⠿⢈⢸⡏⡨⠾⡬⠒⢐⡅⢠⠃⢁⡂⠑⠁⠐
⠐⢀⠤⠨⢁⠏⠛⠤⠏⠸⡂⠝;BEGIN{->s{eval((a="⡉⢐⡊⡕⢹⢨⡃⢚⣀⢃⠂⠠⠂⠜⠩⠄⠄⠠
⠄⢀⠑⠋⠅⠫⠄⠒⠏⡪⡕";s.gsub(/[^!-~]/,'')))}.[]"⠩⡖⡶⠹⢳⠄⢪⣇⣌⡪⡐⠃⠕⠜⠒⠈⠈
⠈⠂⠂⠊⢅⡍⡄⡗⣌⡴⠁⡒$s=[⡙⠭⣐⢚];⢧⡸de⢴f⡨(⠮⢵s⠷e⢗⠙⠏lf).⠖⢀⢤⣾⢱⢍⡰⠆⡔⠏⢑⠣⢈⡃⢀⡂⠐⡀
⠁⠁⠆⡢⢈⡡⠔⠛⡪⡲⢂⢊⣏met⣄⠂⣰hod⢭⠱_⢳mi⣷ssin⡫g⡓(a⠴⢸⡲)=$⠝⡎⣓⢵⣏⡢⢼⡐⢸⣋⣁⡰⠮⢤⡨⠤⡀⠂
⠄⠠⣀⡒⡋⠣⢌⠑⣇⡖⠧⣙⡙⣻⡌s<<⡾⡾a⡳.⢸to⢯_⡌s;⢟⣣⡾at_⠞e⡡x⠷⢒it{⢆⡝⣆⢛⠋⢮⡻⡖⡵⣍⡤⢑⡢⣡⠂⠭⠡⠙⠂⢀
⠠⠂⢂⢐⢀⡒⡋⡋⡱⣢⠋⡂⣕⢋⢪⠳⢁a,b⣃⢾=⣯⡆a.sp⡼l⡣it;⢼⡌c,*⣺⠰⢇d,e⢦⠧⡏⣪⣃⢯⢗⠶⣠⠦⠝⡲⠩⣃⢶⡮⠧⠱⡨⠒⠂⠈
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
@tompng
tompng / wave.rb
Last active May 20, 2024 08:41
wave_quine
t=( s=" wav e=- >c{ eva l(c.split(
$/) .map{ (_1 *2)[/ #.+ #/] }.j
oin .de let e(' #'+ 32. chr ))} ;class<<wa
ve;de f+(c) =call(c); end;w ave
"). tr" #$/ ",+ ''; eval(t)+%~
s . s p l i t(Re g e x p . n e # s p =3 2. c h r ; q =
) . z i p ( [ 0,4,4 , 5 , 1 , 1, 3# w ( s p + ' + ')
3, 2 , 2 , 1 ,1, 1 , 4 , 1 , 4 ,1 , 3 , # , 3 , 4 , 4 ,
* _ 1 }) . join # 3 , 3 , 4 , 4 , 4 ,4 ] . m a p { s p
@tompng
tompng / capture.html
Last active May 20, 2024 08:41
rk wasm signage
<link href="https://fonts.googleapis.com/css2?family=Dela+Gothic+One&family=Montserrat:wght@400;500;600;700;800&family=Poppins:wght@400;800;900&display=swap" rel="stylesheet">
<style>canvas{background:white;width:100vw;}body{background:gray;margin:0;}</style>
<script src="/dist/browser.script.iife.js"></script>
<script type="text/ruby">
Time.singleton_class.prepend Module.new{
def now
$called = true
$time ||= 0.0
end
}
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
@tompng
tompng / integer_dup.rb
Last active February 28, 2024 04:54
baseの違うbignumの比較
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