Fractran: Difference between revisions

Content added Content deleted
Line 2,554: Line 2,554:


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|1.6}}<syntaxhighlight lang="julia">struct Fractran input::BigInt; fracs::Vector{Rational} end
{{works with|Julia|1.9}}<syntaxhighlight lang="julia">
# FRACTRAN interpreter implemented in Julia as an iterable struct

struct Fractran
input::BigInt
fracs::Vector{Rational{BigInt}}
n::Int
end


function Base.iterate(ft::Fractran, i = ft.input)
function Base.iterate(ft::Fractran, i = ft.input)
Line 2,563: Line 2,570:
end
end


eval(ft::Fractran) = (Int(log2(i)) for i in ft if ispow2(i))
function eval(ft::Fractran)
(Int(log2(i)) for i ∈ ft if ispow2(i))
end


Base.show(io::IO, ft::Fractran) = join(io, Iterators.take(eval(ft), 25 ), ' ')
function Base.show(io::IO, ft::Fractran)
join(io, Iterators.take(eval(ft), ft.n ), ' ')
end


macro code_str(s)
macro code_str(s)
eval(Meta.parse("[" * replace(s, "/" => "//", "\n" => "") * "]"))
eval(Meta.parse("[" * replace(s, "/" => "//", " " => ",") * "]"))
end
end


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


println("First 30 iterations of FRACTRAN program 'primes':\n2 ",
println("FRACTRAN interpreter implemented in Julia as an iterable struct\n")
join(Iterators.take(primes, 30), ' '), "\n")
println("First 25 iterations of FRACTRAN program primes:")
println("2 ", join(Iterators.take(primes, 25), ' '))
println("\nFirst 25 primes: ", primes)</syntaxhighlight>


println("First $(primes.n) primes:")
@show primes;</syntaxhighlight>
{{output}}
{{output}}
<pre>First 30 iterations of FRACTRAN program 'primes':
<pre>FRACTRAN interpreter implemented in Julia as an iterable struct
2 15 825 725 1925 2275 425 390 330 290 770 910 170 156 132 116 308 364 68 4 30 225 12375 10875 28875 25375 67375 79625 14875 13650 2550

First 25 iterations of FRACTRAN program primes:
2 15 825 725 1925 2275 425 390 330 290 770 910 170 156 132 116 308 364 68 4 30 225 12375 10875 28875 25375


First 25 primes:
First 25 primes: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97</pre>
primes = 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97</pre>


=={{header|Kotlin}}==
=={{header|Kotlin}}==