I was showing my daughter the magic pattern of two 111..111 number multiplication in R, e.g.
> options(scipen=999)
> 1^2
[1] 1
> 11^2
[1] 121
> 111^2
[1] 12321
> 1111^2
[1] 1234321
> 11111^2
[1] 123454321
> 111111^2
[1] 12345654321
> 1111111^2
[1] 1234567654321
> 11111111^2
[1] 123456787654321
> 111111111^2
[1] 12345678987654320
Everything works perfectly until it goes to 9 digits. As you can tell from the last line, the answer is obviously wrong; it should be 1234567898765432*1*, not 1234567898765432*0*.
I am running this in R (v3.6.1, x86_64-apple-darwin15.6.0, 64-bit) on a Macbook Pro laptop.
Someone said that it might be caused by an integer overflow. Two questions here:
Can anyone explain how this happened exactly? e.g. how was the last 1 changed to 0?
How can I get the correct answer in R?
Thanks.