debugging - r - Project Euler 40: getting the final digit wrong -


an irrational decimal fraction created concatenating positive integers:

0.123456789101112131415161718192021...

it can seen 12th digit of fractional part 1.

if dn represents nth digit of fractional part, find value of following expression.

d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000

problem source here.

i wrote following code calculate digits:

ans = data.frame(matrix(ncol = 6, nrow=2)) colnames(ans) = c(10,100,1000,10000,100000,1000000) rownames(ans) = c("length","number")  counter = 1 for(i in c(10,100,1000,10000,100000,1000000)) {   c = 0   num = 0    while (c<i)   {     num = num + 1     c = c + nchar(as.character(num))   }    ans[1, counter] = c   ans[2, counter] = num    counter = counter + 1  } 

the output, is:

> ans        10 100 1000 10000  1e+05   1e+06 length 11 101 1002 10001 100004 1000004 number 10  55  370  2777  22222  185185 

in other words, when irrational number 11 digit long, final 2 digits 10, means 10th digit 1. when irrational number 1002 digits long, final 3 digits 370, means 1000th digit 3. , on.

from this, answer is: 1 * 1 * 5 * 3 * 7 * 2 * 8. however, correct answer 1 * 1 * 5 * 3 * 7 * 2 * 1. beats me how able produce correct answers until final digit.

your method gives correct answer if force calculations integer arithmetic - add l numbers: c=0l, num=num+1l, etc.

it goes wrong @ point num=100000 , c=488894 or 488895. without integer arithmetic, using character string "1e+05" instead of "100000", 1 character shorter. 100001 , later not affected. setting options(scipen=99) stop doing this.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -