Problem 80

Problem 80 - Project Euler
平方根を整数部含めて100桁計算し、桁の値の和を求めろという問題。
開平方ムズい。
gmpをまた使ってしまって13秒。

library(gmp)
sqrt.100 <- function(x){
  n <- as.bigz(x)
  a <- as.bigz(0)
  ans <- numeric(100)
  for(i in 1:100){
    c <- 0
    repeat{
      if((a+c+1)*(c+1) > n) break
      c <- c+1
    }
    ans[i] <- c
    n <- (n-(a+c)*c)*100
    a <- (a+2*c)*10
  }
  return(ans)
}

ans <- 0
for(i in 1:100){
  if(sqrt(i)%%1==0) next
  ans <- ans + sum(sqrt.100(i))
}