# 2. Fill in the blanks to make the print_print_factors function print all the prime
# factors of a number. A prime factor is a number that is prime and divides
# another without a remainder.
from math import sqrt


def print_prime_factors(number):
    # Start with two, which is the first prime
    factor = 2
    # Keep going until the factor is larger than the number
    while factor <= number:
        # Check if factor is a divisor of number
        if number % factor == 0:
            # If it is, print it and divide the original number
            print(factor)
            number = number / factor
        else:
            # If it's not, increment the factor by one
            factor += 1
    return "Done"


print_prime_factors(100)


# Should print 2,2,5,5
# DO NOT DELETE THIS COMMENT

# 3. The following code can lead to an infinite loop. Fix the code so that it can finish
# successfully for all numbers.

# Note: Try running your function with the number 0 as the input, and see what
# you get!

def is_power_of_two(n):
    # Check if the number can be divided by two without a remainder
    while n % 2 == 0:
        if n == 0:
            break
        n = n / 2
    # If after dividing by two the number is 1, it's a power of two
    if n == 1:
        return True
    return False


print(is_power_of_two(0))  # Should be False
print(is_power_of_two(1))  # Should be True
print(is_power_of_two(8))  # Should be True
print(is_power_of_two(9))  # Should be False


# 5. The multiplication_table function prints the results of a number passed to it
# multiplied by 1 through 5. An additional requirement is that the result is not to
# exceed 25, which is done with the break statement. Fill in the blanks to complete
# the function to satisfy these conditions:

def multiplication_table(number):
    # Initialize the starting point of the multiplication table
    multiplier = 1
    # Only want to loop through 5
    while multiplier <= 5:
        result = multiplier * number
        # What is the additional condition to exit out of the loop?
        if result > 25:
            break
        print(str(number) + "x" + str(multiplier) + "=" + str(result))
        # Increment the variable for the loop
        multiplier += 1

multiplication_table(3)
# Should print:
multiplication_table(5)

multiplication_table(8)

# 4.
# Question 4
# Fill in the empty function so that it returns the sum of all the divisors of a
# number, without including it. A divisor is a number that divides into another
# without a remainder.
def sum_divisors(n):
    lst = []
    # Return the sum of all divisors of n, not including n
    i = 1
    while i * i < n:
        if n % i == 0:
            lst.append(i)
        i += 1
    for i in range(int(sqrt(n)), 1, -1):
        if n % i == 0:
            lst.append(n // i)
    return sum(lst)



print(sum_divisors(0))
# 0
print(sum_divisors(3))  # Should sum of 1
# 1
print(sum_divisors(36))  # Should sum of 1+2+3+4+6+9+12+18
# 55
print(sum_divisors(102))  # Should be sum of 2+3+6+17+34+51
# 114

# 2. Fill in the blanks to make the print_print_factors function print all the prime
# factors of a number. A prime factor is a number that is prime and divides
# another without a remainder.
from math import sqrt


def print_prime_factors(number):
    # Start with two, which is the first prime
    factor = 2
    # Keep going until the factor is larger than the number
    while factor <= number:
        # Check if factor is a divisor of number
        if number % factor == 0:
            # If it is, print it and divide the original number
            print(factor)
            number = number / factor
        else:
            # If it's not, increment the factor by one
            factor += 1
    return "Done"


print_prime_factors(100)


# Should print 2,2,5,5
# DO NOT DELETE THIS COMMENT

# 3. The following code can lead to an infinite loop. Fix the code so that it can finish
# successfully for all numbers.

# Note: Try running your function with the number 0 as the input, and see what
# you get!

def is_power_of_two(n):
    # Check if the number can be divided by two without a remainder
    while n % 2 == 0:
        if n == 0:
            break
        n = n / 2
    # If after dividing by two the number is 1, it's a power of two
    if n == 1:
        return True
    return False


print(is_power_of_two(0))  # Should be False
print(is_power_of_two(1))  # Should be True
print(is_power_of_two(8))  # Should be True
print(is_power_of_two(9))  # Should be False


# 5. The multiplication_table function prints the results of a number passed to it
# multiplied by 1 through 5. An additional requirement is that the result is not to
# exceed 25, which is done with the break statement. Fill in the blanks to complete
# the function to satisfy these conditions:

def multiplication_table(number):
    # Initialize the starting point of the multiplication table
    multiplier = 1
    # Only want to loop through 5
    while multiplier <= 5:
        result = multiplier * number
        # What is the additional condition to exit out of the loop?
        if result > 25:
            break
        print(str(number) + "x" + str(multiplier) + "=" + str(result))
        # Increment the variable for the loop
        multiplier += 1

multiplication_table(3)
# Should print:
multiplication_table(5)

multiplication_table(8)

# 4.
# Question 4
# Fill in the empty function so that it returns the sum of all the divisors of a
# number, without including it. A divisor is a number that divides into another
# without a remainder.
def sum_divisors(n):
    lst = []
    # Return the sum of all divisors of n, not including n
    i = 1
    while i * i < n:
        if n % i == 0:
            lst.append(i)
        i += 1
    for i in range(int(sqrt(n)), 1, -1):
        if n % i == 0:
            lst.append(n // i)
    return sum(lst)



print(sum_divisors(0))
# 0
print(sum_divisors(3))  # Should sum of 1
# 1
print(sum_divisors(36))  # Should sum of 1+2+3+4+6+9+12+18
# 55
print(sum_divisors(102))  # Should be sum of 2+3+6+17+34+51
# 114

# 2. Fill in the blanks to make the print_print_factors function print all the prime # factors of a number. A prime factor is a number that is prime and divides # another without a remainder. # Start with two, which is the first prime # Keep going until the factor is larger than the number # Check if factor is a divisor of number # If it is, print it and divide the original number # If it's not, increment the factor by one # Should print 2,2,5,5 # DO NOT DELETE THIS COMMENT # 3. The following code can lead to an infinite loop. Fix the code so that it can finish # successfully for all numbers. # Note: Try running your function with the number 0 as the input, and see what # you get! # Check if the number can be divided by two without a remainder # If after dividing by two the number is 1, it's a power of two # Should be False # Should be True # Should be True # Should be False # 5. The multiplication_table function prints the results of a number passed to it # multiplied by 1 through 5. An additional requirement is that the result is not to # exceed 25, which is done with the break statement. Fill in the blanks to complete # the function to satisfy these conditions: # Initialize the starting point of the multiplication table # Only want to loop through 5 # What is the additional condition to exit out of the loop? # Increment the variable for the loop # Should print: # 4. # Question 4 # Fill in the empty function so that it returns the sum of all the divisors of a # number, without including it. A divisor is a number that divides into another # without a remainder. # Return the sum of all divisors of n, not including n # 0 # Should sum of 1 # 1 # Should sum of 1+2+3+4+6+9+12+18 # 55 # Should be sum of 2+3+6+17+34+51 # 114