-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathint2limbs.py
33 lines (27 loc) · 842 Bytes
/
int2limbs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def int_to_limbs(n, base):
"""
Converts an integer to its limbs (digits in a given base).
Args:
- n: the integer to convert
- base: the base in which to represent the integer
Returns:
A list of limbs (digits) representing the integer in the given base.
"""
limbs = []
while n > 0:
n, remainder = divmod(n, base)
limbs.append(remainder)
return limbs[::-1]
def limbs_to_int(limbs, base):
"""
Converts a list of limbs (digits in a given base) to an integer.
Args:
- limbs: a list of digits representing the integer in the given base
- base: the base in which the integer is represented
Returns:
An integer representing the value of the given limbs in the given base.
"""
n = 0
for digit in limbs:
n = base * n + digit
return n