SlideShare a Scribd company logo
Course Project
Solutions
Discrete Mathematics 2019
Requirements
The Course project consists of two counting problems. In
the past, we have had counting problem involving
primes, triangle numbers and paths. Invariably, you would
have to use a computer program to find the solution.
There is no requirement for any particular programming
language and the choice is up to you.
I have tried to find some interesting and relevant
counting problems which can be solved using basic
computer knowledge. Have a look at the past questions
with some solution notes. That will help to give you an
idea of what the expectations are. You will however
need to put on your thinking cap and come up with a
suitable solution and test the solution using simpler
problems and apply it to the case you are asked to solve.
Requirements
For each problem, you will provide:
 Statement of Problem
 Solution
 Algorithms/Pseudocode/Dry run
 Code/Testing including screen prints of relevant output.
 Result
 Conclusion
 References
 Appendix: With code in a format which I can cut and
paste
Problem 1
How many positive integer solutions (including the
solutions in which xn= 0) are there to the equation
x1 + x2 + x3 + … + xn = S?
For instance, if we just have x1 + x2 = 3 then n = 2
and S = 3 and we could have x1 = 1 and x2 = 2, or x1
= 2 and x2 = 1 but we also have x1 = 0 and x2 = 3
and x1 = 3 and x2 = 0. So, in this case, there are four
solutions.
Write a computer program which accepts an
integer n, the number of x’s, and S, a value of the
sum, and computes the number of positive integer
solutions.
How many solutions are there when n = 10 and S =
100? Does your result seem reasonable? Comment.
Problem 1 Solution
# Problem 1: Number of
Combinations
# Programmer: Robert Geofroy
# Date: May 2019
# factorial.py
def factorial(n):
<< your code here>>
#Combinations
def comb (n, S):
<<Your code here>>
You are provided with this program template:
# Inputs
n = input ("Enter the value of n: ")
S = input ("Enter the value of S: ")
int_n = int(n)
int_S = int(S)
print ("The number of solutions is ",
comb ( int_n, int_S))
Testing
Input Output
n S Expected Output Algorithm Output
2 3 4
3 5
3 17 171
We are given a little help with the
following table for testing some cases…
We have to ensure that whatever
algorithm we devise, outputs the values
we calculated manually. This is the dry
run.
Problem 1 Solution
Input Output
n S Expected Output Computer Output
2 3 4
3 5 21
3 17 171
For each of the following cases
compute the solutions by hand and
then input the appropriate values in to
your computer program and see how
the output compares with the
expected output. Create two other
test scenarios of your own.
Problem 1 Solution
Take, x1 + x2 = 3. In this case we have x1 = 1 and x2 = 2 or x1 =
2 and x2 = 1 and x1 = 0 and x2 =3 or x1 = 3 and x2 = 0 so we
have 4 cases. The expected output is 4.
Recall the formula: n + r – 1C r = (n + r -1)!/r!(n – 1)!
Worked by hand:
With n = 2 and S = 3, we have solutions: 03 12 21 30
Check: n + S – 1 C S = 2 + 3 - 1 C 3 = 4!/3!1! = 4
With n = 4 and S = 5, we have solutions: 005 050 500 014 041
023 032 104 410 401 140 113 131 122 203 302 320 230 311 ...
With n = 3, S = 17, the number of solutions is 3 + 17 – 1 C 17 =
19C17 = 19!/17!2! = 19.9 = 171
Use your program to determine the number of solutions when
n =10 and S = 100.
Mathematical Justification
Python Code
# factorial.py
def factorial(n):
if n == 1:
return 1
else:
return n * factorial (n - 1)
#Combinations
def comb (n, S):
# We want S + n - 1 C S
numerator = factorial (S + n -1)
denominator = factorial (n - 1) * factorial (S)
result = numerator/denominator
return result
n = input ("Enter the value of n: ")
S = input ("Enter the value of S: ")
Testing the Solution
Problem 1 Testing the Solution
Input Output
n S Expected Output Computer Output
2 3 4 4
3 5 21 21
3 17 171 171
We test each of the scenarios
previously checked in the dry run and
compare with the computer output…
Case in Question
 The case in question is how many solutions are there
for n = 10 and S = 100. This is far more than we can
count manually so assuming that our program is
working, we can obtain the solution using these inputs.
Output
Output
 In the case of n = 10 and S = 100, we
have 4263421511271 solutions.
Conclusion
Given that there was clear grounds
mathematically for the solution design and
that the algorithm was dry run and given
that the program was tested using the test
cases for which the results were know, it is
safe to say that the output is reasonable
and correct. With these large numbers
one concern would be overflow but this
did not occur in this instance.
Problem 2
How many integers less than or equal to one thousand
are not divisible by either 5, 7 or 11? State any principle
which you use to arrive at your conclusion. What would
happen if we substituted 2, 4 and 5 for the three numbers
with T = 10? How would your solution differ? What would
be the count in that case?
Write a program which takes three integers x, y and z
prime to each other and a target, T and computes the
number of integers less than or equal to T that are not
divisible by either x, y or z. How many integers less than
10000 are not divisible by 13, 17 and 19?
Note that your program should test if x, y and z are prime
to each other as a condition for proceeding with the
calculation.
Prove any counting principles you use or employ in this
solution.
Problem 2 Solution
This solution uses the principle of inclusion-exclusion.
We are lucky that 5, 7 and 11 are primes. If they
weren’t then we would have a more difficult
problem. When we run our solution algorithm on
the integers 2, 4 and 5 with T = 10 we see that the
set intersection robs us of a clean solution.
Mathematical Justification
To Show:
|A∪B∪C|=|A|+|B|+|C|−|A∩B|−|B∩C|−|A∩C|+|A∩B∩C|
|A∪B∪C|=|A|+|B−A|+|C−B−A|
|A∩B|+|B−A|=|B|
|C−A|+|A∩C|=|C|
|C−B|+|+|B∩C|=|C|
|C−B−A|+|C|=|C−A|+|C−B|+|A∩B∩C|
summing the previous five equations gives
|A∪B∪C|=|A|+|B|+|C|−|A∩B|−|B∩C|−|A∩C|+|A∩B∩C|
Algorithm
Let Aj be the set of numbers between 1 and 1000 divisible by j.
We want to find the value of 1000 - |A5 Ս A7 Ս A11|. By the
Principle of Inclusion – Exclusion, we have:
|A5 Ս A7 Ս A11| = |A5| + |A7| + |A11| - |A5 ∩ A7| - |A5 ∩ A11| -
|A7 ∩ A11| + |A5 ∩A7 ∩ A11|
We have that |A5| = 200.
How did we arrive at that? 1000/5 = 200.
Also, |A7| = 1000/7 = 142 r 6 but the remainder does not count.
So |A7| = 142 and |A11| = 90.
|A5 ∩ A7| counts the number of numbers divisible by 5 and 7.
Since 5 and 7 are primes, we have |A5 ∩ A7| = |A35| = 28.
Similarly, - |A5 ∩ A11| = |A55| = 18 and |A7 ∩ A11| = |A77| = 12.
Finally, |A5 ∩A7 ∩ A11| = |A385| = 2.
Hence, |A5 U A7 U A11| = |A5| + |A7| + |A11| - |A5 ∩ A7| - |A5
∩ A11| - |A7 ∩ A11| + |A5 ∩A7 ∩ A11|
= 200 + 142 + 90 – 28 – 18 – 12 + 2 = 376 and we have 1000 – 376 =
624 numbers between 1 and 1000 are not divisible by 5, 7, 11.
Algorithm
In the case of 2, 4 and 5 with T = 10 the difference would be:
 We want to find the value of 10 - |A2 Ս A4 Ս A5|. By the
inclusion – Exclusion principle, we have:
 |A2 Ս A4 Ս A5| = |A2| + |A4| + |A5| - |A2 ∩ A4| - |A2 ∩
A5| - |A4 ∩ A5| + |A2 ∩A4 ∩ A5|
 We have A2 = {2, 4, 6, 8, 10} Thus, |A2| = 5; A4 = {4, 8}, so
|A4| = 2 and A5 = {5, 10} so |A5| = 2. A2 ∩ A5 = {10} Thus,
|A2 ∩ A5| =1. Since 2 and 5 are primes, they are also
coprime, and we have |A2 ∩ A5| = |A10| = 1. Similarly,
|A4 ∩ A5| = |A20| = 0. But now, A2 ∩ A4 = {4, 8} so |A2 ∩
A4| = |A4| = 2. Finally, |A2 ∩A4 ∩ A5| = 0.
 Hence, |A2 Ս A4 Ս A5| = |A2| + |A4| + |A5| - |A2 ∩ A4| -
|A2 ∩ A5| - |A4 ∩ A5| + |A2 ∩A4 ∩ A5|
 = 5 + 2 + 2 – 1 – 2 – 0 + 0 = 6 and we have 10 – 6 = 4
numbers between 1 and 10 are not divisible by 2, 4, 5.
These numbers are 1, 3, 7, 9.
Computer Program
We can also do this in Python. Python will pose
some interesting challenges to you, but the coding
is simple. We will need to be able to
 Input the three variables, x, y, z and the target T
 Convert the character string input into ints
 Code the algorithm
