Frizzus
unread,Mar 4, 2025, 11:08:21 AMMar 4Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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
```