Count in factors: Difference between revisions
Content added Content deleted
(Wrote a R code for countinfactors function) |
|||
Line 2,621: | Line 2,621: | ||
Number of primes gathered up to 5000 is 669 |
Number of primes gathered up to 5000 is 669 |
||
CacheInfo(hits=3935, misses=7930, maxsize=2000, currsize=2000)</pre> |
CacheInfo(hits=3935, misses=7930, maxsize=2000, currsize=2000)</pre> |
||
=={{header|R}}== |
|||
<lang R> |
|||
#initially I created a function which returns prime factors then I have created abother function counts in the factors and #prints the values. |
|||
findfactors <- function(num) { |
|||
x <- c() |
|||
p1<- 2 |
|||
p2 <- 3 |
|||
everyprime <- num |
|||
while( everyprime != 1 ) { |
|||
while( everyprime%%p1 == 0 ) { |
|||
x <- c(x, p1) |
|||
everyprime <- floor(everyprime/ p1) |
|||
} |
|||
p1 <- p2 |
|||
p2 <- p2 + 2 |
|||
} |
|||
x |
|||
} |
|||
count_in_factors=function(x){ |
|||
primes=findfactors(x) |
|||
x=c(1) |
|||
for (i in 1:length(primes)) { |
|||
x=paste(primes[i],"x",x) |
|||
} |
|||
return(x) |
|||
} |
|||
count_in_factors(72) |
|||
{{out}} |
|||
<pre> |
|||
[1] "3 x 3 x 2 x 2 x 2 x 1" |
|||
</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |