Jump to content

Count in factors: Difference between revisions

(Wrote a R code for countinfactors function)
Line 2,624:
=={{header|R}}==
<lang R>
#initially I created a function which returns prime factors then I have created abotheranother function counts in the factors and #prints the values.
 
findfactors <- function(num) {
Line 2,641:
x
}
</lang>
 
count_in_factors=function(x){
primes=findfactors(x)
Line 2,654:
<pre>
[1] "3 x 3 x 2 x 2 x 2 x 1"
</pre>
 
=={{header|Racket}}==
<lang racket>
#lang racket
(require math)
 
(define (~ f)
(match f
[(list p 1) (~a p)]
[(list p n) (~a p "^" n)]))
 
(for ([x (in-range 2 20)])
(display (~a x " = "))
(for-each display (add-between (map ~ (factorize x)) " * "))
(newline))
</lang>
Output:
<pre>
2 = 2
3 = 3
4 = 2^2
5 = 5
6 = 2 * 3
7 = 7
8 = 2^3
9 = 3^2
10 = 2 * 5
11 = 11
12 = 2^2 * 3
13 = 13
14 = 2 * 7
15 = 3 * 5
16 = 2^4
17 = 17
18 = 2 * 3^2
19 = 19
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.