Skip to content

num_utils

Number-theoretic utilities.

This module provides functions for prime factorization, divisor search, sign extension, bit operations, rounding, linear interpolation, and perfect-power detection.

prime_factors(n)

Yield the prime factors of n.

Parameters:

Name Type Description Default
n

A positive integer to factorize. Zero yields nothing.

required

Yields:

Type Description

Prime factors of n in ascending order, with multiplicity.

Example
list(num_utils.prime_factors(360))  # [2, 2, 2, 3, 3, 5]
list(num_utils.prime_factors(13))  # [13]
Source code in py_misc_utils/num_utils.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def prime_factors(n):
  """Yield the prime factors of n.

  Args:
    n: A positive integer to factorize. Zero yields nothing.

  Yields:
    Prime factors of n in ascending order, with multiplicity.

  Example:
    list(num_utils.prime_factors(360))  # [2, 2, 2, 3, 3, 5]
    list(num_utils.prime_factors(13))  # [13]
  """
  i = 2
  while i * i <= n:
    q, r = divmod(n, i)
    if r == 0:
      n = q
      yield i
    else:
      i += 1

  if n > 1:
    yield n

nearest_divisor(value, n)

Find the divisor of a value closest to a target number.

Parameters:

Name Type Description Default
value

The value whose divisors are searched.

required
n

The target number the divisor should be as close as possible to.

required

Returns:

Type Description

The divisor of value nearest to n - ties resolved towards the lower

divisor.

