Assignment 2 - Frequently Asked Questions

Q. What are functions? How do we write them in C/C++?

Functions are blocks which contain meaningful code. Each function in a nicely designed C program will have a specific purpose. More information about functions and how to code them can be found in chapter 5 section 5.1.


Q. How can one determine if a number is prime or not?

The answer to this question goes back to school level math. Prime numbers are those numbers which are not divisible by any other numbers except for 1. Prime numbers today are used in information security where extremely large prime numbers (up to 150 - 200 digits) can be used to generate keys.
To determine if a number is prime or not, one can try dividing that particular number by all the numbers which are lesser than itself. Say for example we needed a way to find out if x was prime or not. For that one would have to divide x by all the numbers which are less than x. Offcourse division by 1 would be out of question since 1 is a universal divisor. If any of the divisions produce a 0 remainder, means that x can be divisible by a number other then 1 and hence x is not prime. If no number divides x exactly, it means that x is a prime.
There are many different ways to shorten the problem space and you are free to research them over the internet


Q. No Exactly how can I determine if x is prime or not?

Here is a short algorihmic form of solving your problem. Again you are free to try out your own way.

function test_prime(int j) ; output bool begin
for all numbers between 2 and j, check if division of j with that number returns any remainder or not.
If there is even a single case where the above step has 0 remainder j is not a prime(return false). On the other hand if none of the remainders are 0 then j is prime(return true)
end



Back