How to augment an `Int64` 1-D array or vector

function continued_fraction(num, denom)
    a = []
    quo, rem = divrem(num, denom)
    a0 = quo
    push!(a, a0)
    num = denom
    denom = rem
    i = 1
    while (rem != 0)
        quo, rem = divrem(num, denom)
        push!(a, quo)
        num = denom
        denom = rem
        i += 1
    end
    return(a)
end

a = continued_fraction(1001, 64)
println(a)

I am experimenting writing continued fraction functions; at present restricted to integers, as shown above.

I got the above to work, but I want the array or vector a to be an integer so that I do not get the Any in front of the output.

But I have found that push! does not work with an Int64 array.

Kindly help me improve the above code.

[] is equivalant to Any[] and creates an empty vector that can hold values of any type. Int[] creates an empty vector that can hold values of type Int.

It is a bit unclear what you tried that didn’t work, but your code runs if you change [] to Int[] at least.

What is the difference between using Int and Int64?

I tried a = Array{Int64, 1} and got into strife with push! which was why I raised the query.

On another note, is there a neat way to change the first comma in the output to a semi-colon?

Thank you.

Int is just an alias for your native integer type, on most systems this is equivalent to Int64 (64-bit integer).

That is a type not a value with that type. You could use Array{Int, 1}(), or the equivalent Vector{Int}(), but using Int[] is the most common method I think.

I don’t know what this means – there are no commas printed in my output:

julia> a = continued_fraction(1001, 64)
8-element Vector{Int64}:
 15
  1
  1
  1
  3
  1
  1
  2

The output from my above code, using a file rather than the REPL, gives me a row vector:
[15, 1, 1, 1, 3, 1, 1, 2]

If I can change the first comma to a semi-colon, keeping the other commas intact, the output will be in one of the standard ways to express continued fractions.

What I am seeking is [15; 1, 1, 1, 3, 1, 1, 2]

println("[$(a[1]); " * join(a[2:end], ", ") * "]") seems to do the trick.

If there are better ways, please advise.

Are you trying to show that array like that? Or to also enable the user to define a continues fraction through that syntax

In any case, defining your own struct and extending a method for showing it seems a robust solution.

Initially, I am going from a fraction to a continued fraction expansion.

I am not thinking now of the reverse process of going from the continued fraction to the fraction.

I have to read up on structs (like C?) before I take up your hint of one data structure for the forward and reverse directions. It strikes me as a clean approach.

This is not actually a row vector, but a flat, 1D vector, which behaves like a column vector. It just happens to print horizontally in some contexts.

This isn’t valid Julia syntax. You can either write

[15, 1, 1, 1, 3, 1, 1, 2]

or

[15; 1; 1; 1; 3; 1; 1; 2]

they are equal, and represent regular (column-like) vectors.

For a row-vector, transpose this, or write

[15 1 1 1 3 1 1 2] 

for a row-like 1xN matrix.

1 Like