Saturday, May 9, 2026

Week 2 Notes


# Python Problem Solver
# Week 2 Example 1: the Goldbach Conjecture
#

# Define function to determine whether a number is Prime number
def isPrime(num):
    for i in range(2,num):
        if num % i == 0:
            return False
    return True

# Get user to input an enen number larger than 2
n = int(input("Enter an even number greater than 2: "))
while (n % 2 == 1 or n <= 2):
    n = int(input("Enter an even number greater than 2: "))

# For any i less than n, if both i and n-i are prime, we find a match
for i in range (2, n):
    j = n - i
    if isPrime(i) and isPrime(j):
        print(f"{n} = {i} + {j}")
        break


# Python Problem Solver
# Week 2 Example 2: the Egg Farmer's Puzzle
#

# Get user to input a positive integer
n = int(input("Number of eggs: "))
while (n < 1):
    n = int(input("Number off eggs: "))

# Find number of large cartons
c1 = n // 12
if c1 > 0:
    print(f"You will need {c1} cartons of 12")
n -= c1 * 12

# Find number of small cartons
c2 = n // 6
if c2 > 0:
    print(f"You will need {c2} cartons of 6")
n -= c2 * 6 

# Find number of loose eggs
if n > 0:
    print(f"You will have {n} eggs for breakfast!")




No comments:

Post a Comment

Week 8 Assignments