Thursday, May 14, 2020

Basic Problems (Level 3)

---------------------------------------------------------------------
What was the last topic discussed? Oh, factorization property of a number.
Although, there are different views by different peoples regarding the style of writing of an algorithm. In last article, out of first three Comments, Algorithm_04_01 was selected by two and Algorithm_04_02 was selected by one people.

In my view, Algorithm_04_01 is very clumsy by its structure where as Algorithm_04_02 is so clear, very smooth for the eye when you will read, but Algorithm_04_01 will give you tremendous stress in your eye(if you will read with concentration). Personally, I will prefer Algorithm_04_02 for the interest of your eyes, sorry peoples to whom I disagree.

Problem 05


Well, let us start with some alternative algorithms for checking a given number is prime or not.

Algorithm_05.01: Check_Prime(iNum, iIsPrime)

Inputs: iNum is the given integer.
Output: iIsPrime is an integer to hold the result as,
        iIsPrime= 0, implies iNum is not a prime number.
        iIsPrime= 1, implies iNum is a prime number.
Steps:
1. iIsPrime:= 1
2. iDivisor:= 2
3. WHILE ( iDivisor < (iNum-1) )
     IF ( (iNum%iDivisor) = 0 ) Then
       iIsPrime:= 0
       Return
     End of IF
     iDivisor:= (iDivisor+1)
   End of WHILE
4. Return

Now, consider the following changes,

Algorithm_05.02: Check_Prime(iNum, iIsPrime)

Inputs: iNum is the given integer.
Output: iIsPrime is an integer to hold the result as,
        iIsPrime= 0, implies iNum is not a prime number.
        iIsPrime= 1, implies iNum is a prime number.
Steps:
1. iIsPrime:= 1
2. iDivisor:= 2
3. iLimit:= (iNum-1)
4. WHILE ( iDivisor < iLimit )
     IF ( (iNum%iDivisor) = 0 ) Then
       iIsPrime:= 0
       Return
     End of IF
     iDivisor:= (iDivisor+1)
   End of WHILE
5. Return
Tell me,
Q1) Among 05.01 & 05.02, which one is better & why?
Again, consider the following changes,

Algorithm_05.03: Check_Prime(iNum, iIsPrime)

Inputs: iNum is the given integer.
Output: iIsPrime is an integer to hold the result as,
        iIsPrime= 0, implies iNum is not a prime number.
        iIsPrime= 1, implies iNum is a prime number.
Steps:
1. iIsPrime:= 1
2. iDivisor:= 2
3. iLimit:= (iNum/2)
4. WHILE ( iDivisor <= iLimit )
     IF ( (iNum%iDivisor) = 0 ) Then
       iIsPrime:= 0
       Return
     End of IF
     iDivisor:= (iDivisor+1)
   End of WHILE
5. Return
Again, consider the following changes,

Algorithm_05.04: Check_Prime(iNum, iIsPrime)

Inputs: iNum is the given integer.
Output: iIsPrime is an integer to hold the result as,
        iIsPrime= 0, implies iNum is not a prime number.
        iIsPrime= 1, implies iNum is a prime number.
Steps:
1. iIsPrime:= 1
2. iDivisor:= 2
3. iLimit:= SquareRoot(iNum)
4. WHILE ( iDivisor <= iLimit )
     IF ( (iNum%iDivisor) = 0 ) Then
       iIsPrime:= 0
       Return
     End of IF
     iDivisor:= (iDivisor+1)
   End of WHILE
5. Return
Tell me,
Q2) Among 05.02, 05.03 & 05.04 which one is better & why?

Problem 06


Problem Statement: