Vous êtes sur la page 1sur 4

Discrete Structures – Fall 2018 Assignment-01 Solution

Problem 1 -
a) Write a function bool isPrime(int N), to check whether a given number N is
prime or not.
b) Write a function to check whether P(N) = n2 + n + 41 is prime or not?
c) Write a main program for testing if the above function fails for any value
n?
d) Bonus: Can you design an efficient algorithm for isPrime() which should
take minimum number of divisions?

A Sample Code:

#include <iostream>
#include <math.h>
using namespace std;

bool IsPrime(int No)


{
for (int i = 2; i <= sqrt(No); i++)
if (!(No%i)) return false;
return true;
}
bool Check(int No)
{
return IsPrime((No*No) + No + 41);
}
int main()
{
int i = 1;
while (true)
{
if (!(Check(i)))
{
cout << i;
break;
}

i++;
}
system("Pause");
return 0;
}

Problem 2 -
a) Write a proposition function which on given values a, b, c, and d compute
if Euler's Conjecture a4 + b4 + c4 = d4 fails?
b) Write a program (main) which should test if there exists a 4 tuple on
which the above proposition functions returns TRUE?

A Sample Code:
#include <iostream>
#include <float.h>
using namespace std;

bool Check(double A , double B, double C, double D)


{
Discrete Structures – Fall 2018 Assignment-01 Solution

return ((A*A*A*A) + (B*B*B*B) + (C*C*C*C) == (D*D*D*D));


}

int main()
{
double i = 1;
while (true)
{
for (double j = 1; j <=i; j++)
{
for (double k = 1; k <= j; k++)
{
for (double l = 1; l <= k; l++)
{
if ((l*l*l*l) + (k*k*k*k) + (j*j*j*j)
== (i*i*i*i))
{
cout << i << endl << j << endl
<< k << endl << l << endl;
system("Pause");
}
if ((l*l*l*l) + (k*k*k*k) + (j*j*j*j) >
(i*i*i*i))
{
break;
}
}
}
}
i++;
}
system("Pause");
return 0;
}

Problem 3 -
a) Write a function which should test for a given triplet if it satisfies the
Elliptic Curve Conjecture 313(a3 + b3) = c3.
b) Write a program (main) which should test if there exists a 3 tuple (of
positive integers) on which the above proposition functions returns true.
How much time did it take for testing all the possibilities of integers?

A Sample Code:
#include <iostream>
using namespace std;

bool Check(double A, double B, double C)


{
return ((313 * ((A*A*A) + (B*B*B)) == (C*C*C))) ;
}

int main()
{
cout << Check(2, 3, 5);
double i = 1;
while (true)
Discrete Structures – Fall 2018 Assignment-01 Solution

{
for (double j = 1; j <= i; j++)
{
for (double k = 1; k <= j; k++)
{
if ((313 * ((j*j*j) + (k*k*k))) == (i*i*i))
{
cout << i << endl << j << endl << k <<
endl;
system("Pause");
}
if ((313 * ((j*j*j) + (k*k*k))) > (i*i*i))
{
break;
}
}
}
i++;
}

system("Pause");
return 0;
}

Problem 4 – (Puzzle of Knights and Knaves)


In an island, there live two types of people: KNIGHTS, who always tell the
truth, and KNAVES, who always lie.
You visit the island and meet two natives, who speak to you as follows:
A says: B is knight
B says: A and I are of opposite type.
What are A and B? (they might be both of same type or different)
Describe how you reach your answer. You may use any method to solve the
problem.

Answer:
Both are knave.
Why?:
Suppose A is knight. Then he must be saying truth. So B should be knight.
So if B is knight, then B statement becomes false, which should not be
possible (B being knight). This implies after first assumption that A is
knight is not true.
Now suppose A is knave, then A must be lying. So his statement that B is
knight should be false, which implies B should be knave. Now if B is knave
then his statement should also be false (that they are opposite type), that
means they are of the same type. So both are kanves.
Problem 5 -
In the back of an old cupboard, you discover a note signed by a pirate, who
has studied Discrete Structures and loves logical puzzles. In the note he
wrote, he had hidden a treasure somewhere in this big house. He listed five
TRUE statements. Based on these, find out the location of treasure.
a) If the house is next to the lake, the treasure is not in the kitchen.
b) If the tree in the front yard is an elm, then the treasure is in the
kitchen.
Discrete Structures – Fall 2018 Assignment-01 Solution

c) The house is next to the lake.


d) The tree in the front yard is an elm or the treasure is buried under the
flagpole.
e) If the tree in the backyard is an oak, then the treasure is in the garage.

Where is the treasure? Describe how have you figure out?

Answer:
Treasure is under the flagpole.
Why?:
The problem says ALL statements are TRUE.

c-statement says house in next to the lake. So the hypothesis of a-statement


is true, so treasure is not in the kitchen.

b-statement: Since treasure is not in the kitchen, so the tree in the front
yard is not an elm (by contraposition)

d-statement: Since the tree in the front yard is not an elm, so d-statement
to be true, the second part of OR should be true. That is treasure is buried
under the flagpole.

e-statement: If the hypothesis is true, then treasure must be in the garage,


but then statement-d would be false (but this should not be possible). So
hypothesis of e-statement should be false, then hypothesis could be false
(and implication would still be true).

Hence, the treasure should be under the flagpole.

Vous aimerez peut-être aussi