From ff716d1f4c294f9f0a761b505a2890ef4927d8ac Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 6 Mar 2021 12:05:56 -0800 Subject: [PATCH 1/6] Rename file --- maths/{iterative_decimal_to_binary.rb => decimal_to_binary.rb} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename maths/{iterative_decimal_to_binary.rb => decimal_to_binary.rb} (100%) diff --git a/maths/iterative_decimal_to_binary.rb b/maths/decimal_to_binary.rb similarity index 100% rename from maths/iterative_decimal_to_binary.rb rename to maths/decimal_to_binary.rb From cef59e1094f9b416e7a53b6ad87ba17e73261940 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 6 Mar 2021 12:16:43 -0800 Subject: [PATCH 2/6] Add recursive approach --- maths/decimal_to_binary.rb | 41 +++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/maths/decimal_to_binary.rb b/maths/decimal_to_binary.rb index 918a7d4..0feb888 100644 --- a/maths/decimal_to_binary.rb +++ b/maths/decimal_to_binary.rb @@ -1,17 +1,52 @@ -# Iterative method to convert a given decimal number into binary. +# Convert a given decimal number into binary. + +# +# Approach 1: Iterative +# def decimal_to_binary(n) - bin = [] + bin = [] + until n.zero? bin << n % 2 - n = n / 2 + n /= 2 end + bin.reverse.join end puts 'Binary value of 4 is ' + decimal_to_binary(4).to_s # Binary value of 4 is 100 + puts 'Binary value of 31 is ' + decimal_to_binary(31).to_s # Binary value of 31 is 11111 + +puts 'Binary value of 64 is ' + decimal_to_binary(64).to_s +# Binary value of 64 is 1000000 + +# +# Approach 2: Recursive +# + +def decimal_to_binary(d) + binary = (d % 2).to_s + + if d == 0 + return binary + elsif d == 1 + return 1.to_s + else + binary += decimal_to_binary(d / 2).to_s + end + + binary.to_i +end + +puts 'Binary value of 4 is ' + decimal_to_binary(4).to_s +# Binary value of 4 is 100 + +puts 'Binary value of 31 is ' + decimal_to_binary(31).to_s +# Binary value of 31 is 11111 + puts 'Binary value of 64 is ' + decimal_to_binary(64).to_s # Binary value of 64 is 1000000 From 06724c2502ae057ade021adc0df3899f70051426 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 6 Mar 2021 20:17:04 +0000 Subject: [PATCH 3/6] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index e3f395f..f46c03d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -37,7 +37,7 @@ * [Binary To Decimal](https://github.com/TheAlgorithms/Ruby/blob/master/maths/binary_to_decimal.rb) * [Ceil](https://github.com/TheAlgorithms/Ruby/blob/master/maths/ceil.rb) * [Ceil Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/ceil_test.rb) - * [Iterative Decimal To Binary](https://github.com/TheAlgorithms/Ruby/blob/master/maths/iterative_decimal_to_binary.rb) + * [Decimal To Binary](https://github.com/TheAlgorithms/Ruby/blob/master/maths/decimal_to_binary.rb) * [Number Of Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/number_of_digits.rb) * [Square Root](https://github.com/TheAlgorithms/Ruby/blob/master/maths/square_root.rb) * [Square Root Test](https://github.com/TheAlgorithms/Ruby/blob/master/maths/square_root_test.rb) From f1051324d063de684355cbb4544d104974c88dee Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 6 Mar 2021 12:18:27 -0800 Subject: [PATCH 4/6] Update decimal_to_binary.rb --- maths/decimal_to_binary.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/decimal_to_binary.rb b/maths/decimal_to_binary.rb index 0feb888..40dce3b 100644 --- a/maths/decimal_to_binary.rb +++ b/maths/decimal_to_binary.rb @@ -24,6 +24,7 @@ puts 'Binary value of 31 is ' + decimal_to_binary(31).to_s puts 'Binary value of 64 is ' + decimal_to_binary(64).to_s # Binary value of 64 is 1000000 + # # Approach 2: Recursive # From 4df383c9023b1b30a0ab675a38b39a8e545d1bbb Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sat, 6 Mar 2021 12:29:31 -0800 Subject: [PATCH 5/6] Fix issue --- maths/decimal_to_binary.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/maths/decimal_to_binary.rb b/maths/decimal_to_binary.rb index 40dce3b..839a656 100644 --- a/maths/decimal_to_binary.rb +++ b/maths/decimal_to_binary.rb @@ -24,7 +24,6 @@ puts 'Binary value of 31 is ' + decimal_to_binary(31).to_s puts 'Binary value of 64 is ' + decimal_to_binary(64).to_s # Binary value of 64 is 1000000 - # # Approach 2: Recursive # @@ -32,13 +31,10 @@ puts 'Binary value of 64 is ' + decimal_to_binary(64).to_s def decimal_to_binary(d) binary = (d % 2).to_s - if d == 0 - return binary - elsif d == 1 - return 1.to_s - else - binary += decimal_to_binary(d / 2).to_s - end + return binary if d == 0 + return 1.to_s if d == 1 + + binary = decimal_to_binary(d / 2).to_s + binary binary.to_i end From fafb2f0c18bdb87b84bf3d84746ba1e1913ba0d8 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Sun, 7 Mar 2021 09:26:28 -0800 Subject: [PATCH 6/6] keep the return consistent in recursive and iterative --- maths/decimal_to_binary.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/decimal_to_binary.rb b/maths/decimal_to_binary.rb index 839a656..fa0110d 100644 --- a/maths/decimal_to_binary.rb +++ b/maths/decimal_to_binary.rb @@ -36,7 +36,7 @@ def decimal_to_binary(d) binary = decimal_to_binary(d / 2).to_s + binary - binary.to_i + binary end puts 'Binary value of 4 is ' + decimal_to_binary(4).to_s