0

I have a 50 digits number.

I'd like to write a function that will decompose this number to 50, 25 or 5 digits different numbers (oany number k for which 50/k == integer).

my attempts:

x <- 37107287533902102798797998220837590246510135740250

print(log10(x))
[1] 49.56946

this isn't helping my with discompose the number, as

length(x) == length(log10(x)) == 1

is there a function like this? or any way to discompose a vector to its digits/ characters?

Thank you.

Noa Even
  • 137
  • 1
  • 10
  • 2
    Why not adding what you have already try? Also are those ``k`` numbers random? – Gainz Jun 13 '19 at 15:34
  • 2
    What does decompose mean in this context? Maybe `substring` with `cut`??? – cory Jun 13 '19 at 15:36
  • @cory I'm not sure but I think he is looking for something along the line of ``cut``. – Gainz Jun 13 '19 at 15:37
  • 3
    Can you provide a smaller example with an example of what you would hope for. – Dason Jun 13 '19 at 15:38
  • 1
    How long is the number? Your title says 5k digits, your first line says 50k digits. And how is it stored now? Is it in a file? Do you have a function that returns it? Is it a vector of individual digits? A single element of a `character` vector? Something else? What is the output your are hoping for? – Gregor Thomas Jun 13 '19 at 15:40
  • 1
    @gainz i fixed it. was a mistake – Noa Even Jun 13 '19 at 15:53
  • @cory -- insted of having one number with 50k digits, I want to have 100 numbers with 50 digits each – Noa Even Jun 13 '19 at 15:54
  • @dason fixed it – Noa Even Jun 13 '19 at 15:54
  • @Gregor number is 5000 digits, and I'd like to print a list/ vector of 100 numbers, each of them is a 50 digits number – Noa Even Jun 13 '19 at 15:55
  • 1
    If you read in the number as an integer, it will not be exactly the same as it's too big. You need to read it in as a character. For example, with `a <- 12345678901234567890`, you get `12345678901234567168` when you print it out. – maccruiskeen Jun 13 '19 at 15:55
  • @christoph but just changing the class of the vector doesn't really help me – Noa Even Jun 13 '19 at 15:59
  • You could just do, but recognize that the numbers will be wrong for large numbers `f <- function(k) { sapply(seq(1, nchar(paste(a)), by = k), function(x) substr(paste(a), x, x+k-1)) }` – maccruiskeen Jun 13 '19 at 16:07
  • @christoph so I changed the question so I have a 50 number digits and I'd like to find a way to decompose this number into n different numbers, each of them has the same number of digits – Noa Even Jun 13 '19 at 16:10

1 Answers1

2
x <- 37107287533902102798797998220837590246510135740250
options(scipen=999)
f <- function(k, x) {
  sapply(seq(1, nchar(paste(x)), by = k), function(y)
         substr(paste(x), y, y + k - 1))
}
f(5, x)
# [1] "37107" "28753" "39021" "04311" "02574" "03046" "89820" "49532" "36471" "80800"
f(10, x)
# [1] "3710728753" "3902104311" "0257403046" "8982049532" "3647180800"
f(25, x)
# [1] "3710728753390210431102574" "0304689820495323647180800"
maccruiskeen
  • 2,748
  • 2
  • 13
  • 23
  • This example illustrates nicely R's integer limit. Notice that the last digits of the input are `250` and the last digits of the output are `800`. With any inputs bigger than For accuracy, OP needs to either give the input as a `character` (with quotes around it) or use a special purpose package for big integers, [such as those recommended in answers to [this question](https://stackoverflow.com/q/2053397/903061) – Gregor Thomas Jun 13 '19 at 20:31