Problem with darray

17 views
Skip to first unread message

Frizzus

unread,
Mar 4, 2025, 11:08:21 AMMar 4
to 4tH-compiler
Hi,
I'm new to 4tH and to get some experience i am doing some exercise that I've found on the rosetta code website.
I'm doing the "Proper divisors" exercise with a twist.
The exercise wants the code to print the answer, and I want to store the result in an array.
I use "darray" in my code and I get this error.

```
The proper divisors of 6 are : {1 , 2 , 3 }
The proper divisors of 100 are : {1 , 2 , 4 , 5 , 10 , 20 , 25 , 50 }
Cannot resize darray
Executing;      Word 217: Unhandled exception

```

My guess is that the darray structure is not meant to handle that much data.
So how could I handle the data if I can't resize my darray ?
Should I use another array ?
Is there a way to handle more data with a darray ?

Loïc

My code :
```
include lib/darray.4th
\ All number used will be positive integers

( is n1 a proper divisor of n2 )
: proper_divisor?  ( n1 n2 -- bool )
    swap mod
    0 =
;
: proper_divisors_of ( n -- )
    variable divisors
    divisors +darray

    dup 1 do
        dup i swap proper_divisor? if
            divisors d.cell+
            i divisors d.len 1 - divisors d.!
        then
    loop
;


variable number_in

: .divisors ( -- )
    ." The proper divisors of " number_in @ . ." are : {"
    divisors d.len 0 ?do
        i divisors d.@ .
        i divisors d.len 1 - = not if ." , " then
    loop
    ." }" cr
;

\ proper_divisors_of tests
6 number_in !
number_in @ proper_divisors_of
.divisors

divisors -darray

100 number_in !
number_in @ proper_divisors_of
.divisors

divisors -darray

\ get the integer with the most proper divisors up to 20 000
variable divisor_count
0 divisor_count !
variable answer
0 answer !
20000 1 ?do
    i proper_divisors_of
    divisors d.len divisor_count @ > if
        divisors d.len divisors !
        i answer !
    then
loop
." The number between 1 and 20 000 with the most proper divisors is " answer @ . ." with " divisor_count @ . ." divisors"

divisors -darray

```

Frizzus

unread,
Mar 4, 2025, 3:23:18 PMMar 4
to 4tH-compiler
I just noticed I sent a duplicate of my question by accident in the group. I'm sorry for the inconvenience, I'm not used to use google groups.

The Beez

unread,
Mar 4, 2025, 3:25:19 PMMar 4
to 4tH-compiler
No problem. But I'm only answering one of them ;-)

Hans Bezemer

The Beez

unread,
Mar 5, 2025, 7:31:20 AMMar 5
to 4tH-compiler
Frizzus!

BTW, I've given you (and a whole of a lot other people) access to post messages unmoderated.
So, next time you post something, it should show up immediately.

Hans Bezemer
Reply all
Reply to author
Forward
0 new messages