Left factorials: Difference between revisions
→{{header|R}}: Vectorization solution
m (Phix/mpfr) |
ReeceGoding (talk | contribs) (→{{header|R}}: Vectorization solution) |
||
Line 2,614:
=={{header|R}}==
===Imperative solution===
<lang rsplus>library(gmp)
left_factorial <- function(n) {
Line 2,679:
!8000 has 27749 digits
!9000 has 31678 digits
!10000 has 35656 digits</pre>
===Vectorization solution===
Due to vectorization, these sorts of problems are R's bread and butter. The only challenge comes from making sure that R plays nice with objects from the gmp library.
<lang r>library(gmp)
leftFactorial<-function(n)
{
if(n==0){0}
else{sum(factorialZ(0:(n-1)))}
}
leftFactorialVecInput<-function(vector)
{
Vectorize(leftFactorial,SIMPLIFY=FALSE)(vector)
}
#Task 1
leftFactorialVecInput(0:10)
#Task 2
leftFactorialVecInput(seq(20,110,by=10))
#Task 3
#nchar isn't very well-behaved on big numbers so it needs help from as.character
sapply(leftFactorialVecInput(seq(1000,10000,by=1000)),function(x) nchar(as.character(x)))</lang r>
=={{header|Racket}}==
|