Janesh Devkota
2013-Apr-18 08:47 UTC
[R] Subsetting a large number into smaller numbers and find the largest product
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: 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557 I have a problem subsetting the small numbers out of the big number. Any help is highly appreciated. Best Regards, Janesh Devkota [[alternative HTML version deleted]]
(Ted Harding)
2013-Apr-18 09:06 UTC
[R] Subsetting a large number into smaller numbers and find the largest product
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 DevkotaSince 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
Jorge I Velez
2013-Apr-18 09:13 UTC
[R] Subsetting a large number into smaller numbers and find the largest product
Dear Janesh, Here is one way: # note "x" is a character x <- "73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557" k <- nchar(x) # digits in x b <- 5 # period tapply(strsplit(x, "")[[1]], rep(1:(nchar(x)/b), each = b), function(x) prod(as.numeric(x))) HTH, Jorge.- On Thu, Apr 18, 2013 at 6:47 PM, Janesh Devkota <janesh.devkota@gmail.com>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: > > > > 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557 > > I have a problem subsetting the small numbers out of the big number. > > Any help is highly appreciated. > > Best Regards, > Janesh Devkota > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
andrija djurovic
2013-Apr-18 09:17 UTC
[R] Subsetting a large number into smaller numbers and find the largest product
Hi. Here is one approach: options(scipen=300) numb <- 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557 strsplit(as.character(numb), "") blocks <- rep(1:(nchar(numb)/5), each=5) tapply(as.numeric(unlist(strsplit(as.character(numb), ""))), blocks, prod) Andrija On Thu, Apr 18, 2013 at 10:47 AM, Janesh Devkota <janesh.devkota@gmail.com>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: > > > > 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557 > > I have a problem subsetting the small numbers out of the big number. > > Any help is highly appreciated. > > Best Regards, > Janesh Devkota > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
arun
2013-Apr-18 12:55 UTC
[R] Subsetting a large number into smaller numbers and find the largest product
Hi, You could also use: x <- "73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557" ?sapply(strsplit(substring(x,seq(1,nchar(x)-4,5),seq(5,nchar(x),5)),""),function(x) prod(as.numeric(x))) #[1]?? 882?? 630???? 0?? 648??? 20? 6048? 1680?? 840?? 540? 3888 11664???? 0 #[13]? 1960???? 0? 1890???? 0? 1728???? 0 16128?? 480? 1920???? 0?? 162? 6480 #[25]???? 0? 1323?? 360? 3600???? 0???? 0???? 0 12096? 1400?? 864???? 0? 1620 #[37]???? 0?? 360???? 0? 2100 A.K. ----- Original Message ----- From: Janesh Devkota <janesh.devkota at gmail.com> To: groep R-help <r-help at r-project.org> Cc: Sent: Thursday, April 18, 2013 4:47 AM Subject: [R] Subsetting a large number into smaller numbers and find the largest product 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: 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557 I have a problem subsetting the small numbers out of the big number. Any help is highly appreciated. Best Regards, Janesh Devkota ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
arun
2013-Apr-18 13:42 UTC
[R] Subsetting a large number into smaller numbers and find the largest product
Hi Janesh, This is a bit shorter: library(seqinr) sapply(split(as.numeric(s2c(x)),((seq(nchar(x))-1)%/%5)+1),prod) ? A.K. ----- Original Message ----- From: arun <smartpink111 at yahoo.com> To: Janesh Devkota <janesh.devkota at gmail.com> Cc: R help <r-help at r-project.org> Sent: Thursday, April 18, 2013 8:55 AM Subject: Re: [R] Subsetting a large number into smaller numbers and find the largest product Hi, You could also use: x <- "73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557" ?sapply(strsplit(substring(x,seq(1,nchar(x)-4,5),seq(5,nchar(x),5)),""),function(x) prod(as.numeric(x))) #[1]?? 882?? 630???? 0?? 648??? 20? 6048? 1680?? 840?? 540? 3888 11664???? 0 #[13]? 1960???? 0? 1890???? 0? 1728???? 0 16128?? 480? 1920???? 0?? 162? 6480 #[25]???? 0? 1323?? 360? 3600???? 0???? 0???? 0 12096? 1400?? 864???? 0? 1620 #[37]???? 0?? 360???? 0? 2100 A.K. ----- Original Message ----- From: Janesh Devkota <janesh.devkota at gmail.com> To: groep R-help <r-help at r-project.org> Cc: Sent: Thursday, April 18, 2013 4:47 AM Subject: [R] Subsetting a large number into smaller numbers and find the largest product 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: 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557 I have a problem subsetting the small numbers out of the big number. Any help is highly appreciated. Best Regards, Janesh Devkota ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.