From ea530bc5cf89e42ab0175eec72b2ebccfa3fdad4 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 2 Feb 2022 17:16:00 +0000 Subject: - Added solutions by Robert DiCicco. --- challenge-150/robert-dicicco/ruby/ch-2.rb | 47 +++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 challenge-150/robert-dicicco/ruby/ch-2.rb (limited to 'challenge-150/robert-dicicco/ruby/ch-2.rb') diff --git a/challenge-150/robert-dicicco/ruby/ch-2.rb b/challenge-150/robert-dicicco/ruby/ch-2.rb new file mode 100644 index 0000000000..970e737cb0 --- /dev/null +++ b/challenge-150/robert-dicicco/ruby/ch-2.rb @@ -0,0 +1,47 @@ +#!ruby.exe + +# Author: Robert DiCicco +# Date: 31-JAN-2022 +# Challenge 150 Square-Free Integer (Ruby) + +fsums = [] +LIMIT = 500 + +def factors(num) + arr = [] + (1..num).each do |f| + if num % f == 0 + arr.push(f) + end + end + + return arr +end + +def checkFactors(f) + f.each do |p| + next if p == 1 + if IsPerfectSquare(p) == true + return false + else + next + end + end + + return true +end + +def IsPerfectSquare(num) + Integer.sqrt(num) ** 2 == num +end + +(1..LIMIT).each do |num| + facarr = factors(num) + retval = checkFactors(facarr) + + if retval + fsums.push(num) + end +end + +puts "#{fsums}" -- cgit