Array index out of range on solving cryptarithm

Monkey Forums/Monkey Beginners/Array index out of range on solving cryptarithm

Porsas(Posted 2016) [#1]
The problem was EVE/DID = 0.TALKTALKTALK... where E,V,D,I,T,A,L and K are distinct single digits and EVE and DID are 3-digit coprimes. TALK is a 4 digit integer and right side of equation is a repeating decimal number. Error comes when trying to include line
Local A:Int=Int(TALK[3])

and previous line does not give that error
the whole program is here



muddy_shoes(Posted 2016) [#2]
Right, because for some of the combinations you're trying the result of your division will produce a number with fewer digits and therefore a TALK array with fewer elements than you're trying to access.


Porsas(Posted 2016) [#3]
Yes! Now it is clear, thanks!


Gerry Quinn(Posted 2016) [#4]
Incidentally, while it doesn't affect the exact problem you have here, you'll generally find that ints are better than floats for problems like this. Just a more direct translation between characters and values, and no rounding-related gotchas.


Porsas(Posted 2016) [#5]
This line just after declaring TALK corrected the problem: TALK = TALK.Resize(10). I then appended the line that makes sure every single digit is distinct
 (If E <> V And E <> D And E <> I And V <> D And V <> I And V <> E And D <> I And T <> E And T <> V And T <> D And T <> I And T <> A And T <> L And T <> K And A <> E And A <> V And A <> D And A <> I And A <> L And A <> K And L <> E And L <> V And L <> D And L <> I And L <> K And K <> E And K <> V And K <> D) 

and finally got only one answer out for EVE / DID = 0.TALKTALKTALK...
242 / 303 = 0.7986798679867987
This was my first monkey program btw. and I am still watching the tutorial videos.
And on second program I used Int-only solution after toiling with floats and rounding problems:
Find positive integers a and b and prime p so that a^2(b-a)/(b+a)=p^2.

And this prime function for isprime is handy because it checks only up to the square root: