bool Nand(bool a, bool b) { return 1 - (a * b); } bool Not(bool a) { return Nand(a, a); } bool And(bool a, bool b) { return Not(Nand(a, b)); } bool Nor(bool a, bool b) { return And(Not(a), Not(b)); } bool Or(bool a, bool b) { return Not(Nor(a, b)); } bool Xor(bool a, bool b) { return And(Or(a, b), Not(And(a, b))); } bool Buffer(bool a) { return Not(Not(a)); } bool Xnor(bool a, bool b) { return Not(Xor(a, b)); } bool False(bool a) { return And(a, Not(a)); } bool True(bool a) { return Or(a, Not(a)); }
bool Nand(bool a, bool b){return 1-(a*b);- bool Nand(bool a, bool b) {
- return 1 - (a * b);
- }
bool Not(bool a){return Nand(a, a);- bool Not(bool a) {
- return Nand(a, a);
- }
bool And(bool a, bool b){return Not(Nand(a, b));- bool And(bool a, bool b) {
- return Not(Nand(a, b));
- }
bool Nor(bool a, bool b){return And(Not(a), Not(b));- bool Nor(bool a, bool b) {
- return And(Not(a), Not(b));
- }
bool Or(bool a, bool b){return Not(Nor(a, b));- bool Or(bool a, bool b) {
- return Not(Nor(a, b));
- }
bool Xor(bool a, bool b){return And(Or(a, b), Not(And(a, b)));- bool Xor(bool a, bool b) {
- return And(Or(a, b), Not(And(a, b)));
- }
bool Buffer(bool a){return Not(Not(a));- bool Buffer(bool a) {
- return Not(Not(a));
- }
bool Xnor(bool a, bool b){return Not(Xor(a, b));- bool Xnor(bool a, bool b) {
- return Not(Xor(a, b));
- }
bool False(bool a){return And(a, Not(a));- bool False(bool a) {
- return And(a, Not(a));
- }
bool True(bool a){return Or(a, Not(a));- bool True(bool a) {
- return Or(a, Not(a));
- }
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 = []
- 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 prime_factorization(n): factors = [] cnt = 0 while n % 2 == 0: cnt += 1; n //= 2 if cnt: factors.append((2, cnt)) i = 3 while i * i <= n: cnt = 0 while n % i == 0: cnt += 1; n //= i if cnt: factors.append((i, cnt)) i += 2 if n > 1: factors.append((n, 1)) return factors
- def prime_factorization(n):
- factors = []
- cnt = 0
- while n % 2 == 0: cnt += 1; n //= 2
- if cnt: factors.append((2, cnt))
cnt = 0while n % 3 == 0: cnt += 1; n //= 3if cnt: factors.append((3, cnt))i = 5- i = 3
- while i * i <= n:
- cnt = 0
- while n % i == 0: cnt += 1; n //= i
- if cnt: factors.append((i, cnt))
cnt = 0- i += 2
while n % i == 0: cnt += 1; n //= iif cnt: factors.append((i, cnt))i += 4- if n > 1: factors.append((n, 1))
- return factors
def prime_factorization(input_number): factors = [] for i in range(2, int(input_number**0.5) + 1): while input_number % i == 0: factors.append(i) input_number //= i if input_number > 1: factors.append(input_number) from collections import Counter return sorted(Counter(factors).items())
- def prime_factorization(input_number):
- factors = []
while input_number % 2 == 0:factors.append(2)input_number //= 2for i in range(3, int(input_number**0.5) + 1, 2):- for i in range(2, int(input_number**0.5) + 1):
- while input_number % i == 0:
- factors.append(i)
- input_number //= i
if input_number > 2:- if input_number > 1:
- factors.append(input_number)
- from collections import Counter
factor_count = Counter(factors)return sorted(factor_count.items())- return sorted(Counter(factors).items())
You are tasked with writing a Python function that performs prime factorization on a given integer. The function should return a list of tuples, where each tuple contains a prime factor and its exponent in the factorization.
Requirements:
The function should handle positive integers greater than 1.
The function should return a list of tuples, where each tuple contains a prime factor and its exponent.
The list should be sorted in ascending order based on the prime factors.
Example:
input_number = 84 output_list = prime_factorization(input_number) print(output_list) Expected Output: [(2, 2), (3, 1), (7, 1)]
def prime_factorization(input_number):
factors = []
while input_number % 2 == 0:
factors.append(2)
input_number //= 2
for i in range(3, int(input_number**0.5) + 1, 2):
while input_number % i == 0:
factors.append(i)
input_number //= i
if input_number > 2:
factors.append(input_number)
from collections import Counter
factor_count = Counter(factors)
return sorted(factor_count.items())
import codewars_test as test
import solution # Assuming the solution module contains the function prime_factorization
@test.describe("Prime Factorization Tests")
def test_group():
@test.it("Test with number 84")
def test_case():
input_number = 84
expected_output = [(2, 2), (3, 1), (7, 1)]
test.assert_equals(solution.prime_factorization(input_number), expected_output)
@test.it("Test with number 100")
def test_case():
input_number = 100
expected_output = [(2, 2), (5, 2)]
test.assert_equals(solution.prime_factorization(input_number), expected_output)
@test.it("Test with number 79")
def test_case():
input_number = 79
expected_output = [(79, 1)]
test.assert_equals(solution.prime_factorization(input_number), expected_output)
@test.it("Test with number 13195")
def test_case():
input_number = 13195
expected_output = [(5, 1), (7, 1), (13, 1), (29, 1)]
test.assert_equals(solution.prime_factorization(input_number), expected_output)
@test.it("Test with number 30030")
def test_case():
input_number = 30030
expected_output = [(2, 1), (3, 1), (5, 1), (7, 1), (11, 1), (13, 1)]
test.assert_equals(solution.prime_factorization(input_number), expected_output)