From efda604e0bc8861584a5c6c473eb0b04f7321bc9 Mon Sep 17 00:00:00 2001 From: Abhay Pandey <116704975+Aloneking789@users.noreply.github.com> Date: Wed, 4 Oct 2023 07:33:23 +0530 Subject: [PATCH] Create Base_minus2.py Fixes #9588 This code defines a decimal_to_base_minus_2 function that takes a decimal number as input and converts it to base -2. --- Base_minus2.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Base_minus2.py diff --git a/Base_minus2.py b/Base_minus2.py new file mode 100644 index 000000000000..4241033ab726 --- /dev/null +++ b/Base_minus2.py @@ -0,0 +1,22 @@ +def decimal_to_base_minus_2(n): + if n == 0: + return "0" + + result = "" + + while n != 0: + remainder = n % (-2) + n = -(n // (-2)) + + if remainder < 0: + remainder += 2 + n += 1 + + result = str(remainder) + result + + return result + +# Example usage: +decimal_number = 13 +base_minus_2 = decimal_to_base_minus_2(decimal_number) +print(f"{decimal_number} in base -2 is {base_minus_2}")