def count_prime(n, prime, factors): count = 0 while n % prime == 0: count += 1 n //= prime if count: factors.append((prime, count)) return n, factors def prime_factorization(n): factors = [] n, factors = count_prime(n, 2, factors) prime = 3 while prime * prime <= n: n, factors = count_prime(n, prime, factors) prime += 2 if n > 1: factors.append((n, 1)) return factors
- def count_prime(n, prime, factors):
- count = 0
- while n % prime == 0:
- count += 1
- n //= prime
- if count:
- factors.append((prime, count))
- return n, factors
- def prime_factorization(n):
- factors = []
cnt = 0while n % 2 == 0: cnt += 1; n //= 2if cnt: factors.append((2, cnt))i = 3while i * i <= n:cnt = 0while n % i == 0: cnt += 1; n //= iif cnt: factors.append((i, cnt))i += 2if n > 1: factors.append((n, 1))- n, factors = count_prime(n, 2, factors)
- prime = 3
- while prime * prime <= n:
- n, factors = count_prime(n, prime, factors)
- prime += 2
- if n > 1:
- factors.append((n, 1))
- return factors
Using regexp makes it simpler
import re class Disemvowel: def __init__(self, s): self.s = s def scalpel(self): return re.sub(r'[AEIOU]', '', self.s, flags=re.IGNORECASE)
- import re
- class Disemvowel:
- def __init__(self, s):
- self.s = s
- def scalpel(self):
return ("".join(x for x in self.s if x.lower() not in "aeiou"))- return re.sub(r'[AEIOU]', '', self.s, flags=re.IGNORECASE)