By Kardi Teknomo, PhD .

Prime Factor Using Spreadsheet


< Previous | Next | Content >

Prime Factor Using Spreadsheet

Knowing the algorithm, now we proceed with how we will use spreadsheet iteration (MS Excel 1993/1997) to calculate prime factor of any positive number (we limit ourselves up to 7 digits). Spreadsheet companion of this tutorial can be downloaded here . It's free of charge. The programming challenge is to make a Do-While loop in parallel cells without real loop.

1. We set up a control cell. Suppose cell B3 will be used as control cell. We will fill this cell with any value or text (for example we put text "Delete Me" on it) and color the cell blue such that user can distinguish it. We also give name to this cell by menu Insert > Name > Define > "Control"

Prime Factor Using Spreadsheet

2. We prepare a cell for input number and color it as yellow. We name it Prime Factor Using Spreadsheet . For example, we set cell B9 as input cell.

Prime Factor Using Spreadsheet

3. Now we prepare the cells to actually show the prime factors. Say we want to have a maximum of 17 prime factors (you can do more factors or less factors). Thus, we occupy cell D8 to T8 for the counter of the columns representing the prime factor. We put 1 on D8 and put a formula of =+D8+1 on E8 and copy this formula from F8 to T8. For temporary, we just fill all cells from D9 to T9 with 1. You will end up with the following figure.

Prime Factor Using Spreadsheet

4. The actual computation of prime factor will use iteration. Thus we set menu Tools > Options ?> Calculation Tab > click Iteration Checkbox and set maximum iterations to say 10,000.

Prime Factor Using Spreadsheet

5. For the computation part, we set up 5 cells in B14 to B18 and name them respectively Prime Factor Using Spreadsheet for the counter of column, Prime Factor Using Spreadsheet for the result of integer division, Prime Factor Using Spreadsheet for the nominator, Prime Factor Using Spreadsheet for denominator and Prime Factor Using Spreadsheet for the product of the prime factors so far.

Prime Factor Using Spreadsheet

6. Product of all prime factors is put in cell B18

=+PRODUCT(D9:T9)

This product will be used to examine if all the prime factors has been computed.

Prime Factor Using Spreadsheet

7. Our original number has name Prime Factor Using Spreadsheet and we would like to repeatedly divide this number with the smallest prime number. First, we need to give another name for Prime Factor Using Spreadsheet (or copy the value in another cell) such that after division, the result of division would be our number and will not affect the value of Prime Factor Using Spreadsheet . Let us call our number Prime Factor Using Spreadsheet to indicate nominator of division. Thus, we set Prime Factor Using Spreadsheet for the initial condition (that is if the control cell is not empty). The result of division (which is also Prime Factor Using Spreadsheet ) will be used again for further division if it is divisible. The division take place only if our number Prime Factor Using Spreadsheet is divisible by our smallest prime factor name Prime Factor Using Spreadsheet (= cell B17). We use modulus function (MOD) to examine Prime Factor Using Spreadsheet is divisible by Prime Factor Using Spreadsheet (that is to set equal to zero). Therefore, the result of division also has the same cell name of Prime Factor Using Spreadsheet (= cell B16) and we put all of these things together by typing in cell B16

=+IF(Control="",IF(MOD(n, a)=0,n/a,n),v)

The formulas above shall be explained as follow:

If control cell is empty then

If Prime Factor Using Spreadsheet then

Prime Factor Using Spreadsheet

Else,

Do not change Prime Factor Using Spreadsheet

End If

Else

Let Prime Factor Using Spreadsheet

End If

Prime Factor Using Spreadsheet

8. Our next computational cell is to find the smallest prime factor. Let us name this cell Prime Factor Using Spreadsheet in cell B17, type

=+IF(AND(Control="", product<>v),IF(p=1,a+1,1),1)

The formula above can be explained as follow:

If control cell is empty and Prime Factor Using Spreadsheet of all prime factor is not the same as our original number then

If Prime Factor Using Spreadsheet is not divisible by Prime Factor Using Spreadsheet (therefore, Prime Factor Using Spreadsheet ) then

Increment Prime Factor Using Spreadsheet

Else,

Set Prime Factor Using Spreadsheet

End If

Else

Set Prime Factor Using Spreadsheet (for the first time)

End If

Prime Factor Using Spreadsheet

9. The next computational cell is actually a transitional cell to show the result of integer division, before we copy this result into the appropriate cell. Let us name this transitional cell as Prime Factor Using Spreadsheet and locate this in cell B16:

=+IF(Control="",IF(MOD(n,a)=0,a,1),1)

The meaning of this formula is quite straightforward. If our number Prime Factor Using Spreadsheet is divisible by our smallest prime factor Prime Factor Using Spreadsheet then we set Prime Factor Using Spreadsheet , otherwise we set Prime Factor Using Spreadsheet as our default value.

Prime Factor Using Spreadsheet

10. Then we need a counter of column number where we can copy our smallest prime factor into the correct cell. Let us call this variable Prime Factor Using Spreadsheet and locate it in cell B15:

=+IF(Control="",IF(n=1,0,IF(AND(n>1,p>1),t+1,t)),1)

In here we set initial column number as one if the control cell is not empty. If the control cell is empty, we examine if the loop have been finished. The loop would finish if the product of all prime factor is the same as our original number (that is equivalent to Prime Factor Using Spreadsheet ). In this case, we make agreement to set Prime Factor Using Spreadsheet to indicate the loop is finished. Otherwise, we need examine if the computation still going on (that is indicated by Prime Factor Using Spreadsheet ). There are two cases in the on going computation:

The transitional cell Prime Factor Using Spreadsheet indicates we just copy the transitional value of smallest prime factor Prime Factor Using Spreadsheet into the appropriate cell

The transitional cell Prime Factor Using Spreadsheet indicates we just copy the value of smallest prime factor Prime Factor Using Spreadsheet into Prime Factor Using Spreadsheet

You can see this clearly if you set the Maximum Iteration to 1 instead of 10,000.

Prime Factor Using Spreadsheet

11. Finally, we type the following to copy the value of smallest prime factor Prime Factor Using Spreadsheet into the appropriate cell in cell D9:

=+IF(Control="",IF(t=D$8,p,IF(OR(D9>1,t=0),D9,1)),1)

The meaning of above formula is simply to copy the value of Prime Factor Using Spreadsheet if the column counter Prime Factor Using Spreadsheet is equal to the column number above this cell. Otherwise, there are three cases:

  1. By our agreement if Prime Factor Using Spreadsheet , the computation is finished and we keep the value of the cell.
  2. Alternatively, if the value of this cell has been filled (therefore it is greater than 1), we also want to keep the value of the cell.
  3. Otherwise, we just set the value of this cell as one.

After that we copy the formula from cell D9 to E9:T9

Prime Factor Using Spreadsheet

12. We do conditional formatting such that the column number and cells with value smaller or equal to 1 would be painted with white. We make it colorful and nicer table. The final result is shown below. How to use the spreadsheet is also remarked in the spreadsheet notes.

Prime Factor Using Spreadsheet

< Previous | Next | Content >

This tutorial is copyrighted .