Usage of Blockly in integer factorization calculator

  1. Alpertron
  2. Videos of Integer factorization calculator
  3. Blockly

by Dario Alejandro Alpern


Hello everybody. Today I will show you how to use Blockly in the integer factorization calculator.

This calculator has several modes of operation. You can enter a number or a numerical expression, such as 10 to the 59 plus 213 and then press the Factor button to obtain its prime factors, along with other useful information.

You can factor many numbers by typing a loop. The example that is being copied computes the first 100 odd prime numbers minus 1 and then factors them.

Instead of pressing the Factor button, you could press the Only evaluate button, just to print the numbers that were calculated, or the Prime button, which shows whether the numbers are prime or not instead of giving a complete factorization.

Blockly is a visual programming language that uses blocks to represent loops, printing instructions and operations with numbers.

At the left you can see the toolbox, with different categories of blocks:

We will use some blocks to create a program that factors all odd prime numbers minus 1 less than 1000. This will require a variable to hold the prime number. We will call it x.

Now we drag the set variable block to the workspace. The workspace will hold all the blocks that form the program. Then we drag the number that is in the Basic math category. We have to insert the tab of the number block in the notch of the set variable block. Then we change the number to 3, which is the first odd prime number.

Now we have to drag the repeat block to the workspace to be just below the set variable block. The repeat block is in the Flow Control category.

The repeat loop must be done while the prime x is less or equal to 1000. So we will need the comparison block. Then we drag the get variable block to overwrite the left operator of the comparison block, and then we change the right operator to 1000.

Now we have to insert the blocks that form the body of the repeat loop.

We drag the print prime factors block to the workspace that is in the Output category. We have to print the factors of x minus 1. So we have to overwrite the number 5 by the minus operation which is in the Basic math category. Then we change the default operators with the expected ones.

We have to change the value of the variable x to hold the next prime number, so we have to drag the set variable block and the next prime after block. Finally we overwrite the default value by the variable x.

When we run the program, using the Run button, we will see that the calculator exits Blockly mode and shows the results, that can be seen by scrolling down.

Now we go back to Blockly mode and save the program. To do that, we write the name of the program and then press the Save button.

If we refresh the page or close the navigator and go back to the calculator, after entering Blockly mode you will see that the workspace is empty. To get the program again, you will type the name of the program and then press the Load button. Then if we run the program, we will see the same results.

Finally you can delete all blocks by pressing the Delete blocks button.

That is all for today. I hope it is useful for you.