Problem 2 Solution
# 2019 Problem 2
# Programmer: Robert Geofroy
# Date: April 2019
# Read in data as character strings
x = input ("Enter the value of x: ")
<< Your code here>>
# Convert to ints
x_int = int (x)
<< Your code here>>
# Can we do integer division in Python?
int_xy = int (x_int/y_int)
print (int_xy)
# Define GCD
def gcd(a, b):
<<Your code here>>
# Define GCD
def gcd(a, b):
<<Your code here>>
# Define coprime function
def coprime(a, b):
<< Your code here>>
def findnumber (x, y, z, T):
<< your code here>>
return total
if coprime (x_int, y_int) and coprime
(y_int, z_int) and coprime (x_int, z_int):
<<Your code here>>)
Problem 2 Solution
# 2019 Problem 2
# Programmer: Robert Geofroy
# Date: April 2019
# Read in data as character strings
x = input ("Enter the value of x: ")
y = input ("Enter the value of y: ")
z = input ("Enter the value of z: ")
T = input ("Enter the target, T: ")
# Convert to ints
x_int = int (x)
y_int = int (y)
z_int = int (z)
T_int = int (T)
# Can we do integer division in Python?
int_xy = int (x_int/y_int)
print (int_xy)
# Define GCD
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
# Define coprime function
def coprime(a, b):
return gcd(a, b) == 1
def findnumber (x, y, z, T):
# your code here
num_1 = int (T/x) + int (T/y) + int (T/z)
num_2 = int (T/(x*y)) + int (T/(y*z)) + int (T/(x*z))
num_3 = int (T/(x*y*z))
total = num_1 - num_2 + num_3
return total
if coprime (x_int, y_int) and coprime (y_int, z_int) and
coprime (x_int, z_int):
print (x, y, z, T)
nums = findnumber (x_int, y_int, z_int, T_int)
print ("Cases = ", T_int - nums)
else:
print ("Numbers are not coprime.")
Program Run
Testing
x y z T Expected Output Computer Output
2 3 5 10 2
2 4 5 10 Not coprime
…add more rows if
necessary
In the case of x = 2, y = 3 and z = 5 with T = 10, there
are 2 numbers which are not divisible by either 2, 3,
or 5. These are: 1 and 7.
Testing
x y z T Expected Output Computer Output
2 3 5 10 2
2 4 5 10 Not coprime
…add more rows if
necessary
If we put x = 2, y = 4 and z = 5 with T = 10 we should
get an error message saying the numbers are not
coprime.
Testing
x y z T Expected Output Computer Output
2 3 5 10 2
2 4 5 10 Not coprime
5 7 11 624 624
5 15 11 Not coprime
The complete set of test cases is shown below:
How many integers less than 10000 are not divisible
by 13, 17 and 19?
Program Run
For x = 13, y = 17 and z = 19 with target = 10000 the
program gives 8230 as the answer.
Conclusion
The code has been tested using cases for which the
result can be computed mentally and by hand and
from all appearances it is working as expected. It
also deals with the case where the numbers are not
coprime as per the requirements of the question.
From all appearances, the program is working as
expected.

More Related Content

What's hot

The Properties of Mathematics
The Properties of MathematicsThe Properties of Mathematics
The Properties of Mathematics
arinedge
 
