Skip to content

Instantly share code, notes, and snippets.

View youz's full-sized avatar
🈳
kyomu

Yōsuke Ushiki youz

🈳
kyomu
View GitHub Profile
@youz
youz / dc.svg
Last active January 9, 2024 04:55
drawing dragon curve in SQL
View dc.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@youz
youz / dragon-curve.lisp
Last active January 2, 2024 00:46
dragon curve ascii art
View dragon-curve.lisp
(defun f1 (z) (* z #C(1/2 1/2)))
(defun f2 (z) (- 1 (* z #C(1/2 -1/2))))
(defun c2p (z) (list (realpart z) (imagpart z)))
(defun gen (n)
(let ((b (expt 2 (ash (1+ n) -1))))
(loop
for i from 0 to n
for s = (list (list 0 1))
then (mapcan (lambda (l) (list (mapcar #'f1 l) (mapcar #'f2 l))) s)
@youz
youz / solve_m23.lisp
Last active June 24, 2023 02:54
EQUALINE Mission 23 solver (in common lisp)
View solve_m23.lisp
;;; EQUALINE Mission 23 solver
(defstruct kifu
(boards (list #(1 :+ 1 :+ 1 :+ 1 :+ 1))) ; 盤面のリスト (逆順)
(routes nil) ; 指し手(int list)のリスト (逆順)
)
(defun print-kifu (k)
(loop for b in (reverse (kifu-boards k))
for r in (cons nil (reverse (kifu-routes k)))
@youz
youz / pi.atl
Last active January 10, 2023 15:53
Atlas https://github.com/darrenks/atlas で円周率1000桁計算
View pi.atl
# ref. https://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/spigot.pdf
i=2:1+i
q=1:10*q*i*~1-2*i
r=180:10*u*(q*~2-5*i)+r-y*t
t=60:t*u
u=6+i*27+i*27
y=((q*~12-27*i)+5*r)/5*t
c='0+1001[y
([c):'.:>c
@youz
youz / cookpad-puzzles-2022.rb
Last active September 13, 2022 03:25
Cookpad Code Puzzle for RubyKaigi 2022 solutions
View cookpad-puzzles-2022.rb
# https://ruby-puzzles-2022.cookpad.tech/
def answer1(n)
n+1
end
def answer2(str)
@youz
youz / Makefile
Created September 11, 2022 13:14
libm, openlibmのpowとmpfr_powを比較
View Makefile
CC = gcc
CFLAGS = -Wall -Wextra -std=gnu11 -O0
LIBS = -lmpfr -lgmp
SRC = pow.c
LIBMBIN = pow_m
OPENLIBMBIN = pow_o
all: $(LIBMBIN) $(OPENLIBMBIN)
$(LIBMBIN): $(SRC)
View verify.l
;;; https://github.com/TokusiN/AtanMagic/blob/main/data.txt を保存して開いておく
(labels ((read-data (buf)
(with-input-from-buffer (buf)
(loop for i to 15 collect
(mapcar #'read-from-string (split-string (read-line) #\,)))))
(frac2comp (f)
(complex (denominator f) (numerator f)))
(verify1 (desc row)
; 各分数を複素数に変換(p/q → q+pi)し、総乗の虚数部=0 かつ 偏角の総和≒2πならokとする
@youz
youz / spigot_e.sql
Created June 1, 2022 14:35
Spigot algorithm in SQL (for duckdb)
View spigot_e.sql
drop table if exists consts;
drop table if exists digits;
create table consts as select 10000 ndigits;
create table digits(n int primary key, d int);
.timer on
insert into digits(n, d)
with recursive init(n, num, den) as (
@youz
youz / pi_pg.sql
Last active May 29, 2022 11:30
Computing the digits of π in PostgreSQL
View pi_pg.sql
-- ref https://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/spigot.pdf
with recursive pigen(q, r, t, i, y, n) as (
select 1::decimal, 180::decimal, 60::decimal, 2, 0::decimal, 0
union all select 10*q*i*(2*i-1), 10*u*(q*(5*i-2)+r-y*t), t*u, i+1, y, n+1
from (select q, r, t, i, n, 3*(3*i+1)*(3*i+2) as u, div(q*(27*i-12)+5*r,(5*t)) as y from pigen) as tmp
where n < 1000
)
select string_agg(y::text, '') pi
from pigen where n > 0;
@youz
youz / pi_spigot.sql
Created May 29, 2022 04:57
Computing the digits of π in SQLite3
View pi_spigot.sql
-- ref. https://xn--w6q13e505b.jp/program/spigot.html
create table consts as
select 1000 ndigits;
.timer on
with a(i) as (
select ndigits/4*14 from consts
union all select i-1 from a where i>0