Professional Documents
Culture Documents
Assignment 01 Solution
Assignment 01 Solution
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;
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;
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;
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;
}
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
Answer:
Treasure is under the flagpole.
Why?:
The problem says ALL statements are TRUE.
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.