diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-12-31 19:29:16 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-31 19:29:16 +0000 |
| commit | 86ce9124e2522d57f3870da95b02a67deae288a3 (patch) | |
| tree | 542e862009cbcf905f078368ad855c79b3c545ff | |
| parent | c1993d1a350e3e646b70c8b805466d8beda3873e (diff) | |
| parent | dcf12e71a80d7a560cdb99a047a524062ada80d3 (diff) | |
| download | perlweeklychallenge-club-86ce9124e2522d57f3870da95b02a67deae288a3.tar.gz perlweeklychallenge-club-86ce9124e2522d57f3870da95b02a67deae288a3.tar.bz2 perlweeklychallenge-club-86ce9124e2522d57f3870da95b02a67deae288a3.zip | |
Merge pull request #1096 from wanderdoc/master
Solutions from wanderdoc. Happy New Year!
| -rw-r--r-- | challenge-041/wanderdoc/R/ch-01.R | 80 | ||||
| -rw-r--r-- | challenge-041/wanderdoc/R/ch-02.R | 29 | ||||
| -rw-r--r-- | challenge-041/wanderdoc/perl5/ch-01.pl | 99 | ||||
| -rw-r--r-- | challenge-041/wanderdoc/perl5/ch-02.pl | 43 |
4 files changed, 251 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 diff --git a/challenge-041/wanderdoc/perl5/ch-01.pl b/challenge-041/wanderdoc/perl5/ch-01.pl new file mode 100644 index 0000000000..c49ee37479 --- /dev/null +++ b/challenge-041/wanderdoc/perl5/ch-01.pl @@ -0,0 +1,99 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +Write a script to display attractive number between 1 and 50. + A number is an attractive number if the number of its prime factors is also prime number. +=cut + + + + + + +my $N = shift || 50; # or die "How much is the number?\n"; + +for ( 1 .. $N ) +{ + print_attractive($_); +} + +=output +4 = 2 * 2 +6 = 2 * 3 +8 = 2 * 2 * 2 +9 = 3 * 3 +10 = 2 * 5 +12 = 2 * 2 * 3 +14 = 2 * 7 +15 = 3 * 5 +18 = 2 * 3 * 3 +20 = 2 * 2 * 5 +21 = 3 * 7 +22 = 2 * 11 +25 = 5 * 5 +26 = 2 * 13 +27 = 3 * 3 * 3 +28 = 2 * 2 * 7 +30 = 2 * 3 * 5 +32 = 2 * 2 * 2 * 2 * 2 +33 = 3 * 11 +34 = 2 * 17 +35 = 5 * 7 +38 = 2 * 19 +39 = 3 * 13 +42 = 2 * 3 * 7 +44 = 2 * 2 * 11 +45 = 3 * 3 * 5 +46 = 2 * 23 +48 = 2 * 2 * 2 * 2 * 3 +49 = 7 * 7 +50 = 2 * 5 * 5 +=cut + +sub print_attractive +{ + my $n = $_[0]; + + my $n_orig = $n; + my $counter = 0; + + my @factors; + + # Old nice one-liner for prime factorization: + # perl -le "$x = shift; for $k (2 .. $x/2) {$x%$k or (print $k and $x /= $k and redo) }" + + + for my $k ( 2 .. $n/2 ) + { + 0 == $n % $k and + $factors[$counter++] = $k and + $n /= $k and redo; + } + print join(" ", $n_orig, '=', join(' * ',@factors)), $/ if is_prime($counter); +} + + +# https://en.wikipedia.org/wiki/Primality_test + +sub is_prime +{ + my $n = $_[0]; + if ($n <= 3) {return $n > 1 ? 1 : 0;} + elsif (0 == $n % 2 or 0 == $n % 3) {return 0;} + + else + { + my $i = 5; + while ( $i * $i <= $n ) + { + if ( 0 == $n % $i or 0 == $n % ($i + 2)) + { + return 0; + } + $i += 6; + } + } + return 1; +}
\ No newline at end of file diff --git a/challenge-041/wanderdoc/perl5/ch-02.pl b/challenge-041/wanderdoc/perl5/ch-02.pl new file mode 100644 index 0000000000..3a9532d5a0 --- /dev/null +++ b/challenge-041/wanderdoc/perl5/ch-02.pl @@ -0,0 +1,43 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +Write a script to display first 20 Leonardo Numbers. + +a(n) = 2 * Fibonacci(n+1) - 1. - Richard L. Ollerton Mar 22 2002. +https://oeis.org/A001595 + +First 20 Leonardo numbers (first being No.0): +1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891 +=cut + +# use bigint; # If you will 500 numbers or so :-). + +my $fn = shift || 20; # or die "How much numbers?\n"; +print join(" ", map 2 * fib($_ + 1) - 1, 0 .. 20), $/; + +=output +1 1 3 5 9 15 25 41 67 109 177 287 465 753 1219 1973 3193 5167 8361 13529 21891 +=cut + + +# Algorithm found at: +# https://stackoverflow.com/questions/494594 + +sub fib +{ + my $n = shift; + my $x = 0; + my $y = 1; + + my $m = 0; + while ( $m < $n ) + { + ($x, $y) = ($y, $x + $y); + $m++; + + } + + return $x; +} |
