[R] Subsetting a large number into smaller numbers and find the largest product

(Ted Harding) Ted.Harding at wlandres.net
Thu Apr 18 11:06:47 CEST 2013


On 18-Apr-2013 08:47:18 Janesh Devkota wrote:
> Hello,
> 
> I have a big number lets say of around hundred digits. I want to subset
> that big number into consecutive number of 5 digits and find the product of
> those 5 digits. For example my first 5 digit number would be 73167. I need
> to check the product of the individual numbers in 73167 and so on.
> 
> The sample number is as follows:
> 
> 
> 731671765313306249192251196744265747423553491949349698352031277450632623957831
> 801698480186947885184385861560789112949495459501737958331952853208805511125406
> 98747158523863050715693290963295227443043557
> 
> I have a problem subsetting the small numbers out of the big number.
> 
> Any help is highly appreciated.
> 
> Best Regards,
> Janesh Devkota

Since the number is too large to be stored in any numerical format in R,
it needs to be stored as a character string:

X <- "731671765313306249....63295227443043557".

Then you can easily access successive 5-digit blocks as, e.g. for
block i (i = 1:N, where 5*N is the length of the number):

  block <- substr(X, 1+5*(i-1), 5*i)

You now have a 5-character string from which you can extract the
individual digits. And then on to whatever you want to do ...

Hoping this helps,
Ted.

-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 18-Apr-2013  Time: 10:06:43
This message was sent by XFMail



More information about the R-help mailing list