aboutsummaryrefslogtreecommitdiff
path: root/challenge-159
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-04-08 04:53:08 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-04-08 04:53:08 +0100
commit836d362d9680b42677b0b472e4fc24cf0a24dc96 (patch)
tree8d6981fcc553825a9e3ecf4983b34dfc3d97eb22 /challenge-159
parent92bee2e691cf2967a56fb8b58c85a07495cf6320 (diff)
downloadperlweeklychallenge-club-836d362d9680b42677b0b472e4fc24cf0a24dc96.tar.gz
perlweeklychallenge-club-836d362d9680b42677b0b472e4fc24cf0a24dc96.tar.bz2
perlweeklychallenge-club-836d362d9680b42677b0b472e4fc24cf0a24dc96.zip
- Added solution by Robert DiCicco.
Diffstat (limited to 'challenge-159')
-rw-r--r--challenge-159/robert-dicicco/raku/ch-2.raku45
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-159/robert-dicicco/raku/ch-2.raku b/challenge-159/robert-dicicco/raku/ch-2.raku
new file mode 100644
index 0000000000..281acf5bfc
--- /dev/null
+++ b/challenge-159/robert-dicicco/raku/ch-2.raku
@@ -0,0 +1,45 @@
+use v6;
+
+use Prime::Factor;
+
+# AUTHOR: Robert DiCicco
+# DATE: 7-APR-2022
+# Challenge 159 Moebius Number ( Raku )
+
+sub SquareFree (@arr) {
+ my %dvals = ();
+
+ for @arr -> $num {
+ %dvals{$num}:exists ?? return(0) !! %dvals{$num} = 1
+ }
+
+ return 1;
+}
+
+sub getPrimeFactorCount (@arr ) {
+ my $sz = @arr.elems;
+
+ # return 1 if even number of factors
+
+ if ($sz % 2 == 0 ) { return(1) }
+
+ # return 0 if odd number of factors
+
+ return(0);
+}
+
+sub MAIN (Int $num) {
+ print("Input: n = $num\n");
+
+ my @arr = prime-factors($num);
+ my $pf = getPrimeFactorCount(@arr);
+ my $sf = SquareFree(@arr);
+
+ if (($pf == 1) && ($sf == 1)) { # has even number of prime factors and is square free
+ print("Output: 1\n");
+ } elsif (($pf == 0) && ($sf == 1)) { # has odd number of prime factors and is square free
+ print("Output: -1\n");
+ } elsif ( $sf == 0 ) { # number is not square free
+ print("Output: 0\n");
+ } else { die "Error!!!"}; # something is wrong!!
+}