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 | |
| 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')
| -rw-r--r-- | challenge-159/robert-dicicco/julia/ch-2.jl | 63 | ||||
| -rw-r--r-- | challenge-159/robert-dicicco/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-159/robert-dicicco/perl/ch-2.pl | 46 | ||||
| -rw-r--r-- | challenge-159/robert-dicicco/raku/ch-1.raku | 34 | ||||
| -rw-r--r-- | challenge-159/robert-dicicco/raku/ch-2.raku | 45 | ||||
| -rw-r--r-- | challenge-159/robert-dicicco/ruby/ch-1.rb | 36 | ||||
| -rw-r--r-- | challenge-159/robert-dicicco/ruby/ch-2.rb | 53 |
7 files changed, 315 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/perl/ch-1.pl b/challenge-159/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..fb7298bc20 --- /dev/null +++ b/challenge-159/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!perl.exe + +use strict; +use warnings; +use Number::Fraction qw/:constants/; + +# Author: Robert DiCicco +# DATE: 4-APR-2022 +# Challenge 159 Farey Sequence ( Perl ) + +my $n = $ARGV[0]; # command line arg, no error check ! + +my %equivs; # hash arrray to hold fraction and its decimal equivalent +my $LOWER_LIMIT = '0' . '/' . '1'; # lower limit in the output '0/1' +my $UPPER_LIMIT = '1' . '/' . '1'; # upper limit in the output '1/1' + +for (1..($n-1)) { # get numerators we will work with + my $frac = Number::Fraction->new($_, $n); # create fraction $_/$n + $equivs{$frac} = ($_ / $n); # store decimal value in hash keyed by fraction +} + +my $d = $n-1; +while($d) { # create all other fractions with denoms 1 .. n-1 + for my $num (1..($d-1)) { + my $frac = Number::Fraction->new($num, $d); + $equivs{$frac} = ($num / $d); # store decimal value in hash keyed by fraction + } + $d--; +} + +# print hash sorted by decimal values, displayed by fraction value +print("Input: n = $n\n"); +print("$LOWER_LIMIT "); +foreach my $v (sort { $equivs{$a} <=> $equivs{$b} } keys %equivs) { + print("$v "); +} + +print("$UPPER_LIMIT\n"); diff --git a/challenge-159/robert-dicicco/perl/ch-2.pl b/challenge-159/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..fbc177653f --- /dev/null +++ b/challenge-159/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!perl.exe + +use strict; +use warnings; +use feature qw/say/; +use ntheory qw/factor is_prime is_square_free/; + +# AUTHOR: Robert DiCicco +# DATE: 4-APR-2022 +# Challenge 159 Moebius Number ( Perl ) + +sub checkSquareFree { # return 1 if number is squae free, 0 if not + my $num = shift; + if(is_square_free($num)) { + return(1); + } else { + return(0); + } +} + +sub getPrimeFactorCount { # Get prime factor count + my $num = shift; + + my @arr = factor($num); + my $sz = scalar(@arr); + if($sz % 2 == 0){ + return(1); # return 1 if even + } + + return(0); # return 0 if not +} + +my $n = $ARGV[0]; # command line arg, no checking! + +my $sf = checkSquareFree($n); +my $pf = getPrimeFactorCount($n); + +print("Input: n = $n\n"); + +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!! diff --git a/challenge-159/robert-dicicco/raku/ch-1.raku b/challenge-159/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..cd3b2f4e92 --- /dev/null +++ b/challenge-159/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,34 @@ +use v6; + +use Lingua::EN::Numbers; + +# AUTHOR: Robert DiCicco +# DATE: 7-APR-2022 +# Challenge 159 Farey Sequence ( Raku ) + +my %equivs; + +sub MAIN(Int $num) { + my $d = $num; + + while $d { + for (1..($d-1)) { + my $frac = pretty-rat($_/$d); + %equivs{$frac} = $_/$d; + #say $frac; + } + + $d--; + } + + print("Input: \$n = $num\n"); + print '0/1 '; + + for %equivs.sort(*.value) { + my $k = $_.key; + print("$k "); + } + + print '1/1'; + say "\n"; +} 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!! +} 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) |
