From ba8da6b2b53485a4cd7ed32552a63ed157113814 Mon Sep 17 00:00:00 2001 From: baphim <269010393+baphim@users.noreply.github.com> Date: Mon, 12 Oct 2026 17:48:57 -0400 Subject: [PATCH] feat: completed exercise 17 and 18 --- lab-05/exercise-17.py | 12 +++++++++++- lab-05/exercise-18.py | 19 ++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lab-05/exercise-17.py b/lab-05/exercise-17.py index 4e73063..48456b0 100644 --- a/lab-05/exercise-17.py +++ b/lab-05/exercise-17.py @@ -1,4 +1,14 @@ """ Write a program to use a loop to find the factorial of a given number (e.g., 5!). The factorial of N is the product of all integers from 1 to N. -""" \ No newline at end of file +""" +# Set input number and factorial variables +num = 5 +factorial = 1 + +# Iterate through range of 1 to num var + 1 and multiply by i +for i in range(1, num + 1): + factorial = factorial * i + +# Print resulting factorial +print(factorial) diff --git a/lab-05/exercise-18.py b/lab-05/exercise-18.py index 328916d..b7a2624 100644 --- a/lab-05/exercise-18.py +++ b/lab-05/exercise-18.py @@ -2,4 +2,21 @@ The Collatz conjecture states that if you start with any positive integer n, and if n is even, divide it by 2; if n is odd, multiply it by 3 and add 1. Repeat the process. The sequence will always eventually reach 1. Write a program to print this sequence for a given number. -""" \ No newline at end of file +""" + +# set variable n to 6 +n = 6 + +# Runs the loop until n = 1, signifying the end of the Collatz conjecture +while n != 1: + # print value of n plus a comma and space to print on the same line + print(n, end=", ") + # if n is even, halve n + if n % 2 == 0: + n = n // 2 + # if n is odd, multiply by 3 and add one + else: + n = n * 3 + 1 + +# Prints final value for n, which should always be 1 +print(n) \ No newline at end of file