Jump to content

Continued fraction/Arithmetic/Construct from rational number: Difference between revisions

Line 721:
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
<lang Julia># It's most appropriate to define a Julia iterable object for this task
 
# Julia doesn't have Python's yield, the closest to it is produce/consume calls with Julia tasks
<lang Juliajulia># It'sst most appropriate to define a Julia iterable object for this task
# Julia doesn't have Python'sst yield, the closest to it is produce/consume calls with Julia tasks
# but for various reasons they don't work out for this task
# This solution works with two integers, a Julia rational or a real
 
mutable struct ContinuedFraction{T<:Integer}
type R2cf
n1::Union{Int,Float64}T # numerator or real
n2::IntT # denominator or 1 if real
t1::IntT # generated coefficient
f::Float64 # aux. field for working with real inputs
end
 
# constructorsConstructors for all possible input types
ContinuedFraction{T<:Integer}(n1::T, n2::T) = ContinuedFraction(n1, n2, 0)
R2cf(n::Union{Int,Float64})=R2cf(n,1,0,0.1)
ContinuedFraction(n::Rational) = ContinuedFraction(numerator(n), denominator(n))
R2cf(n1::Int,n2::Int)=R2cf(n1,n2,0,0.1)
ContinuedFraction(n::AbstractFloat) = ContinuedFraction(Rational(n))
R2cf(r::Rational{Int})=R2cf(num(r),den(r),0,0.1)
 
# methodsMethods to make our object iterable
Base.start(::R2cfContinuedFraction) = nothing
# returnsReturns true if we've prepared the continued fraction
 
Base.done(cf::ContinuedFraction, st) = cf.n2 == 0
# generatesGenerates the next coefficient
function Base.next(cf::R2cfContinuedFraction,s st)
if typeof(cf.n1)==Int
cf.n1, (cf.t1, cf.n2) = cf.n2, divrem(cf.n1, cf.n2)
return (cf.t1, nothing)
else
cf.t1,cf.f=divrem(cf.n1,1)
if cf.f!=0.0 cf.n1=1/cf.f end
end
return (cf.t1,nothing)
end
 
# tellTell Julia that this object always returns ints (all coeffs are integers)
# returns true if we've prepared the continued fraction
Base.eltype{T}(::Type{R2cfContinuedFraction{T}}) =Int T
function Base.done(cf::R2cf,s)
if typeof(cf.n1)==Int
return cf.n2==0
else
return cf.f==0.0
end
end
 
# overloadOverload the default collect function so that we can collect the first maxiter coeffs of infinite continued fractions
# tell Julia that this object always returns ints (all coeffs are integers)
Base.eltype(::Type{R2cf})=Int
 
# overload the default collect function so that we can collect the first maxiter coeffs of infinite continued fractions
# array slicing doesn't work as Julia crashes before the slicing due to our infinitely long array
function Base.collect(itr::R2cfContinuedFraction, maxiter::IntInteger = 100)
r = Array{eltype(itr)}(maxiter)
r=Array{Int,1}() # all results are ints
i = 1
for v= in itr
r[i] = v
push!(r,v)
i += 1
if i== > maxiter break end
end
return r[1:i-1]
end
 
# testTest cases according to task description with outputs in comments
println(collect(R2cfContinuedFraction(1, 2))) # => [0, 2]
println(collect(R2cfContinuedFraction(3, 1))) # => [3]
println(collect(R2cfContinuedFraction(23, 8))) # => [2, 1, 7]
println(collect(R2cfContinuedFraction(13, 11))) # => [1, 5, 2]
println(collect(R2cfContinuedFraction(22, 7))) # => [3, 7]
println(collect(R2cfContinuedFraction(14142, 10000))) # => [1, 2, 2, 2, 2, 2, 1, 1, 29]
println(collect(R2cfContinuedFraction(141421, 100000))) # => [1, 2, 2, 2, 2, 2, 2, 3, 1, 1, 3, 1, 7, 2]
println(collect(R2cfContinuedFraction(1414214, 1000000))) # => [1, 2, 2, 2, 2, 2, 2, 2, 3, 6, 1, 2, 1, 12]
println(collect(R2cfContinuedFraction(14142136, 10000000))) # => [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6, 1, 2, 4, 1, 1, 2]
 
println(collect(R2cfContinuedFraction(13 // 11))) # => [1, 5, 2]
println(collect(R2cfContinuedFraction(2 ^ 0.5√2), 20)) # => [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]</lang>
 
=={{header|Kotlin}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.