[R] Subsetting a large number into smaller numbers and find the largest product
arun
smartpink111 at yahoo.com
Thu Apr 18 15:42:50 CEST 2013
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.
More information about the R-help
mailing list