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, |
<lang julia>function fractran(n::Integer, ratios::Vector{<:Rational}, steplim::Integer) |
||
rst = zeros(BigInt, steplim) |
|||
for i in 1: |
for i in 1:steplim |
||
rst[i] = n |
|||
if (pos = findfirst( |
if (pos = findfirst(x -> isinteger(n * x), ratios)) > 0 |
||
n *= |
n *= ratios[pos] |
||
else |
else |
||
break |
break |
||
end |
end |
||
end |
end |
||
return rst |
|||
end |
end |
||
using IterTools |
|||
function str2Rationals(s) |
|||
macro ratio_str(s) |
|||
a = |
a = split(s, r"[\s,/]+") |
||
return collect(parse(BigInt, n) // parse(BigInt, d) for (n, d) in partition(a, 2)) |
|||
end |
end |
||
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""" |
||
⚫ | |||
fracts = str2Rationals(fracstring) |
|||
⚫ | |||
prmfound = 0 |
|||
n = |
n = big(2) |
||
while |
while prmfound < 20 |
||
if isinteger(log2(n)) |
if isinteger(log2(n)) |
||
prmfound += 1 |
|||
println("Prime $ |
println("Prime $prmfound found: $n is 2 ^ $(Int(log2(n)))") |
||
end |
end |
||
n = fractran(n, |
n = fractran(n, fracs, 2)[2] |
||
end |
end</lang> |
||
</lang> |
|||
{{output}} |
{{output}} |
||
⚫ | |||
<pre> |
|||
⚫ | |||
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}}== |