aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-01-27 18:51:34 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-01-27 18:51:34 +0000
commit5c51501702cb5f9d214166a516a1181df959df7b (patch)
tree8cf369ec59c0c5d37e42c37ead6542dcb4259098
parent15f56dfbd299e105f66f501e64de84d0569f19aa (diff)
downloadperlweeklychallenge-club-5c51501702cb5f9d214166a516a1181df959df7b.tar.gz
perlweeklychallenge-club-5c51501702cb5f9d214166a516a1181df959df7b.tar.bz2
perlweeklychallenge-club-5c51501702cb5f9d214166a516a1181df959df7b.zip
- Added guest contribution by Robert DiCicco.
-rw-r--r--challenge-149/robert-dicicco/ruby/ch-1.rb48
1 files changed, 48 insertions, 0 deletions
diff --git a/challenge-149/robert-dicicco/ruby/ch-1.rb b/challenge-149/robert-dicicco/ruby/ch-1.rb
new file mode 100644
index 0000000000..912a3b5bb5
--- /dev/null
+++ b/challenge-149/robert-dicicco/ruby/ch-1.rb
@@ -0,0 +1,48 @@
+#!ruby.exe
+
+# Author: Robert DiCicco
+# Date: 26-JAN-2022
+# Challenge 149 Fibonacci Digit Sum (Ruby)
+
+def SingleSumOfDigits (num)
+ digsum = num.digits.reverse.sum
+end
+
+def IsPerfectSquare(num)
+ Integer.sqrt(num) ** 2 == num
+end
+
+def IsFibonacci(n)
+ # n is Fibonacci if one of 5*n*n + 4 or 5*n*n - 4 or both
+ # is a perfect square
+
+ if IsPerfectSquare(5*n*n + 4) then
+ return true
+ end
+
+ if IsPerfectSquare(5*n*n - 4) then
+ return true
+ end
+
+ return false
+end
+
+expected = [0, 1, 2, 3, 5, 8, 10, 11, 12, 14, 17, 20, 21, 23, 26, 30, 32, 35, 41, 44]
+
+cnt=0
+total=0
+arr_output = []
+
+# Get first 20 fibonacci numbers
+while total < 20 do
+ sumval = SingleSumOfDigits(cnt) # get digits sum, then check if fibonacci
+ if(IsFibonacci(sumval)) then
+ arr_output.push(cnt)
+ total += 1 # keep track of how many are output
+ end
+
+ cnt += 1 # bump cnt
+end
+
+puts "Expected: " + expected.to_s + " "
+puts "Output: " + arr_output.to_s + " "