Factors Of n -NPM-

Nora LC
3 min readJul 29, 2021

How to’s: Get the factors (the function) & Publish the npm package

from my package page

A factor of a Number is a whole number, that when multiplied with another, produces that number.

Factors are an essential element in maths, used to solve problems including real-life ones. I created an NPM package that provides access to a function that returns a sorted array of all factors of a positive integer. Let’s explore how I did it in the following lines.

The Algorithm

The approach starts with understanding and collecting as much data as possible about the concept. First, we know that number n is purely divisible by its factors. So, now, we know that any number n itself and the number 1 are ALWAYS factors.

Another thing that we can tackle from the start is handling the wrong input. In our case, meaning when n is equal to 0 or not a positive number. It is just not really valid to want to find factors of 0. Pretty similar to when thinking about negative numbers. -n has the same factors of n in addition to ‘negative them’. For Eg. Factors of 10 are 10, 5, 2, 1, and factors of -10 are 10, 5, 2, 1 (the same) in addition to -10, -5, -2, -1. So our function would only handle positive numbers as all we would need to do in case of a negative number is to duplicate them denoted with a minus sign preceding them.

This is how I started my code => by handling the case of wrong input -throw- and by determining our first two factors -n and 1-.

// can't handle n <= 0, function to throwif (n <= 0) throw 'Input n must be greater than 0';// 1 and the number itselflet factors = [1, n]

I wanted to use the square root of n to iterate through all possibilities up to it. Then every time we find a factor in our lower interval 3 to the square root of n (if n is an odd integer) or 2 to the square root of n (if n is an even integer) to find its complement or inverse from the upper interval. Here is what the loop would look like:

Github Repo

After getting our factors array, we could then sort it and return it. The final code would be shown in the next section when covering the NPM package specifications.

Initializing and Publishing the Package

To give other developers wanting to use the package the ability to import/access the function, I made sure to add the export statement. Here is the complete code:

The following are the steps to finally publishing the package on NPM:

  • Create an NPM profile if not already available.
  • Log in from the command line by running npm login.
  • I rannpm init to create and fill out the package.json file (my example below)
  • I installed mocha for unit testing.
  • I tested, finished a README and published by running npm publish.

tadaaaa… That’s all.

The Package.json

{"name": "factors-of-n","version": "1.0.0","description": "A library to find factors for a given input","main": "src/index.js","scripts": {"test": "mocha"},"keywords": ["factor","math","factors","getfactors"],"author": "Noura Loudani","license": "ISC","devDependencies": {"mocha": "^9.0.3"}}

Conclusion

Publishing node modules is pretty simple and super beneficial. The modules are accessible by anyone that needs them. I hope the blog post was helpful to read. Here are the Github repo link and the NPM page link.

--

--