def testGeneratorEratosthenes(self): g = prime.generator_eratosthenes(3) self.assertEqual(2, g.next()) self.assertEqual(3, g.next()) self.assertRaises(StopIteration, g.next) g = prime.generator_eratosthenes(541) self.assertEqual(100, len([p for p in g]))
def _parse_seq(self, options): """ Parse 'options' to define trial sequaence. """ if 'start' in options and 'stop' in options: if 'step' in options: trials = bigrange.range(options['start'], options['stop'], options['step']) else: trials = bigrange.range(options['start'], options['stop']) elif 'iterator' in options: trials = options['iterator'] elif 'eratosthenes' in options: trials = prime.generator_eratosthenes(options['eratosthenes']) elif options['n'] < 1000000: trials = prime.generator_eratosthenes(arith1.floorsqrt(options['n'])) else: trials = prime.generator() return trials
def class_formula(disc, uprbd): """ Return the approximation of class number 'h' with the given discriminant. h = sqrt(|D|)/pi (1 - (D/p)(1/p))^{-1} where p is less than ubound. """ ht = math.sqrt(abs(disc)) / math.pi ml = number_unit(disc) / 2 for factor in prime.generator_eratosthenes(uprbd): ml = ml * (1 - (kronecker(disc, factor) / factor))**(-1) return int(ht * ml + 0.5)
def _parse_seq(self, options): """ Parse 'options' to define trial sequaence. """ if 'start' in options and 'stop' in options: if 'step' in options: trials = bigrange.range(options['start'], options['stop'], options['step']) else: trials = bigrange.range(options['start'], options['stop']) elif 'iterator' in options: trials = options['iterator'] elif 'eratosthenes' in options: trials = prime.generator_eratosthenes(options['eratosthenes']) elif options['n'] < 1000000: trials = prime.generator_eratosthenes( arith1.floorsqrt(options['n'])) else: trials = prime.generator() return trials
def stage1(n, bounds, C, Q): """ ECM stage 1 for factoring n. The upper bound for primes to be tested is bounds.first. It uses curve C and starting point Q. """ for p in _prime.generator_eratosthenes(bounds.first): q = p while q < bounds.first: Q = mul(Q, p, C, n) q *= p g = _gcd.gcd(Q.z, n) _log.debug("Stage 1: %d" % g) return g
def perfect_power_detection(n): """ Return (m, k) if n = m**k. Note that k is the smallest possible and m still can be a perfect power; if n is not a perfect power, it returns (n, 1). """ f = arith1.log(n) + 1 y = DyadicRational(0, n).nroot(1, 3 + (f - 1) // 2 + 1) for p in prime.generator_eratosthenes(f): x = _is_kth_power(n, p, y, f) if x != 0: return (x, p) return (n, 1)
def powerDetection(n, largest_exp=False): """ param positive integer n param boolean largest_exp return integer x, k s.t. n == x ** k (2 <= k if exist else x, k == n, 1) if largest_exp is true then return largest k """ from nzmath.prime import generator_eratosthenes as generator_eratosthenes ge = generator_eratosthenes(log(n, 2)) for exp in ge: power_root, power = floorpowerroot(n, exp, True) if power == n: if largest_exp: x, k = powerDetection(power_root, True) return x, k * exp else: return power_root, exp return n, 1
def trialDivision(n, **options): """ Return a factor of given integer by trial division. options can be either: 1) 'start' and 'stop' as range parameters. 2) 'iterator' as an iterator of primes. If both options are not given, prime factor is searched from 2 to the square root of the given integer. """ # verbosity verbose = options.get('verbose', False) if not verbose: _silence() if 'start' in options and 'stop' in options: if 'step' in options: trials = list( range(options['start'], options['stop'], options['step'])) else: trials = list(range(options['start'], options['stop'])) elif 'iterator' in options: trials = options['iterator'] elif n < 1000000: trials = prime.generator_eratosthenes(arith1.floorsqrt(n)) else: trials = prime.generator() limit = arith1.floorsqrt(n) for p in trials: if limit < p: break if 0 == n % p: if not verbose: _verbose() return p if not verbose: _verbose() return 1
def trialDivision(n, **options): """ Return a factor of given integer by trial division. options can be either: 1) 'start' and 'stop' as range parameters. 2) 'iterator' as an iterator of primes. If both options are not given, prime factor is searched from 2 to the square root of the given integer. """ # verbosity verbose = options.get('verbose', False) if not verbose: _silence() if 'start' in options and 'stop' in options: if 'step' in options: trials = range(options['start'], options['stop'], options['step']) else: trials = range(options['start'], options['stop']) elif 'iterator' in options: trials = options['iterator'] elif n < 1000000: trials = prime.generator_eratosthenes(arith1.floorsqrt(n)) else: trials = prime.generator() for p in trials: if not (n % p): if not verbose: _verbose() return p if p ** 2 > n: break if not verbose: _verbose() return 1