Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,37 @@
# FRACTRAN interpreter implemented as an iterable struct
using .Iterators: filter, map, take
import Base: iterate, show
struct Fractran
rs::Vector{Rational{BigInt}}
i₀::BigInt
end
iterate(f::Fractran, i = f.i₀) =
for r in f.rs
if iszero(i % r.den) # faster than isinteger(i*r)
i = i ÷ r.den * r.num
return (i, i)
end
end
run(f::Fractran) = map(trailing_zeros, filter(ispow2, f))
show(io::IO, f::Fractran) = join(io, take(run(f), 30), ' ')
macro code_str(s)
eval(Meta.parse("[" * replace(s, "/" => "//") * "]"))
end
# Example FRACTRAN program generating primes
primes = Fractran(code"17/91, 78/85, 19/51, 23/38, 29/33, 77/29, 95/23,
77/19, 1/17, 11/13, 13/11, 15/14, 15/2, 55/1", 2)
# Output
println("First 25 iterations of FRACTRAN program 'primes':\n2 ",
join(take(primes, 25), ' '))
println("\nWatch the first 30 primes dropping out within seconds:")
primes