# Re: [racket-users] Long division algorithm in HtDP way

```Let me support John’s email with a hint:

#lang htdp/isl+```
```
;; long division

;; Number Number -> (cons Number (cons Number [Listof Number]))
;; compute the list of steps needed to divide x by y

;; work thru an example
;; given  5432, 12
;; wanted [list 452 8) ... optional: (list 54 48) (list 63 60) (list 32 24)]
;; because
;; (quotient 5432 12) = 12
;; (remainder 5432 12) = 8
;; IGNORE the trace part

(define (long x y)
(cond
[(< x y) (list 0 x)]
[(= y x) (list 1 0)]
[else (local ((define r (long (- x y) y))
(define quotient  (first r))
(define remainder (second r)))
(list (+ quotient 1) remainder))]))

(long 5432 12)

> On Sep 7, 2017, at 2:09 PM, 'John Clements' via Racket Users
>
>
>> On Sep 7, 2017, at 9:28 AM, jaroslaw.mo...@gmail.com wrote:
>>
>>
>> Dear Racketeers!
>>
>> I would like to write the function divide in racket, for performing the long
>> division, which would print the whole computational process, as the
>> elementary school pupils do. For example, the call (divide 5432, 12) should
>> print this:
>>
>> 5432 : 12 = 452
>> -48
>> --
>> 63
>> -60
>> --
>>  32
>> -24
>>  --
>>   8
>>   =
>
> Sounds like fun!
>
> If I were you, I would separate this into two problems.
>
> 1) given the divisor and the divisee, produce a “trace” of the division.
> 2) given a “trace”, produce a (single multi-line) string representing it.
>
> Most of the thinking will be in developing the data definition for the
> “trace. ” Once this is done, the rest should be pretty straightforward.
>
> John Clements
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an