from math import floor, log10 def count_digits_log(n): """Return the number of digits of an integer n using logarithms.""" if n == 0: return 1 return floor(log10(abs(n))) + 1 def count_digits_str(n): """Return the number of digits of an integer n using string len.""" return len(str(abs(n))) def count_digits_loop(n): """Return the number of digits of an integer n using repeated division.""" if n == 0: return 1 n = abs(n) res = 0 while n > 0: res += 1 n //= 10 return res n = int(input("n = ")) print("count_digits_log({}) = {}".format(n, count_digits_log(n))) print("count_digits_str({}) = {}".format(n, count_digits_str(n))) print("count_digits_loop({}) = {}".format(n, count_digits_loop(n))) def is_prime_def(n): """Returns `True` if an integer `n` is a prime and `False` otherwise.""" if n < 2: return False for d in range(2, n): if n % d == 0: return False return True def is_prime_sqrt(n): """Returns `True` if an integer `n` is a prime and `False` otherwise.""" if n < 2: return False d = 2 while d*d <= n: if n % d == 0: return False d += 1 return True n = int(input("n = ")) bool2text = { False: "is not", True: "is" } print("is_prime_def says that number {} {} a prime.".format(n, bool2text[is_prime_def(n)])) print("is_prime_sqrt says that number {} {} a prime.".format(n, bool2text[is_prime_sqrt(n)])) def is_prime_def(n): """Returns `True` if an integer `n` is a prime and `False` otherwise.""" if n < 2: return False for d in range(2, n): if n % d == 0: return False return True def is_prime_sqrt(n): """Returns `True` if an integer `n` is a prime and `False` otherwise.""" if n < 2: return False d = 2 while d*d <= n: if n % d == 0: return False d += 1 return True n = int(input("n = ")) bool2text = { False: "is not", True: "is" } print("is_prime_def says that number {} {} a prime.".format(n, bool2text[is_prime_def(n)])) print("is_prime_sqrt says that number {} {} a prime.".format(n, bool2text[is_prime_sqrt(n)])) from math import floor, sqrt def is_prime_sqrt(n): """Returns `True` if an integer `n` is a prime and `False` otherwise.""" if n < 2: return False for d in range(2, floor(sqrt(n)) + 1): if n % d == 0: return False d += 1 return True n = int(input("n = ")) bool2text = { False: "is not", True: "is" } print("is_prime_sqrt says that number {} {} a prime.".format(n, bool2text[is_prime_sqrt(n)])) from math import floor, sqrt def is_prime_py(n): """Returns `True` if an integer `n` is a prime and `False` otherwise.""" return (n >= 2) and all(n%d != 0 for d in range(2, floor(sqrt(n)) + 1)) n = int(input("n = ")) bool2text = { False: "is not", True: "is" } print("is_prime_py says that number {} {} a prime.".format(n, bool2text[is_prime_py(n)])) from time import time def divisors_sum(n): """Returns the sum of all natural divisiors of a natural number `n` except `n` itself.""" res = 0 for d in range(1, n): if n % d == 0: res += d return res # More Pythonic version def divisors_sum_py(n): """Returns the sum of all natural divisiors of a natural number `n` except `n` itself.""" return sum(d for d in range(1, n) if n % d == 0) n = int(input("n = ")) st = time() for a in range(1, n+1): for b in range(a+1, n+1): if a == divisors_sum(b) and b == divisors_sum(a): print((a, b)) print("Done in {:.6f} seconds.".format(time() - st)) n = int(input("n = ")) st = time() for a in range(1, n+1): b = divisors_sum(a) if a < b and a == divisors_sum(b): print((a, b)) print("Done in {:.6f} seconds.".format(time() - st)) from time import time from math import floor, sqrt def is_prime_sqrt(n): """Returns `True` if an integer `n` is a prime and `False` otherwise.""" if n < 2: return False for d in range(2, floor(sqrt(n)) + 1): if n % d == 0: return False d += 1 return True def print_prime_factors_def(n): """Prints all the prime factors of `n`, each of them once.""" for p in range(2, abs(n)+1): if n % p == 0 and is_prime_sqrt(p): print(p) n = int(input("n = ")) print("The prime factors of {} are:".format(n)) st = time() print_prime_factors_def(n) print("Total time:", time() - st) from time import time def print_prime_factors_div(n): """Prints all the prime factors of `n`, each of them once.""" p = 2 n = abs(n) while n > 1: if n % p == 0: print(p) while n % p == 0: n //= p p += 1 n = int(input("n = ")) print("The prime factors of {} are:".format(n)) st = time() print_prime_factors_div(n) print("Total time:", time() - st) from time import time from math import floor def print_prime_factors_div(n): """Prints all the prime factors of `n`, each of them once.""" p = 2 n = abs(n) n2 = floor(sqrt(n)) while p <= n2: if n % p == 0: print(p) while n % p == 0: n //= p n2 = floor(sqrt(n)) p += 1 if n > 1: print(n) n = int(input("n = ")) print("The prime factors of {} are:".format(n)) st = time() print_prime_factors_div(n) print("Total time:", time() - st) def print_prime_factors_div(n): """Prints all the prime factors of `n`, each of them repeated according to its multiplicity.""" p = 2 n = abs(n) while n > 1: if n % p == 0: while n % p == 0: print(p) n //= p p += 1 n = int(input("n = ")) print("The prime factors of {} are:".format(n)) print_prime_factors_div(n) def print_prime_factors_div(n): """Prints all the prime factors of `n`, each of them repeated according to its multiplicity.""" p = 2 n = abs(n) while n > 1: while n % p == 0: print(p) n //= p p += 1 n = int(input("n = ")) print("The prime factors of {} are:".format(n)) print_prime_factors_div(n) def print_prime_factors_div(n): """Prints the summands of the prime factorization of `n`.""" p = 2 n = abs(n) while n > 1: if n % p == 0: i = 0 while n % p == 0: i += 1 n //= p print("{}**{}".format(p, i)) p += 1 n = int(input("n = ")) print("The prime factors of {} are:".format(n)) print_prime_factors_div(n) def prime_factors_div(n): """Returns the generator that makes an iterator for the prime factorization of `n`.""" p = 2 n = abs(n) while n > 1: if n % p == 0: i = 0 while n % p == 0: i += 1 n //= p yield "{}**{}".format(p, i) p += 1 n = int(input("n = ")) print(n, "=", "+".join(prime_factors_div(n)))