本文整理汇总了Python中fractions.gcd方法的典型用法代码示例。如果您正苦于以下问题:Python fractions.gcd方法的具体用法?Python fractions.gcd怎么用?Python fractions.gcd使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fractions
的用法示例。
在下文中一共展示了fractions.gcd方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __new__
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import gcd [as 别名]
def __new__(cls, name, bases, attrs):
# A list of all functions which is marked as 'is_cronjob=True'
cron_jobs = []
# The min_tick is the greatest common divisor(GCD) of the interval of cronjobs
# this value would be queried by scheduler when the project initial loaded.
# Scheudler may only send _on_cronjob task every min_tick seconds. It can reduce
# the number of tasks sent from scheduler.
min_tick = 0
for each in attrs.values():
if inspect.isfunction(each) and getattr(each, 'is_cronjob', False):
cron_jobs.append(each)
min_tick = fractions.gcd(min_tick, each.tick)
newcls = type.__new__(cls, name, bases, attrs)
newcls._cron_jobs = cron_jobs
newcls._min_tick = min_tick
return newcls
示例2: exgcd
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import gcd [as 别名]
def exgcd(a,b):
"""
Bézout coefficients (u,v) of (a,b) as:
a*u + b*v = gcd(a,b)
Result is the tuple: (u, v, gcd(a,b)). Examples:
>>> exgcd(7*3, 15*3)
(-2, 1, 3)
>>> exgcd(24157817, 39088169) #sequential Fibonacci numbers
(-14930352, 9227465, 1)
Algorithm source: Pierre L. Douillet
http://www.douillet.info/~douillet/working_papers/bezout/node2.html
"""
u, v, s, t = 1, 0, 0, 1
while b !=0:
q, r = divmod(a,b)
a, b = b, r
u, s = s, u - q*s
v, t = t, v - q*t
return (u, v, a)
示例3: calculate_alloc_stats
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import gcd [as 别名]
def calculate_alloc_stats(self):
"""Calculates the minimum_size and alignment_gcd to determine "virtual allocs" when read lengths of data
It's particularly important to cast all numbers to ints, since they're used a lot and object take effort to reread.
"""
available_allocs = list(self.get_available_allocs())
self.minimum_size = int(min([size for _, size in available_allocs]))
accumulator = self.minimum_size
for start, _ in available_allocs:
if accumulator is None and start > 1:
accumulator = start
if accumulator and start > 0:
accumulator = fractions.gcd(accumulator, start)
self.alignment_gcd = int(accumulator)
# Pick an arbitrary cut-off that'll lead to too many reads
if self.alignment_gcd < 0x4:
debug.warning("Alignment of " + self.__class__.__name__ + " is too small, plugins will be extremely slow")
示例4: testMisc
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import gcd [as 别名]
def testMisc(self):
# fractions.gcd() is deprecated
with self.assertWarnsRegex(DeprecationWarning, r'fractions\.gcd'):
gcd(1, 1)
with warnings.catch_warnings():
warnings.filterwarnings('ignore', r'fractions\.gcd',
DeprecationWarning)
self.assertEqual(0, gcd(0, 0))
self.assertEqual(1, gcd(1, 0))
self.assertEqual(-1, gcd(-1, 0))
self.assertEqual(1, gcd(0, 1))
self.assertEqual(-1, gcd(0, -1))
self.assertEqual(1, gcd(7, 1))
self.assertEqual(-1, gcd(7, -1))
self.assertEqual(1, gcd(-23, 15))
self.assertEqual(12, gcd(120, 84))
self.assertEqual(-12, gcd(84, -120))
self.assertEqual(gcd(120.0, 84), 12.0)
self.assertEqual(gcd(120, 84.0), 12.0)
self.assertEqual(gcd(F(120), F(84)), F(12))
self.assertEqual(gcd(F(120, 77), F(84, 55)), F(12, 385))
示例5: generate_indices
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import gcd [as 别名]
def generate_indices(max_index):
"""Return an array of miller indices enumerated up to values
plus or minus some maximum. Filters out lists with greatest
common divisors greater than one. Only positive values need to
be considered for the first index.
Parameters
----------
max_index : int
Maximum number that will be considered for a given surface.
Returns
-------
unique_index : ndarray (n, 3)
Unique miller indices
"""
grid = np.mgrid[max_index:-1:-1,
max_index:-max_index-1:-1,
max_index:-max_index-1:-1]
index = grid.reshape(3, -1)
gcd = utils.list_gcd(index)
unique_index = index.T[np.where(gcd == 1)]
return unique_index
示例6: rotate
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import gcd [as 别名]
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: None Do not return anything, modify nums in-place instead.
"""
import fractions
if len(nums) == 0 or k == 0 or k == len(nums):
return
gcd = fractions.gcd(len(nums), k)
for i in xrange(gcd):
runner = i
num = nums[runner]
while True:
next_idx = (runner + k) % len(nums)
tmp = nums[next_idx]
nums[next_idx] = num
num = tmp
runner = next_idx
if runner == i:
break
示例7: _initialize_mtf_dimension_name_to_size_gcd
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import gcd [as 别名]
def _initialize_mtf_dimension_name_to_size_gcd(self, mtf_graph):
"""Initializer for self._mtf_dimension_name_to_size_gcd.
Args:
mtf_graph: an mtf.Graph.
Returns:
A {string: int}, mapping the name of an MTF dimension to the greatest
common divisor of all the sizes it has. All these sizes being evenly
divisible by some x is equivalent to the GCD being divisible by x.
"""
mtf_dimension_name_to_size_gcd = {}
for mtf_operation in mtf_graph.operations:
for mtf_tensor in mtf_operation.outputs:
for mtf_dimension in mtf_tensor.shape.dims:
mtf_dimension_name_to_size_gcd[mtf_dimension.name] = fractions.gcd(
mtf_dimension_name_to_size_gcd.get(mtf_dimension.name,
mtf_dimension.size),
mtf_dimension.size)
return mtf_dimension_name_to_size_gcd
示例8: gcd
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import gcd [as 别名]
def gcd(*values: int) -> int:
"""
Return the greatest common divisor of a series of ints
:param values: The values of which to compute the GCD
:return: The GCD
"""
assert len(values) > 0
# 3.5 moves fractions.gcd to math.gcd
if sys.version_info.major == 3 and sys.version_info.minor < 5:
import fractions
return reduce(fractions.gcd, values)
else:
return reduce(math.gcd, values)
示例9: lcm
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import gcd [as 别名]
def lcm(*values: int) -> int:
"""
Return the least common multiple of a series of ints
:param values: The values of which to compute the LCM
:return: The LCM
"""
assert len(values) > 0
# 3.5 moves fractions.gcd to math.gcd
if sys.version_info.major == 3 and sys.version_info.minor < 5:
import fractions
return reduce(lambda x, y: (x * y) // fractions.gcd(x, y), values)
else:
return reduce(lambda x, y: (x * y) // math.gcd(x, y), values)
示例10: _lcm
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import gcd [as 别名]
def _lcm(self, a, b):
return a * b // fractions.gcd(a, b)
示例11: __init__
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import gcd [as 别名]
def __init__(self, target, rand=True, state=None):
# see https://fr.wikipedia.org/wiki/Générateur_congruentiel_linéaire
self.target = target
self.nextcount = 0
self.lcg_m = target.targetscount
if state is not None:
self.previous = state[0]
self.lcg_c = state[1]
self.lcg_a = state[2]
self.nextcount = state[3]
elif rand and target.targetscount > 1:
# X_{-1}
self.previous = random.randint(0, self.lcg_m - 1)
# GCD(c, m) == 1
self.lcg_c = random.randint(1, self.lcg_m - 1)
# pylint: disable=deprecated-method
while gcd(self.lcg_c, self.lcg_m) != 1:
self.lcg_c = random.randint(1, self.lcg_m - 1)
# a - 1 is divisible by all prime factors of m
mfactors = reduce(mul, set(mathutils.factors(self.lcg_m)))
# a - 1 is a multiple of 4 if m is a multiple of 4.
if self.lcg_m % 4 == 0:
mfactors *= 2
self.lcg_a = mfactors + 1
else:
self.previous = self.lcg_m - 1
self.lcg_a = 1
self.lcg_c = 1
示例12: multiples_of_a_divisors_of_b
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import gcd [as 别名]
def multiples_of_a_divisors_of_b(lcm, gcd):
result = []
multiplyer = 1
while (lcm * multiplyer <= gcd):
if (gcd % (lcm * multiplyer) == 0):
result.append(lcm * multiplyer)
multiplyer += 1
return result
# Import gcd and lcm from https://gist.github.com/endolith/114336
示例13: gcd
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import gcd [as 别名]
def gcd(*numbers):
"""Return the greatest common divisor of the given integers"""
from fractions import gcd
return reduce(gcd, numbers)
示例14: lcm
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import gcd [as 别名]
def lcm(*numbers):
"""Return lowest common multiple."""
def lcm(a, b):
return (a * b) // gcd(a, b)
return reduce(lcm, numbers, 1)
示例15: __init__
# 需要导入模块: import fractions [as 别名]
# 或者: from fractions import gcd [as 别名]
def __init__(self, num, den):
g = gcd(num, den)
self.num = num // g
self.den = den // g