Docsity

Discrete Math: C Program for Primes, Factorization, Euclidean Algorithm, Matrices, Inferen, Exercises of Discrete Mathematics

Toggle sidebar
bg1
CS 320L – Applied Discrete Mathematics – Spring 2013
Assignment #2
Sample Solutions
Question 1: Everything is Relative
.
Below is a simple C program that, for each integer from 2 to 100, prints the set of relative
primes and its cardinality.
#include <stdio.h>
#include <math.h>
int gcd(int a, int b)
{
if (a % b == 0)
return b;
return gcd(b, a % b);
}
int countRelprimes(int n)
{
int i, p = 0;
printf("%d: ", n);
for (i = 2; i < n; i++)
if (gcd(n, i) == 1)
{
printf("%d ", i);
p++;
}
printf("-> %d numbers\n", p);
return p;
}
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Discrete Math: C Program for Primes, Factorization, Euclidean Algorithm, Matrices, Inferen and more Exercises Discrete Mathematics in PDF only on Docsity!

CS 320L – Applied Discrete Mathematics – Spring 2013

Assignment

Sample Solutions

Question 1: Everything is Relative

Below is a simple C program that, for each integer from 2 to 100, prints the set of relative primes and its cardinality.

#include <stdio.h> #include <math.h>

int gcd(int a, int b) { if (a % b == 0) return b; return gcd(b, a % b); }

int countRelprimes(int n) { int i, p = 0;

printf("%d: ", n); for (i = 2; i < n; i++) if (gcd(n, i) == 1) { printf("%d ", i); p++; } printf("-> %d numbers\n", p); return p; }

void main() { int j;

for (j = 2; j <= 100; j++) countRelprimes(j); }

b) Use your program to find the smallest integer n for which there are exactly 19 integers m with 1 < m < n so that m and n are relatively prime. Write down this integer n and the 19 integers with which it is relatively prime.

The smallest integer of this kind is 25, and the 19 relatively prime numbers are 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19, 21, 22, 23, and 24.

Question 2: Prime Factor Examples

Write down the prime factorization (with primes listed in ascending order) of each of the following integers (Example: 720 = 2^4  32 5).

a) 444 = 2^2  3  37 b) 1025 = 5^2  41 c) 41 = 41 d) 3072 = 2^10  3

Question 3: Euclid’s Legacy

Use the Euclidean algorithm to determine the following greatest common divisors. Write down every step in your calculation.

a) gcd(810, 422)

810/422 = 1 R 388 422/388 = 1 R 34 388/34 = 11 R 34/14 = 2 R 6 14/6 = 2 R 2 6/2 = 3 R 0

gcd(810, 422) = 2

3

4

1

2

m

m

m

m

This gives us the following solution:

M 

b) Is there a matrix M that is not an identity matrix, does not only contain zeroes, and for which MM = M? Either provide such a matrix M or prove that it does not exist.

There are infinitely many solutions, such as:

M 

c) Find a zero-one matrix A so that the following equation becomes true, where “◦” is the Boolean multiplication symbol:

 A

Explain how you obtained your solution.

The simplest way of doing this is as follows: For this type of equation X ◦ A = Y, we know that in order for a 1 to show up in Y , there has to be at least one match between two 1s when we superimpose a column from A with a row from X. So we can just determine for each column in A , the rows in X with which it has to produce such a match, and then choose the elements in this column accordingly. The following matrix A solves the equation:

A

Question 5: Rules of Inference

Use rules of inference to show whether the following arguments are valid or not. Some cases may require predicates and/or quantification, and others may not. Write down every step of your reasoning as we did in class. If you find that an argument is not valid, provide an example for which the argument fails.

a) There is at least one Red Sox player who drives a BMW. All BMW drivers like eating sauerkraut. Therefore, there is at least one Red Sox player who likes eating sauerkraut.

R(x): x is a Red Sox player. B(x): x drives a BMW. S(x): x likes eating sauerkraut.

Hypotheses:

x[R(x)  B(x)] x[B(x)  S(x)]

Conclusion: x[R(x)  S(x)]

