We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 1243138 commit 0a43d07Copy full SHA for 0a43d07
Base(-2)_conversion.py
@@ -0,0 +1,22 @@
1
+def decimal_to_base_minus_2(n):
2
+ if n == 0:
3
+ return "0"
4
+
5
+ result = ""
6
7
+ while n != 0:
8
+ remainder = n % (-2)
9
+ n = -(n // (-2))
10
11
+ if remainder < 0:
12
+ remainder += 2
13
+ n += 1
14
15
+ result = str(remainder) + result
16
17
+ return result
18
19
+# Example usage:
20
+decimal_number = 13
21
+base_minus_2 = decimal_to_base_minus_2(decimal_number)
22
+print(f"{decimal_number} in base -2 is {base_minus_2}")
0 commit comments