# Function to calculate the sum of cubes of the first n integers and show the formula
def sum_of_cubes(n):
cubes = [i**3 for i in range(1, n + 1)]
sum_cubes = sum(cubes)
return sum_cubes, cubes
# Function to calculate the square of the sum of the first n integers and show the formula
def square_of_sum(n):
numbers = list(range(1, n + 1))
sum_of_numbers = sum(numbers)
square_sum = sum_of_numbers ** 2
return square_sum, numbers
# Prompt user for the number of lines to print
try:
limit = int(input("How many numbers would you like to print? "))
if limit > 0:
for n in range(1, limit + 1):
# Sum of cubes calculation and output
sum_cubes, cubes = sum_of_cubes(n)
cubes_formula = ' + '.join([f"{i}^3" for i in range(1, n + 1)])
print(f"n = {n}: {cubes_formula} = {sum_cubes}", end=" ")
# Square of sum calculation and output
square_sum, numbers = square_of_sum(n)
numbers_formula = ' + '.join([str(i) for i in range(1, n + 1)])
print(f"({numbers_formula})^2 = {square_sum}")
print() # Blank line between the results
else:
print("Please enter a positive integer.")
except ValueError:
print("Invalid input! Please enter a valid integer.")