Fractran: Difference between revisions

Content added Content deleted
Line 2,718: Line 2,718:


using .Iterators: filter, map, take
using .Iterators: filter, map, take
import Base: iterate, show


struct Fractran
struct Fractran
rs::Vector{Rational{BigInt}}
rs::Vector{Rational{BigInt}}
i₀::BigInt
i₀::BigInt
limit::Int
end
end


iterate(f::Fractran, i = f.i₀) =
Base.iterate(f::Fractran, i = f.i₀) =
for r in f.rs
for r in f.rs
if iszero(i % r.den)
if iszero(i % r.den)
Line 2,733: Line 2,733:
end
end


run(f::Fractran) = map(trailing_zeros, filter(ispow2, f))
interpret(f::Fractran) =
take(
map(trailing_zeros,
filter(ispow2, f))
f.limit)


show(io::IO, f::Fractran) = join(io, take(run(f), 30), ' ')
Base.show(io::IO, f::Fractran) =
join(io, interpret(f), ' ')


macro code_str(s)
macro code_str(s)
eval(Meta.parse("[" * replace(s, "/" => "//") * "]"))
[eval(Meta.parse(replace(t, "/" => "//"))) for t ∈ split(s)]
end
end


primes = Fractran(code"17/91, 78/85, 19/51, 23/38, 29/33, 77/29, 95/23,
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)
77/19 1/17 11/13 13/11 15/14 15/2 55/1", 2, 30)


# Output
# Output