Step 1: x[R(x)  B(x)] (Hypothesis) Step 2: There is a y for which R(y)  B(y) (Existential Instantiation) Step 3: x[B(x)  S(x)] (Hypothesis) Step 4: B(y)  S(y) (Universal Instantiation) Step 5: B(y) (Simplification of Step 2) Step 6: S(y) (Modus Ponens Steps 4 and 5) Step 7: R(y) (Simplification of Step 2) Step 8: R(y)  S(y) (Conjunction of Steps 6 and 7) Step 9: x[R(x)  S(x)] (Existential Generalization of Step 8)

The argument is valid.

x[S(x)  H(x)] H(George)

Conclusion:

C(George)

Step 1: x[C(x)  S(x)] (Hypothesis) Step 2: C(George)  S(George) (Universal Instantiation of Step 1) Step 3: x[S(x)  H(x)] (Hypothesis) Step 4: S(George)  H(George) (Universal Instantiation of Step 3) Step 5: C(George)  H(George) (Hypothetical Syllogism Steps 2 and 4) Step 6: H(George) (Hypothesis) Step 7: C(George) (Modus Tollens Steps 5 and 6)

The argument is valid.

d) If it snows heavily, UMB will be closed. If UMB is closed, the students are bored. The students are bored. Therefore, it snowed heavily.

S: It shows heavily. C: UMB is closed. B: The students are bored.

Hypotheses:

S  C

C  B

B

Conclusion:

S

This argument is not valid. For example, if S is false, C is true, and B is true, then all hypotheses are true but the conclusion is false.

e) All CS professors wear thick glasses and pocket protectors. Whoever wears a pocket protector is carrying pens. Catherine does not carry pens. Therefore, Catherine is not a CS professor.

C(x): x is a CS professor. G(x): x wears thick glasses. W(x): x wears a pocket protector.

P(x): x carries pens.

Hypotheses:

x[C(x)  G(x)  W(x)] x[W(x)  P(x)] P(Catherine)

Conclusion:

C(Catherine)

Step 1: x[W(x)  P(x)] (Hypothesis) Step 2: W(Catherine)  P(Catherine) (Universal Instantiation of Step 1) Step 3: P(Catherine) (Hypothesis) Step 4: W(Catherine) (Modus Tollens Steps 2 and 3) Step 5: x[C(x)  G(x)  W(x)] (Hypothesis) Step 6: C(Catherine)  G(Catherine)  W(Catherine) (Universal Instantiation of Step 1) Step 7: G(Catherine)  W(Catherine) (Addition from Step 4) Step 8: (G(Catherine)  W(Catherine)) (De Morgan’s law from Step 7) Step 9: C(Catherine) (Modus Tollens Steps 6 and 8)

The argument is valid.

Question 6: Proofs

Prove or disprove the following statements:

a) For all positive integers n , 2 n^ is not divisible by 3.

Knowing the fundamental theorem of arithmetic, this is an easy proof to do:

Based on this theorem, in order to be divisible by 3, an integer needs to have the factor 3 in its prime factorization. However, the prime factorization of 2 n^ for any positive integer n is exactly that, 2 n. No other primes than 2 are included. Therefore, none of these numbers is divisible by 3.

b) The product of two odd integers is always an odd integer.

A number n is an odd integer if it is true that

n = 2 k + 1, where k is an integer.

So let us say that we have two odd integers m and n such that:

Question 8: Some Easy Conversions

For the following questions, show every step of your computation.

a) Convert the decimal number 2748 into its hexadecimal equivalent.

2748/16 = 171 R 12 171/16 = 10 R 11 10/16 = 0 R 10

2748 = (ABC) 16

b) Convert the decimal number 229 into its binary equivalent.

229/2 = 114 R 1 114/2 = 57 R 0 57/2 = 28 R 1 28/2 = 14 R 0 14/2 = 7 R 0 7/2 = 3 R 1 3/2 = 1 R 1 1/2 = 0 R 1

229 = (11100101) 2

c) Convert the binary number (1001101) 2 into its decimal equivalent.

(1001101) 2 = 2^6 + 2^3 + 2^2 + 2^0 = 64 + 8 + 4 + 1 = 77

Question 9: Recursive Definitions

Devise a recursive definition of the sequence {an}, where n = 1, 2, 3, …, for

a) an = -2n + 1

a 1 = - an+1 = an – 2

b) an = (-1)n

a 1 = - an+1 = -an

c) an = n

a 1 = 1 an+1 = an +

d) an = 5n!

a 1 = 5 an+1 = (n + 1)an