Array index out of range on solving cryptarithm
Monkey Forums/Monkey Beginners/Array index out of range on solving cryptarithm
| ||
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 lineLocal A:Int=Int(TALK[3]) and previous line does not give that error the whole program is here |
| ||
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. |
| ||
Yes! Now it is clear, thanks! |
| ||
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. |
| ||
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: |