aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-04-07 05:19:00 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-04-07 05:19:00 +0100
commitb64cdddfba756e72ed2dc1731bb06d575fe6e12d (patch)
treecc359fa63680357fd72df63d2846e91ab5a0c89f
parent7d2ce8f79ac768b7a437d569fd7b19551936f429 (diff)
downloadperlweeklychallenge-club-b64cdddfba756e72ed2dc1731bb06d575fe6e12d.tar.gz
perlweeklychallenge-club-b64cdddfba756e72ed2dc1731bb06d575fe6e12d.tar.bz2
perlweeklychallenge-club-b64cdddfba756e72ed2dc1731bb06d575fe6e12d.zip
- Added guest contributions by Robert DiCicco.
-rw-r--r--challenge-159/robert-dicicco/julia/ch-2.jl63
-rw-r--r--challenge-159/robert-dicicco/ruby/ch-1.rb36
-rw-r--r--challenge-159/robert-dicicco/ruby/ch-2.rb53
3 files changed, 152 insertions, 0 deletions
diff --git a/challenge-159/robert-dicicco/julia/ch-2.jl b/challenge-159/robert-dicicco/julia/ch-2.jl
new file mode 100644
index 0000000000..67e80bf7d2
--- /dev/null
+++ b/challenge-159/robert-dicicco/julia/ch-2.jl
@@ -0,0 +1,63 @@
+#!julia.exe
+
+using Printf
+using Primes
+
+# AUTHOR: Robert DiCicco
+# DATE: 6-APR-2022
+# Challenge 159 Moebius Number ( Julia )
+
+const sqprimes = map(x -> x * x, primes(2, 100))
+possdivisorsfor(n) = vcat(filter(x -> x <= n / 2, sqprimes), n in sqprimes ? n : [])
+issquarefree(n) = all(x -> floor(n / x) != n / x, possdivisorsfor(n))
+
+function checkArgs(args)
+ global num
+ try
+ num = parse(Int64, args[1])
+ catch
+ println("Error: Argument must be an integer")
+ exit(0)
+ finally
+ main(num)
+ end
+end
+
+function getPrimeFactorCount(n)
+ pf = factor(Array, n )
+ sz = size(pf)
+ cnt = mod(count(i->(i>0), pf),2)
+
+ if cnt == 0
+ return 1
+ end
+
+ return 0
+end
+
+function main(num)
+ println("Input: $num")
+
+ sf = issquarefree(num)
+
+ if (sf)
+ sf = 1
+ else
+ sf = 0
+ end
+
+ pfc = getPrimeFactorCount(num)
+
+ if ((pfc == 1) && (sf == 1))
+ println("Output: 1\n")
+ elseif ((pfc == 0) && (sf == 1))
+ println("Output: -1\n")
+ elseif ( sf == 0)
+ println("Output: 0\n")
+ else
+ print("Error!!!\n")
+ end
+
+end
+
+checkArgs(ARGS)
diff --git a/challenge-159/robert-dicicco/ruby/ch-1.rb b/challenge-159/robert-dicicco/ruby/ch-1.rb
new file mode 100644
index 0000000000..baaaef6b36
--- /dev/null
+++ b/challenge-159/robert-dicicco/ruby/ch-1.rb
@@ -0,0 +1,36 @@
+#!ruby.exe
+
+# AUTHOR: Robert DiCicco
+# DATE: 5-APR-2002
+# Challenge 159 Farey Sequence ( Ruby )
+
+eqvals = Array.new
+fracvals = Array.new
+
+n = ARGV[0].to_i
+
+(0..(n)).each { | numerator |
+ decval = sprintf("%.2f", numerator.to_f/n.to_f)
+ eqvals = eqvals.push(decval) # this saves decimal values of fractions (0/n .. n/n)
+}
+
+d = n-1 # create decimal values 1/d .. ((d-1) / d)
+
+while d > 0
+ (1..(d-1)).each { | num |
+ decval = sprintf("%.2f", num.to_f/d.to_f)
+ eqvals = eqvals.push(decval)
+ }
+
+ d = d - 1
+end
+
+eqvals.sort.each do |i| # sort the decimal values
+ fracvals = fracvals.push(i.to_r.rationalize(0.005)) # put their fractional rep in another array
+end
+
+fracvals.sort.uniq.each do | val | # sort the fraction array and guarantee unique entries
+ print "#{val} " # and print em
+end
+
+puts ' '
diff --git a/challenge-159/robert-dicicco/ruby/ch-2.rb b/challenge-159/robert-dicicco/ruby/ch-2.rb
new file mode 100644
index 0000000000..b5b30e5c2f
--- /dev/null
+++ b/challenge-159/robert-dicicco/ruby/ch-2.rb
@@ -0,0 +1,53 @@
+#!ruby.exe
+
+require 'prime'
+
+# AUTHOR: Robert DiCicco
+# DATE: 6-APR-2022
+# Challenge 159 Moebius Number ( Ruby )
+
+def prime_factorization(n)
+ Prime.prime_division(n).flat_map { |factor, power| [factor] * power }
+end
+
+def checkSquareFree(n)
+ Prime.prime_division(n).each do |i|
+ if i[1] > 1
+ return 0
+ end
+ end
+
+ return 1
+end
+
+def getPrimeFactorCount(n)
+ a = prime_factorization(n)
+ x = a.size % 2
+
+ if x == 0
+ return 1
+ else
+ return 0
+ end
+end
+
+def showResults(pf,sf)
+ if ((pf == 1) && (sf == 1))
+ print("Output: 1\n")
+ elsif ((pf == 0) && (sf == 1))
+ print("Output: -1\n")
+ elsif ( sf == 0 )
+ print("Output: 0\n")
+ else
+ print("Error!!!\n")
+ end
+end
+
+n = ARGV[0].to_i
+
+print("Input: #{n}\n")
+
+sf = checkSquareFree(n)
+pf = getPrimeFactorCount(n)
+
+showResults(pf,sf)