Long division algorithm using julia code for school project

Hi Team,
as part of my school project work I am trying to write code for division which also display steps similar to show in image below , but I am not able to display it in a proper way, Need help and support.

# Function to perform long division and print step by step
function long_division(dividend::Int, divisor::Int)
    println("Long Division of $dividend by $divisor:")
    println("     ---------")
    println(" " * string(divisor) * " | " * string(dividend))
    println("-"^10)
    
    quotient = div(dividend, divisor)
    remainder = dividend % divisor
    println(" " * string(divisor * quotient) * " | " * string(quotient))
    println(" " * "-"^5)
    println(" " * string(dividend - divisor * quotient))
    
    # Continue division for decimal places
    decimal_part = ""
    while remainder != 0
        dividend = remainder * 10
        new_quotient_part = div(dividend, divisor)
        remainder = dividend % divisor
        decimal_part *= string(new_quotient_part)
        println(" " * string(dividend))
        println(" " * string(divisor * new_quotient_part))
        println(" " * "-"^5)
        println(" " * string(remainder))
    end
    
    # Final result
    println("\nThe final result of the division is $quotient.$decimal_part")
end

# Call the function with the example from the image (45 Ă· 12)
long_division(45, 12)

Long Division of 45 by 12:
     ---------
 12 | 45
----------
 36 | 3
 -----
 9
 90
 84
 -----
 6
 60
 60
 -----
 0

The final result of the division is 3.75

need solution similar to below image

So what exactly is the question here? “Need help and support” is not particularly specific.

To me it looks like you only need to fix the indentation of the lines. So you’ll need to figure out how far to indent the first block and then how much further each successive block needs to be indented. I suggest keeping track of that with another variable. Then you just emit that amount of spaces for each new line within your while loop.

3 Likes

Hi,

Apart from the indentation, you should presumably also find the integer part in steps. For example, at the moment you have

julia> long_division(6279, 7)
Long Division of 6279 by 7:
     ---------
 7 | 6279
----------
 6279 | 897
 -----
 0

In this example you should start with the first two (why two?) digits of 6279, find 62 = 8 \cdot 7 + 6 and continue your loop with the remainder 6. Of course you should also put the decimal point (if applicable) at the correct spot.

Depending on the instructions of the project, it might also be considered improper to find the 8 above as div(62, 7). To give a more difficult example you could encounter, is it trivial that div(78974, 8764) == 9 (and in particular, not 8)? For the same reason, I don’t think you should use remainder = divident % divisor. Luckily you already wrote dividend - divisor * quotient elsewhere, which should be fine.

Another problem is that you can easily get into infinite loops, e.g. in long_division(1, 3). You should add a parameter for the number of digits of the quotient you want to compute and stop the loop accordingly.

Good luck with the project!

3 Likes

Hi Team,
since I am new to Julia I am still struggling with solution , can some one fix this program so that I continue my project…

Hi @Shiva_raj,
It’s great that you’re using Julia for a school project, but people here cannot (and will not) code your function in your place. If you have specific points in the answers above which you do not understand, feel free to ask more focused questions!

8 Likes

There are definitely examples of people providing code to solve a problem, but it’s very unlikely for this particular problem because 1) there are already libraries for dividing integers, and 2) this is one of the main goals in a school project, where there’s a greater responsibility for original work. So, people have provided advice instead of code.

Please try to use the advice to make a new version of the long_division method, and people will be more willing to offer further advice as the questions narrow and the outputs approach the goal.

3 Likes

Here is my advice

  1. Show us what source code you have for your code so far
  2. Get some paper and execute your code by hand, one step at a time
  3. Concentrate on the solution digit by digit
  4. Do not forget the leading zeros (eg. “03.75”) this makes your algorithm more uniform. You can remove the leading zeros once you got your final answer.
  5. If you want to make your algorithm more generalise (ie work on all integers) please use BigInt instead of Int64
2 Likes