Fractran: Difference between revisions

Content added Content deleted
mNo edit summary
Line 1,866: Line 1,866:


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
<lang julia>
function fractran(n, rationals, steplimit)
<lang julia>function fractran(n::Integer, ratios::Vector{<:Rational}, steplim::Integer)
ret = zeros(BigInt, steplimit)
rst = zeros(BigInt, steplim)
for i in 1:steplimit
for i in 1:steplim
ret[i] = n
rst[i] = n
if (pos = findfirst(r -> isinteger(n*r), rationals)) > 0
if (pos = findfirst(x -> isinteger(n * x), ratios)) > 0
n *= rationals[pos]
n *= ratios[pos]
else
else
break
break
end
end
end
end
ret
return rst
end
end


using IterTools
function str2Rationals(s)
macro ratio_str(s)
a = collect(split(s,r"[\s,/]+"))
a = split(s, r"[\s,/]+")
[parse(BigInt,a[i])//parse(BigInt,a[i+1]) for i in 1:2:length(a)-1]
return collect(parse(BigInt, n) // parse(BigInt, d) for (n, d) in partition(a, 2))
end
end

fracstring = """17 / 91, 78 / 85, 19 / 51, 23 / 38, 29 / 33, 77 / 29, 95 / 23,
fracs = ratio"""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"""
77 / 19, 1 / 17, 11 / 13, 13 / 11, 15 / 14, 15 / 2, 55 / 1"""
println("The first 20 in the series are ", fractran(2, fracs, 20))
fracts = str2Rationals(fracstring)
println("The first 20 in the series are ", fractran(2, fracts, 20))


primesfound = 0
prmfound = 0
n = BigInt(2)
n = big(2)
while primesfound < 20
while prmfound < 20
if isinteger(log2(n))
if isinteger(log2(n))
primesfound += 1
prmfound += 1
println("Prime $primesfound found: $n is 2 ^ $(Int(round(log2(n))))")
println("Prime $prmfound found: $n is 2 ^ $(Int(log2(n)))")
end
end
n = fractran(n, fracts, 2)[2]
n = fractran(n, fracs, 2)[2]
end
end</lang>

</lang>
{{output}}
{{output}}
<pre>The first 20 in the series are BigInt[2, 15, 825, 725, 1925, 2275, 425, 390, 330, 290, 770, 910, 170, 156, 132, 116, 308, 364, 68, 4]
<pre>
The first 20 in the series are BigInt[2, 15, 825, 725, 1925, 2275, 425, 390, 330, 290, 770, 910, 170, 156, 132, 116, 308, 364, 68, 4]
Prime 1 found: 2 is 2 ^ 1
Prime 1 found: 2 is 2 ^ 1
Prime 2 found: 4 is 2 ^ 2
Prime 2 found: 4 is 2 ^ 2
Line 1,923: Line 1,921:
Prime 18 found: 576460752303423488 is 2 ^ 59
Prime 18 found: 576460752303423488 is 2 ^ 59
Prime 19 found: 2305843009213693952 is 2 ^ 61
Prime 19 found: 2305843009213693952 is 2 ^ 61
Prime 20 found: 147573952589676412928 is 2 ^ 67
Prime 20 found: 147573952589676412928 is 2 ^ 67</pre>
</pre>


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