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/ch-2.rb | |
| 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/ch-2.rb')
| -rw-r--r-- | challenge-159/robert-dicicco/ruby/ch-2.rb | 53 |
1 files changed, 53 insertions, 0 deletions
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) |
