Author: Theodore Odeluga
The source files for this project can be downloaded from here
This is a Node command line project and Node.js can be downloaded from here
Instructions for running a command line program with Node.js can be found here
In this tutorial we'll create two versions of a program working with square numbers.
The first will be a simple utility to identify all the square numbers in a given range.
The second will take any integer and find all its factors as products of a number multiplied by itself.
Let's begin.
In the first iteration, we generate a range of 1000 and find all the square numbers in this range.
As noted by my comments in the code sample, I noticed a bug (which I couldn't fix before writing but which I found a workaround for) where the value assigned to the limit variable is short by 100.
The 'fix' seems to work but as yet, I've been unable to explain it. I'll publish the results here if further investigation yields any answers.
The squares array (currently empty) will contain all instances of the square variable and the square variable itself will contain the results of multiplying j by itself.
j will also double up as an iterator in a for loop running through a range defined by the limit variable.
Within this structure, the square variable will equate to j multiplying by j, the squares array will be populated by instances of square (resulting from the j multiplication) and a condition will test to make sure the values of j never exceed the limit imposed by limit.
Here's the version for finding squares in a given range.
/*Problem: Find all square numbers between 1 and 1000*/
let limit = 900;
let squares = [];
let square;
let j;
for(j=0; j < limit; j++){
square = j * j;
squares.push(square);
if(squares[j] > limit){
break;
}
}
console.log(squares);
The next version of the code is more versatile.
Here we not only find the square products within the range of a given number but those which are factors of that selected number.
As above, we create an empty array for square numbers, a variable to contain the squares themselves and an iterator.
In this example, the limit variable is replaced by the myinteger variable to hold the integer being factorised.
Similar to the above code, a loop (a while loop this time) runs against the myinteger variable as the limit.
During this process, the i iterator is incremented and at each iteration, a square value is generated.
The generation of square values are stored in square as described and then tested against myinteger for equal divisibility.
If this test returns true, the particular instance of square being checked at that point is pushed into the squares array.
It's a handy utility and a simple way to find the relationships between an integer and its significant component numbers.
Here's the sample for this version of the code.
/*Problem: Find the square factors of any integer*/
let myinteger = 5000;
let squares = [];
let i = 0;
let square;
while(i < myinteger){
i++;
square = i * i;
if(myinteger % square == 0){
squares.push(square);
}
}
console.log(squares);
Conclusion
I hope the above comes in handy for any code you might write involving factorisation and squares. A reversal of the functionality could be used to identify square roots. See what you can come up with by playing around with it. Thanks for reading and happy coding.