Count in factors: Difference between revisions
Content added Content deleted
(Wrote a R code for countinfactors function) |
|||
Line 2,624: | Line 2,624: | ||
=={{header|R}}== |
=={{header|R}}== |
||
<lang R> |
<lang R> |
||
#initially I created a function which returns prime factors then I have created |
#initially I created a function which returns prime factors then I have created another function counts in the factors and #prints the values. |
||
findfactors <- function(num) { |
findfactors <- function(num) { |
||
Line 2,641: | Line 2,641: | ||
x |
x |
||
} |
} |
||
⚫ | |||
count_in_factors=function(x){ |
count_in_factors=function(x){ |
||
primes=findfactors(x) |
primes=findfactors(x) |
||
Line 2,654: | Line 2,654: | ||
<pre> |
<pre> |
||
[1] "3 x 3 x 2 x 2 x 2 x 1" |
[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)) |
|||
⚫ | |||
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> |
</pre> |
||