Problem with coercing a huge string into an integer value

Hello. Trying to create a number by stringing together the digits "146" 841 times, then convert it into a numeric variable so that I can check its remainder when dividing by 11.

However, when I try to coefrce the string into a different type, I either get this error warning:

make_huge <- function(digits,repeats) {
  paste(rep(digits,repeats), collapse = "")
}

integer_huge <- as.numeric(make_huge)

"Error in as.numeric(make_huge) : 
  cannot coerce type 'closure' to vector of type 'double' "

or this one:

integer_huge <- as.integer(make_huge)
"Warning message:
NAs introduced by coercion to integer range "

Any idea how I can fix this? Or an entirely different approach to generating a huge number without having to type it and then be able to do arithmetic functions on it?

1 Like

R (like almost all programs) uses 32-bit integers, which means the largest integer is 2,147,483,647 ( 2^31 - 1 ). So use of as.integer() is doomed.

The first error message is because you applied as.numeric() to make_huge (a function definition), which makes no sense. You could try as.numeric(make_huge("146", 841)), but that will just produce Inf as the result.

Your best bet is to use a loop, where in each pass you take the result of the previous pass (an integer from 0 to 10), multiply it by 1000 and add 146, get the remainder mod 11, and move on to the next pass.

1 Like

Your issue comes from two problems:

  1. You're passing a function (make_huge) instead of calling it. You need to call make_huge("146", 841).
  2. The number is too large for R's numeric types. R's numeric (double) and integer types cannot store such a huge number directly.

Solution: Use modular arithmetic directly

Since you're only interested in the remainder when dividing by 11, you can compute it without storing the full number:

make_huge_mod11 <- function(digits, repeats, mod) {
  num_digits <- strsplit(digits, "")[[1]]  # Split "146" into c("1", "4", "6")
  num_digits <- as.numeric(num_digits)     # Convert to numeric
  pattern_length <- length(num_digits)

  remainder <- 0
  for (i in 1:(repeats * pattern_length)) {
    remainder <- (remainder * 10 + num_digits[(i - 1) %% pattern_length + 1]) %% mod
  }
  
  return(remainder)
}

make_huge_mod11("146", 841, 11)

This avoids constructing the huge number while still computing the remainder correctly.

1 Like

Yes, thank you. It was a bad copy and paste job. I meant to apply as.integerto the function output, which I thought I had stored as make_huge. Instead, in this version of the code I had used that as the function definition.

I hadn't thought about the 32-bit limit.

1 Like

Ah, excellent. This was actually a bad copy and paste. Elsewhere in a later chunk of code I didn't include, I had saved the output of that function as make_huge. I had pasted the error codes from a few different places.

I wasn't sure how to how to perform the operations on an object I hadn't actually defined outside of the function. Thank you!

1 Like

You might also try the bignum package

Best,
Randy

1 Like