diff options
| author | wanderdoc <wanderdoc@googlemail.com> | 2019-12-31 19:17:58 +0100 |
|---|---|---|
| committer | wanderdoc <wanderdoc@googlemail.com> | 2019-12-31 19:17:58 +0100 |
| commit | dcf12e71a80d7a560cdb99a047a524062ada80d3 (patch) | |
| tree | 2f085385c399b2330c64734357150c40e6204361 /challenge-041/wanderdoc/R | |
| parent | 4f1588a9cd039d5d80731d131187645c37faed45 (diff) | |
| download | perlweeklychallenge-club-dcf12e71a80d7a560cdb99a047a524062ada80d3.tar.gz perlweeklychallenge-club-dcf12e71a80d7a560cdb99a047a524062ada80d3.tar.bz2 perlweeklychallenge-club-dcf12e71a80d7a560cdb99a047a524062ada80d3.zip | |
Solutions from wanderdoc. Happy New Year!
Diffstat (limited to 'challenge-041/wanderdoc/R')
| -rw-r--r-- | challenge-041/wanderdoc/R/ch-01.R | 80 | ||||
| -rw-r--r-- | challenge-041/wanderdoc/R/ch-02.R | 29 |
2 files changed, 109 insertions, 0 deletions
diff --git a/challenge-041/wanderdoc/R/ch-01.R b/challenge-041/wanderdoc/R/ch-01.R new file mode 100644 index 0000000000..36ab135753 --- /dev/null +++ b/challenge-041/wanderdoc/R/ch-01.R @@ -0,0 +1,80 @@ +decomp <- function(x) +{ + num <- x + + div_max <-num/2 + fct<-numeric(0) + if(x <=3) {return(fct)} # because of for loop below. + + for(i in 2:div_max) + { + while( 0 == x%%i) + { + + fct <- c(fct, i) + x = x/i + } + } + fct<-fct[!is.na(fct)] +} + +# https://stackoverflow.com/questions/3789968 , here slightly modified to match 2 too: + +isPrime <- function(x) +{ + div <- 2:ceiling(sqrt(x)) + + !any(x %% div == 0) || 2 == x +} + +print_attractive <- function(x) +{ + factors <- decomp(x) + size <- length(factors) + if(size > 0 && isPrime(size)) + { + print(paste(x, paste(factors, collapse=" x "), sep = " = ")) + } +} + + +for (i in 1:50) +{ + print_attractive(i) +} + + + + + +# Prints: +# [1] "4 = 2 x 2" +# [1] "6 = 2 x 3" +# [1] "8 = 2 x 2 x 2" +# [1] "9 = 3 x 3" +# [1] "10 = 2 x 5" +# [1] "12 = 2 x 2 x 3" +# [1] "14 = 2 x 7" +# [1] "15 = 3 x 5" +# [1] "18 = 2 x 3 x 3" +# [1] "20 = 2 x 2 x 5" +# [1] "21 = 3 x 7" +# [1] "22 = 2 x 11" +# [1] "25 = 5 x 5" +# [1] "26 = 2 x 13" +# [1] "27 = 3 x 3 x 3" +# [1] "28 = 2 x 2 x 7" +# [1] "30 = 2 x 3 x 5" +# [1] "32 = 2 x 2 x 2 x 2 x 2" +# [1] "33 = 3 x 11" +# [1] "34 = 2 x 17" +# [1] "35 = 5 x 7" +# [1] "38 = 2 x 19" +# [1] "39 = 3 x 13" +# [1] "42 = 2 x 3 x 7" +# [1] "44 = 2 x 2 x 11" +# [1] "45 = 3 x 3 x 5" +# [1] "46 = 2 x 23" +# [1] "48 = 2 x 2 x 2 x 2 x 3" +# [1] "49 = 7 x 7" +# [1] "50 = 2 x 5 x 5"
\ No newline at end of file diff --git a/challenge-041/wanderdoc/R/ch-02.R b/challenge-041/wanderdoc/R/ch-02.R new file mode 100644 index 0000000000..397aa8cb93 --- /dev/null +++ b/challenge-041/wanderdoc/R/ch-02.R @@ -0,0 +1,29 @@ +fib <- function(n) +{ + x <- 0; y <- 1; + + m <- 0; + while (m < n) + { + temp <-x; + x <- y; + y <- temp + y; + m = m + 1; + } + + x +} + + +leonardo <- function(x) +{ + leo_num <- 2 * fib(x + 1) - 1 + leo_num +} + + +sapply(1:20, leonardo) + +# Prints: +# [1] 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 +# [16] 3193 5167 8361 13529 21891 |