Example
num_utils.nearest_divisor(100, 6)  # 5: the divisor closest to 6.
num_utils.nearest_divisor(24, 5)  # 4: tie (4 vs 6) resolved downwards.
Source code in py_misc_utils/num_utils.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def nearest_divisor(value, n):
  """Find the divisor of a value closest to a target number.

  Args:
    value: The value whose divisors are searched.
    n: The target number the divisor should be as close as possible to.

  Returns:
    The divisor of value nearest to n - ties resolved towards the lower
    divisor.

  Example:
    num_utils.nearest_divisor(100, 6)  # 5: the divisor closest to 6.
    num_utils.nearest_divisor(24, 5)  # 4: tie (4 vs 6) resolved downwards.
  """
  nmin, nmax = n, n

  while nmin > 1:
    if value % nmin == 0:
      break
    nmin -= 1

  dmin = n - nmin
  top_n = min(n + dmin, value // 2)

  while nmax <= top_n:
    if value % nmax == 0:
      break
    nmax += 1

  if value % nmax != 0:
    return nmin

  return nmin if dmin <= (nmax - n) else nmax

sign_extend(value, nbits)

Sign-extend an nbits-wide two's complement value to Python int.

Parameters:

Name Type Description Default
value

The value to extend.

required
nbits

The bit width of the value.

required

Returns:

Type Description

The negative value when the sign bit is set, the value otherwise.

Example
num_utils.sign_extend(0xFF, 8)  # -1
num_utils.sign_extend(0x7F, 8)  # 127
Source code in py_misc_utils/num_utils.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def sign_extend(value, nbits):
  """Sign-extend an nbits-wide two's complement value to Python int.

  Args:
    value: The value to extend.
    nbits: The bit width of the value.

  Returns:
    The negative value when the sign bit is set, the value otherwise.

  Example:
    num_utils.sign_extend(0xFF, 8)  # -1
    num_utils.sign_extend(0x7F, 8)  # 127
  """
  sign = 1 << (nbits - 1)

  return (value & (sign - 1)) - (value & sign)

ffs(n)

Find the index of the least significant set bit (find-first-set).

Parameters:

Name Type Description Default
n

A non-zero integer.

required

Returns:

Type Description

The 0-based index of the lowest set bit of n.

Example
num_utils.ffs(8)  # 3
num_utils.ffs(12)  # 2
Source code in py_misc_utils/num_utils.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def ffs(n):
  """Find the index of the least significant set bit (find-first-set).

  Args:
    n: A non-zero integer.

  Returns:
    The 0-based index of the lowest set bit of n.

  Example:
    num_utils.ffs(8)  # 3
    num_utils.ffs(12)  # 2
  """
  nnorm = n & (-n)

  return nnorm.bit_length() - 1

round_up(v, step)

Round a value up to the next multiple of a step.

Parameters:

Name Type Description Default
v

The value to round.

required
step

The rounding step.

required

Returns:

Type Description

The smallest multiple of step which is greater than or equal to v.

Example
num_utils.round_up(17, 5)  # 20
num_utils.round_down(17, 5)  # 15
Source code in py_misc_utils/num_utils.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def round_up(v, step):
  """Round a value up to the next multiple of a step.

  Args:
    v: The value to round.
    step: The rounding step.

  Returns:
    The smallest multiple of step which is greater than or equal to v.

  Example:
    num_utils.round_up(17, 5)  # 20
    num_utils.round_down(17, 5)  # 15
  """
  return ((v + step - 1) // step) * step

round_down(v, step)

Round a value down to the previous multiple of a step.

Parameters:

Name Type Description Default
v

The value to round.

required
step

The rounding step.

required

Returns:

Type Description

The largest multiple of step which is less than or equal to v.

Source code in py_misc_utils/num_utils.py
127
128
129
130
131
132
133
134
135
136
137
def round_down(v, step):
  """Round a value down to the previous multiple of a step.

  Args:
    v: The value to round.
    step: The rounding step.

  Returns:
    The largest multiple of step which is less than or equal to v.
  """
  return (v // step) * step

mix(a, b, gamma)

Linearly interpolate two values.

Parameters:

Name Type Description Default
a

The value returned when gamma is 1.0.

required
b

The value returned when gamma is 0.0.

required
gamma

The interpolation factor.

required

Returns:

Type Description

a * gamma + b * (1.0 - gamma).

Example
num_utils.mix(10, 20, 0.25)  # 17.5
Source code in py_misc_utils/num_utils.py
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def mix(a, b, gamma):
  """Linearly interpolate two values.

  Args:
    a: The value returned when gamma is 1.0.
    b: The value returned when gamma is 0.0.
    gamma: The interpolation factor.

  Returns:
    a * gamma + b * (1.0 - gamma).

  Example:
    num_utils.mix(10, 20, 0.25)  # 17.5
  """
  return a * gamma + b * (1.0 - gamma)

perfect_power(value, base)

Return the exponent when value is a power of base.

Parameters:

Name Type Description Default
value

The value to check.

required
base

The base of the power.

required

Returns:

Type Description

The exponent n when value == base**n, or None otherwise.

Example
num_utils.perfect_power(8, 2)  # 3
num_utils.perfect_power(10, 2)  # None
Source code in py_misc_utils/num_utils.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def perfect_power(value, base):
  """Return the exponent when value is a power of base.

  Args:
    value: The value to check.
    base: The base of the power.

  Returns:
    The exponent n when value == base**n, or None otherwise.

  Example:
    num_utils.perfect_power(8, 2)  # 3
    num_utils.perfect_power(10, 2)  # None
  """
  vlog = round(math.log(value, base))

  return vlog if base**vlog == value else None

address_bits(size)

Return the number of bits needed to address a memory size.

Parameters:

Name Type Description Default
size

The number of addressable units.

required

Returns:

Type Description

The number of address bits: 1 for sizes <= 1, the bit length of

size - 1 otherwise.

Source code in py_misc_utils/num_utils.py
176
177
178
179
180
181
182
183
184
185
186
def address_bits(size):
  """Return the number of bits needed to address a memory size.

  Args:
    size: The number of addressable units.

  Returns:
    The number of address bits: 1 for sizes <= 1, the bit length of
    size - 1 otherwise.
  """
  return (size - 1).bit_length() if size > 1 else 1