from math import sqrt, ceil # Factorial using for cycle def factorial_for(n): f = 1 for i in range(1, n+1): f = f * i return f # Factorial using while cycle def factorial_while(n): f = 1 while n > 1: f *= n n -= 1 return f print(factorial_for(6)) print(factorial_while(6)) # Digit sum of number n def digit_sum(n): c = 0 while n > 0: digit = n % 10 n = n // 10 c += digit return c # Repeated digit sum of number n # Applies digit_sum repeatedly on # number n until one-digit result is found def repeated_digit_sum(n): while n >= 10: n = digit_sum(n) return n print(digit_sum(5692)) print(repeated_digit_sum(5692)) # Prints all divisors of a number def divisors(n): for i in range(1, n+1): if n % i == 0: print(i, end=" ") print() return 14 divisors(24) # Just prints the divisors print(divisors(24)) # Prints the divisors and then the return value 14 a = divisors(24) * 5 # Prints the divisors and stores the return value 14 # multiplied by 5 in variable a print("a =",a) # Variable a is printed # Function that returns whether n is a prime number def is_prime(n): # Special value of n=2 treated separately if n == 2: return True # All other cases fall in here # The largest number we need to check is sqrt(n) for i in range(2, ceil(sqrt(n))+1): if n % i == 0: # If there is a divisor, we immediately say it is not a prime number return False # At this point we went through all the numbers that could # have divided n, but none of them did. The number is thus a prime. return True print(is_prime(2)) # We can use function is_prime to perform more complex computations # Prints all the primes less than a limit def primes_less_than(limit): # We go through all the numbers from 2 to the limit for j in range(2, limit + 1): # If tested number j is prime, we print it if is_prime(j): print(j, end=" ") # Prints k-th prime number def kth_prime(k): # Counter stores how many primes we have already gone through counter = 0 # n is current tested number n = 2 # We cycle as long as we do not have k-th prime number while counter < k: if is_prime(n): # When we have found a prime number, we increase the counter counter += 1 n += 1 # We go through the numbers one by one return n - 1 # We return n-1 as even after we found k-th prime number, we increased n primes_less_than(15) print(kth_prime(1)) print(kth_prime(100)) # Prints the factorized number # Note how this function need not use is_prime function def factorization(n): # div is current tested divisor div = 2 # When n == 1 we went through all its divisors while n > 1: if n % div == 0: # When div is n's divisor, we print it and divide n by it print(div, end=" ") n = n // div else: # If div does not divide n, we increase it and test another one div += 1 print() # return is not here, as we do not want to return any value factorization(1024) # Converts number from decimal to binary def convert_10_to_2(x): # out stores the binary string out = "" # We divide x and store the bits until x is 0 while x > 0: bit = str(x % 2) # bit contains actual remainder after division by 2 # This is the bit we obtained out = bit + out # We prepend the output string with found bit x = x // 2 return out print(convert_10_to_2(42))