Powerpoint on K-12 Mathematics Grade 7 Q1 (Fundamental Operations of Integer...
Powerpoint  on K-12 Mathematics Grade 7 Q1 (Fundamental Operations of Integer...Powerpoint  on K-12 Mathematics Grade 7 Q1 (Fundamental Operations of Integer...
Powerpoint on K-12 Mathematics Grade 7 Q1 (Fundamental Operations of Integer...
Franz Jeremiah Ü Ibay
 
The continued fraction part i
The continued fraction part iThe continued fraction part i
The continued fraction part i
EdTechonGC Mallett
 
G6 m2-c-lesson 13-t
G6 m2-c-lesson 13-tG6 m2-c-lesson 13-t
G6 m2-c-lesson 13-t
mlabuski
 
Answering Techniques Ad Maths P1
Answering Techniques Ad Maths P1Answering Techniques Ad Maths P1
Answering Techniques Ad Maths P1
morabisma
 
Complex number
Complex numberComplex number
Grade 8 math_review
Grade 8 math_reviewGrade 8 math_review
Grade 8 math_review
Wenny Wang Wu
 
Lesson 1 1 properties of real numbers
Lesson 1 1 properties of real numbersLesson 1 1 properties of real numbers
Lesson 1 1 properties of real numbers
Terry Gastauer
 
Basic math akash
Basic math akashBasic math akash
Basic math akash
Akash Varaiya
 
Integers
IntegersIntegers
Integers
Punita Verma
 
5.9 complex numbers
5.9 complex numbers5.9 complex numbers
5.9 complex numbers
Jessica Garcia
 
1 4 Properties of Real Numbers
1 4 Properties of Real Numbers1 4 Properties of Real Numbers
1 4 Properties of Real Numbers
Dee Black
 
Adding Subtracting Integers
Adding Subtracting IntegersAdding Subtracting Integers
Adding Subtracting Integers
alvarezd
 
1.3 Complex Numbers, Quadratic Equations In The Complex Number System
1.3 Complex Numbers, Quadratic Equations In The Complex Number System1.3 Complex Numbers, Quadratic Equations In The Complex Number System
1.3 Complex Numbers, Quadratic Equations In The Complex Number System
guest620260
 
Number system
Number systemNumber system
Number system
Diksha Shivpure
 
Add and subtract pos and neg numbers 4 parts
Add and subtract pos and neg numbers 4 partsAdd and subtract pos and neg numbers 4 parts
Add and subtract pos and neg numbers 4 parts
Melanie_Anderson
 
IIT JEE Maths 1999
IIT JEE Maths   1999IIT JEE Maths   1999
IIT JEE Maths 1999
Vasista Vinuthan
 
Absolute value
Absolute valueAbsolute value
Absolute value
tvierra
 
Operations on Real Numbers
Operations on Real NumbersOperations on Real Numbers
Operations on Real Numbers
Mid Michigan Community College
 

What's hot (19)

The Properties of Mathematics
The Properties of MathematicsThe Properties of Mathematics
The Properties of Mathematics
 
Powerpoint on K-12 Mathematics Grade 7 Q1 (Fundamental Operations of Integer...
Powerpoint  on K-12 Mathematics Grade 7 Q1 (Fundamental Operations of Integer...Powerpoint  on K-12 Mathematics Grade 7 Q1 (Fundamental Operations of Integer...
Powerpoint on K-12 Mathematics Grade 7 Q1 (Fundamental Operations of Integer...
 
The continued fraction part i
The continued fraction part iThe continued fraction part i
The continued fraction part i
 
G6 m2-c-lesson 13-t
G6 m2-c-lesson 13-tG6 m2-c-lesson 13-t
G6 m2-c-lesson 13-t
 
Answering Techniques Ad Maths P1
Answering Techniques Ad Maths P1Answering Techniques Ad Maths P1
Answering Techniques Ad Maths P1
 
Complex number
Complex numberComplex number
Complex number
 
Grade 8 math_review
Grade 8 math_reviewGrade 8 math_review
Grade 8 math_review
 
Lesson 1 1 properties of real numbers
Lesson 1 1 properties of real numbersLesson 1 1 properties of real numbers
Lesson 1 1 properties of real numbers
 
Basic math akash
Basic math akashBasic math akash
Basic math akash
 
Integers
IntegersIntegers
Integers
 
5.9 complex numbers
5.9 complex numbers5.9 complex numbers
5.9 complex numbers
 
1 4 Properties of Real Numbers
1 4 Properties of Real Numbers1 4 Properties of Real Numbers
1 4 Properties of Real Numbers
 
Adding Subtracting Integers
Adding Subtracting IntegersAdding Subtracting Integers
Adding Subtracting Integers
 
1.3 Complex Numbers, Quadratic Equations In The Complex Number System
1.3 Complex Numbers, Quadratic Equations In The Complex Number System1.3 Complex Numbers, Quadratic Equations In The Complex Number System
1.3 Complex Numbers, Quadratic Equations In The Complex Number System
 
Number system
Number systemNumber system
Number system
 
Add and subtract pos and neg numbers 4 parts
Add and subtract pos and neg numbers 4 partsAdd and subtract pos and neg numbers 4 parts
Add and subtract pos and neg numbers 4 parts
 
IIT JEE Maths 1999
IIT JEE Maths   1999IIT JEE Maths   1999
IIT JEE Maths 1999
 
Absolute value
Absolute valueAbsolute value
Absolute value
 
Operations on Real Numbers
Operations on Real NumbersOperations on Real Numbers
Operations on Real Numbers
 

Similar to Course project solutions 2019

tutorial5.ppt
tutorial5.ppttutorial5.ppt
tutorial5.ppt
jvjfvvoa
 
Module 1 solving inequalities notes
Module 1 solving inequalities notesModule 1 solving inequalities notes
Module 1 solving inequalities notes
Michelle Barnhill
 
Stanford splash spring 2016 basic programming
Stanford splash spring 2016 basic programmingStanford splash spring 2016 basic programming
Stanford splash spring 2016 basic programming
Yu-Sheng (Yosen) Chen
 
Std 10 computer chapter 9 Problems and Problem Solving
Std 10 computer chapter 9 Problems and Problem SolvingStd 10 computer chapter 9 Problems and Problem Solving
Std 10 computer chapter 9 Problems and Problem Solving
Nuzhat Memon
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
B.Kirron Reddi
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.ppt
ISHANAMRITSRIVASTAVA
 
OverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docxOverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docx
gerardkortney
 
Marh algebra lesson
Marh algebra lessonMarh algebra lesson
Marh algebra lesson
M, Michelle Jeannite
 
ppt math6 qt3-wk3.pptx
ppt math6 qt3-wk3.pptxppt math6 qt3-wk3.pptx
ppt math6 qt3-wk3.pptx
casafelicemcat04
 
5. R basics
5. R basics5. R basics
5. R basics
FAO
 
Sept. 21, 2012
Sept. 21, 2012Sept. 21, 2012
Sept. 21, 2012
khyps13
 
ICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptxICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptx
ssuser2f67c91
 
Course project solutions 2018
Course project solutions 2018Course project solutions 2018
Course project solutions 2018
Robert Geofroy
 
Aditya Class 8th
Aditya Class 8thAditya Class 8th
Aditya Class 8th
BasantOjha1
 
Hemh101
Hemh101Hemh101
PEA305 workbook.pdf
PEA305 workbook.pdfPEA305 workbook.pdf
PEA305 workbook.pdf
RohitkumarYadav80
 
Handa ka funda math formulas 5.0
Handa ka funda   math formulas 5.0Handa ka funda   math formulas 5.0
Handa ka funda math formulas 5.0
Chandan Kumar
 
C Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.pptC Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.ppt
GAURAVNAUTIYAL19
 
Regression
RegressionRegression
Regression
ramyaranjith
 
Probability module 1
Probability module 1Probability module 1
Probability module 1
Richard Paulino
 

Similar to Course project solutions 2019 (20)

tutorial5.ppt
tutorial5.ppttutorial5.ppt
tutorial5.ppt
 
Module 1 solving inequalities notes
Module 1 solving inequalities notesModule 1 solving inequalities notes
Module 1 solving inequalities notes
 
Stanford splash spring 2016 basic programming
Stanford splash spring 2016 basic programmingStanford splash spring 2016 basic programming
Stanford splash spring 2016 basic programming
 
Std 10 computer chapter 9 Problems and Problem Solving
Std 10 computer chapter 9 Problems and Problem SolvingStd 10 computer chapter 9 Problems and Problem Solving
Std 10 computer chapter 9 Problems and Problem Solving
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.ppt
 
OverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docxOverviewThis hands-on lab allows you to follow and experiment w.docx
OverviewThis hands-on lab allows you to follow and experiment w.docx
 
Marh algebra lesson
Marh algebra lessonMarh algebra lesson
Marh algebra lesson
 
ppt math6 qt3-wk3.pptx
ppt math6 qt3-wk3.pptxppt math6 qt3-wk3.pptx
ppt math6 qt3-wk3.pptx
 
5. R basics
5. R basics5. R basics
5. R basics
 
Sept. 21, 2012
Sept. 21, 2012Sept. 21, 2012
Sept. 21, 2012
 
ICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptxICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptx
 
Course project solutions 2018
Course project solutions 2018Course project solutions 2018
Course project solutions 2018
 
Aditya Class 8th
Aditya Class 8thAditya Class 8th
Aditya Class 8th
 
Hemh101
Hemh101Hemh101
Hemh101
 
PEA305 workbook.pdf
PEA305 workbook.pdfPEA305 workbook.pdf
PEA305 workbook.pdf
 
Handa ka funda math formulas 5.0
Handa ka funda   math formulas 5.0Handa ka funda   math formulas 5.0
Handa ka funda math formulas 5.0
 
C Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.pptC Programing Arithmetic Operators.ppt
C Programing Arithmetic Operators.ppt
 
Regression
RegressionRegression
Regression
 
Probability module 1
Probability module 1Probability module 1
Probability module 1
 

More from Robert Geofroy

ESOL_COURSE_ INTRODUCTION_R_GEOFROY.pptx
ESOL_COURSE_ INTRODUCTION_R_GEOFROY.pptxESOL_COURSE_ INTRODUCTION_R_GEOFROY.pptx
ESOL_COURSE_ INTRODUCTION_R_GEOFROY.pptx
Robert Geofroy
 
This is the introductory set of slides for the Basic English course.
This is the introductory set of slides for the Basic English course.This is the introductory set of slides for the Basic English course.
This is the introductory set of slides for the Basic English course.
Robert Geofroy
 
Sampling Distribution of Sample proportion
Sampling Distribution of Sample proportionSampling Distribution of Sample proportion
Sampling Distribution of Sample proportion
Robert Geofroy
 
Dm2021 binary operations
Dm2021 binary operationsDm2021 binary operations
Dm2021 binary operations
Robert Geofroy
 
Set theory
Set theorySet theory
Set theory
Robert Geofroy
 
Permutations 2020
Permutations 2020Permutations 2020
Permutations 2020
Robert Geofroy
 
Datasets
DatasetsDatasets
Datasets
Robert Geofroy
 
DM2020 boolean algebra
DM2020 boolean algebraDM2020 boolean algebra
DM2020 boolean algebra
Robert Geofroy
 
Arguments and methods of proof
Arguments and methods of proofArguments and methods of proof
Arguments and methods of proof
Robert Geofroy
 
Using sage maths to solve systems of linear equations
Using sage maths to solve systems of linear equationsUsing sage maths to solve systems of linear equations
Using sage maths to solve systems of linear equations
Robert Geofroy
 

More from Robert Geofroy (10)

ESOL_COURSE_ INTRODUCTION_R_GEOFROY.pptx
ESOL_COURSE_ INTRODUCTION_R_GEOFROY.pptxESOL_COURSE_ INTRODUCTION_R_GEOFROY.pptx
ESOL_COURSE_ INTRODUCTION_R_GEOFROY.pptx
 
This is the introductory set of slides for the Basic English course.
This is the introductory set of slides for the Basic English course.This is the introductory set of slides for the Basic English course.
This is the introductory set of slides for the Basic English course.
 
Sampling Distribution of Sample proportion
Sampling Distribution of Sample proportionSampling Distribution of Sample proportion
Sampling Distribution of Sample proportion
 
Dm2021 binary operations
Dm2021 binary operationsDm2021 binary operations
Dm2021 binary operations
 
Set theory
Set theorySet theory
Set theory
 
Permutations 2020
Permutations 2020Permutations 2020
Permutations 2020
 
Datasets
DatasetsDatasets
Datasets
 
DM2020 boolean algebra
DM2020 boolean algebraDM2020 boolean algebra
DM2020 boolean algebra
 
Arguments and methods of proof
Arguments and methods of proofArguments and methods of proof
Arguments and methods of proof
 
Using sage maths to solve systems of linear equations
Using sage maths to solve systems of linear equationsUsing sage maths to solve systems of linear equations
Using sage maths to solve systems of linear equations
 

Recently uploaded

Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 

Recently uploaded (20)

Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 

Course project solutions 2019

  • 2. Requirements The Course project consists of two counting problems. In the past, we have had counting problem involving primes, triangle numbers and paths. Invariably, you would have to use a computer program to find the solution. There is no requirement for any particular programming language and the choice is up to you. I have tried to find some interesting and relevant counting problems which can be solved using basic computer knowledge. Have a look at the past questions with some solution notes. That will help to give you an idea of what the expectations are. You will however need to put on your thinking cap and come up with a suitable solution and test the solution using simpler problems and apply it to the case you are asked to solve.
  • 3. Requirements For each problem, you will provide:  Statement of Problem  Solution  Algorithms/Pseudocode/Dry run  Code/Testing including screen prints of relevant output.  Result  Conclusion  References  Appendix: With code in a format which I can cut and paste
  • 4. Problem 1 How many positive integer solutions (including the solutions in which xn= 0) are there to the equation x1 + x2 + x3 + … + xn = S? For instance, if we just have x1 + x2 = 3 then n = 2 and S = 3 and we could have x1 = 1 and x2 = 2, or x1 = 2 and x2 = 1 but we also have x1 = 0 and x2 = 3 and x1 = 3 and x2 = 0. So, in this case, there are four solutions. Write a computer program which accepts an integer n, the number of x’s, and S, a value of the sum, and computes the number of positive integer solutions. How many solutions are there when n = 10 and S = 100? Does your result seem reasonable? Comment.
  • 5. Problem 1 Solution # Problem 1: Number of Combinations # Programmer: Robert Geofroy # Date: May 2019 # factorial.py def factorial(n): << your code here>> #Combinations def comb (n, S): <<Your code here>> You are provided with this program template: # Inputs n = input ("Enter the value of n: ") S = input ("Enter the value of S: ") int_n = int(n) int_S = int(S) print ("The number of solutions is ", comb ( int_n, int_S))
  • 6. Testing Input Output n S Expected Output Algorithm Output 2 3 4 3 5 3 17 171 We are given a little help with the following table for testing some cases… We have to ensure that whatever algorithm we devise, outputs the values we calculated manually. This is the dry run.
  • 7. Problem 1 Solution Input Output n S Expected Output Computer Output 2 3 4 3 5 21 3 17 171 For each of the following cases compute the solutions by hand and then input the appropriate values in to your computer program and see how the output compares with the expected output. Create two other test scenarios of your own.
  • 8. Problem 1 Solution Take, x1 + x2 = 3. In this case we have x1 = 1 and x2 = 2 or x1 = 2 and x2 = 1 and x1 = 0 and x2 =3 or x1 = 3 and x2 = 0 so we have 4 cases. The expected output is 4. Recall the formula: n + r – 1C r = (n + r -1)!/r!(n – 1)! Worked by hand: With n = 2 and S = 3, we have solutions: 03 12 21 30 Check: n + S – 1 C S = 2 + 3 - 1 C 3 = 4!/3!1! = 4 With n = 4 and S = 5, we have solutions: 005 050 500 014 041 023 032 104 410 401 140 113 131 122 203 302 320 230 311 ... With n = 3, S = 17, the number of solutions is 3 + 17 – 1 C 17 = 19C17 = 19!/17!2! = 19.9 = 171 Use your program to determine the number of solutions when n =10 and S = 100. Mathematical Justification
  • 9. Python Code # factorial.py def factorial(n): if n == 1: return 1 else: return n * factorial (n - 1) #Combinations def comb (n, S): # We want S + n - 1 C S numerator = factorial (S + n -1) denominator = factorial (n - 1) * factorial (S) result = numerator/denominator return result n = input ("Enter the value of n: ") S = input ("Enter the value of S: ")
  • 11. Problem 1 Testing the Solution Input Output n S Expected Output Computer Output 2 3 4 4 3 5 21 21 3 17 171 171 We test each of the scenarios previously checked in the dry run and compare with the computer output…
  • 12. Case in Question  The case in question is how many solutions are there for n = 10 and S = 100. This is far more than we can count manually so assuming that our program is working, we can obtain the solution using these inputs.
  • 14. Output  In the case of n = 10 and S = 100, we have 4263421511271 solutions.
  • 15. Conclusion Given that there was clear grounds mathematically for the solution design and that the algorithm was dry run and given that the program was tested using the test cases for which the results were know, it is safe to say that the output is reasonable and correct. With these large numbers one concern would be overflow but this did not occur in this instance.
  • 16. Problem 2 How many integers less than or equal to one thousand are not divisible by either 5, 7 or 11? State any principle which you use to arrive at your conclusion. What would happen if we substituted 2, 4 and 5 for the three numbers with T = 10? How would your solution differ? What would be the count in that case? Write a program which takes three integers x, y and z prime to each other and a target, T and computes the number of integers less than or equal to T that are not divisible by either x, y or z. How many integers less than 10000 are not divisible by 13, 17 and 19? Note that your program should test if x, y and z are prime to each other as a condition for proceeding with the calculation. Prove any counting principles you use or employ in this solution.
  • 17. Problem 2 Solution This solution uses the principle of inclusion-exclusion. We are lucky that 5, 7 and 11 are primes. If they weren’t then we would have a more difficult problem. When we run our solution algorithm on the integers 2, 4 and 5 with T = 10 we see that the set intersection robs us of a clean solution. Mathematical Justification
  • 19. Algorithm Let Aj be the set of numbers between 1 and 1000 divisible by j. We want to find the value of 1000 - |A5 Ս A7 Ս A11|. By the Principle of Inclusion – Exclusion, we have: |A5 Ս A7 Ս A11| = |A5| + |A7| + |A11| - |A5 ∩ A7| - |A5 ∩ A11| - |A7 ∩ A11| + |A5 ∩A7 ∩ A11| We have that |A5| = 200. How did we arrive at that? 1000/5 = 200. Also, |A7| = 1000/7 = 142 r 6 but the remainder does not count. So |A7| = 142 and |A11| = 90. |A5 ∩ A7| counts the number of numbers divisible by 5 and 7. Since 5 and 7 are primes, we have |A5 ∩ A7| = |A35| = 28. Similarly, - |A5 ∩ A11| = |A55| = 18 and |A7 ∩ A11| = |A77| = 12. Finally, |A5 ∩A7 ∩ A11| = |A385| = 2. Hence, |A5 U A7 U A11| = |A5| + |A7| + |A11| - |A5 ∩ A7| - |A5 ∩ A11| - |A7 ∩ A11| + |A5 ∩A7 ∩ A11| = 200 + 142 + 90 – 28 – 18 – 12 + 2 = 376 and we have 1000 – 376 = 624 numbers between 1 and 1000 are not divisible by 5, 7, 11.
  • 20. Algorithm In the case of 2, 4 and 5 with T = 10 the difference would be:  We want to find the value of 10 - |A2 Ս A4 Ս A5|. By the inclusion – Exclusion principle, we have:  |A2 Ս A4 Ս A5| = |A2| + |A4| + |A5| - |A2 ∩ A4| - |A2 ∩ A5| - |A4 ∩ A5| + |A2 ∩A4 ∩ A5|  We have A2 = {2, 4, 6, 8, 10} Thus, |A2| = 5; A4 = {4, 8}, so |A4| = 2 and A5 = {5, 10} so |A5| = 2. A2 ∩ A5 = {10} Thus, |A2 ∩ A5| =1. Since 2 and 5 are primes, they are also coprime, and we have |A2 ∩ A5| = |A10| = 1. Similarly, |A4 ∩ A5| = |A20| = 0. But now, A2 ∩ A4 = {4, 8} so |A2 ∩ A4| = |A4| = 2. Finally, |A2 ∩A4 ∩ A5| = 0.  Hence, |A2 Ս A4 Ս A5| = |A2| + |A4| + |A5| - |A2 ∩ A4| - |A2 ∩ A5| - |A4 ∩ A5| + |A2 ∩A4 ∩ A5|  = 5 + 2 + 2 – 1 – 2 – 0 + 0 = 6 and we have 10 – 6 = 4 numbers between 1 and 10 are not divisible by 2, 4, 5. These numbers are 1, 3, 7, 9.
  • 21. Computer Program We can also do this in Python. Python will pose some interesting challenges to you, but the coding is simple. We will need to be able to  Input the three variables, x, y, z and the target T  Convert the character string input into ints  Code the algorithm
  • 22. Problem 2 Solution # 2019 Problem 2 # Programmer: Robert Geofroy # Date: April 2019 # Read in data as character strings x = input ("Enter the value of x: ") << Your code here>> # Convert to ints x_int = int (x) << Your code here>> # Can we do integer division in Python? int_xy = int (x_int/y_int) print (int_xy) # Define GCD def gcd(a, b): <<Your code here>> # Define GCD def gcd(a, b): <<Your code here>> # Define coprime function def coprime(a, b): << Your code here>> def findnumber (x, y, z, T): << your code here>> return total if coprime (x_int, y_int) and coprime (y_int, z_int) and coprime (x_int, z_int): <<Your code here>>)
  • 23. Problem 2 Solution # 2019 Problem 2 # Programmer: Robert Geofroy # Date: April 2019 # Read in data as character strings x = input ("Enter the value of x: ") y = input ("Enter the value of y: ") z = input ("Enter the value of z: ") T = input ("Enter the target, T: ") # Convert to ints x_int = int (x) y_int = int (y) z_int = int (z) T_int = int (T) # Can we do integer division in Python? int_xy = int (x_int/y_int) print (int_xy) # Define GCD def gcd(a, b): while b != 0: a, b = b, a % b return a # Define coprime function def coprime(a, b): return gcd(a, b) == 1 def findnumber (x, y, z, T): # your code here num_1 = int (T/x) + int (T/y) + int (T/z) num_2 = int (T/(x*y)) + int (T/(y*z)) + int (T/(x*z)) num_3 = int (T/(x*y*z)) total = num_1 - num_2 + num_3 return total if coprime (x_int, y_int) and coprime (y_int, z_int) and coprime (x_int, z_int): print (x, y, z, T) nums = findnumber (x_int, y_int, z_int, T_int) print ("Cases = ", T_int - nums) else: print ("Numbers are not coprime.")
  • 25. Testing x y z T Expected Output Computer Output 2 3 5 10 2 2 4 5 10 Not coprime …add more rows if necessary In the case of x = 2, y = 3 and z = 5 with T = 10, there are 2 numbers which are not divisible by either 2, 3, or 5. These are: 1 and 7.
  • 26. Testing x y z T Expected Output Computer Output 2 3 5 10 2 2 4 5 10 Not coprime …add more rows if necessary If we put x = 2, y = 4 and z = 5 with T = 10 we should get an error message saying the numbers are not coprime.
  • 27. Testing x y z T Expected Output Computer Output 2 3 5 10 2 2 4 5 10 Not coprime 5 7 11 624 624 5 15 11 Not coprime The complete set of test cases is shown below:
  • 28. How many integers less than 10000 are not divisible by 13, 17 and 19?
  • 29. Program Run For x = 13, y = 17 and z = 19 with target = 10000 the program gives 8230 as the answer.
  • 30. Conclusion The code has been tested using cases for which the result can be computed mentally and by hand and from all appearances it is working as expected. It also deals with the case where the numbers are not coprime as per the requirements of the question. From all appearances, the program is working as expected.