diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-12 18:56:29 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-12 18:56:29 +0100 |
| commit | 49f7f459092f538b5468e255ff4e8ebac490d70a (patch) | |
| tree | fa4a94a8207a58a56d4dca98ff22d39bed047f6e /challenge-159/robert-dicicco/ruby | |
| parent | 1711da4f548d3134248cd5e02a13d71762d5e4cb (diff) | |
| parent | 72ba70a96cfd587443c3eaa0f5ba02e3557f4b82 (diff) | |
| download | perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.tar.gz perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.tar.bz2 perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.zip | |
Merge remote-tracking branch 'upstream/master'
# Conflicts:
# challenge-160/paulo-custodio/Makefile
Diffstat (limited to 'challenge-159/robert-dicicco/ruby')
| -rw-r--r-- | challenge-159/robert-dicicco/ruby/ch-1.rb | 36 | ||||
| -rw-r--r-- | challenge-159/robert-dicicco/ruby/ch-2.rb | 53 |
2 files changed, 89 insertions, 0 deletions
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) |
