Prime Number in Typescript

Published on 05 October 2023 (Updated: 21 October 2023)

Welcome to the Prime Number in Typescript page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

function isPrime(num: number): boolean {
  if (num < 2) {
    return false;
  }
  for (let i = 2; i <= Math.sqrt(num); i++) {
    if (num % i === 0) {
      return false;
    }
  }
  return true;
}

function main() {
  if (process.argv.length !== 3) {
    console.log('Usage: please input a non-negative integer');
    return;
  }

  const inputNum = parseFloat(process.argv[2]);

  if (isNaN(inputNum) || inputNum < 0 || inputNum % 1 !== 0) {
    console.log('Usage: please input a non-negative integer');
    return;
  }

  if (isPrime(inputNum)) {
    console.log('prime');
  } else {
    console.log('composite');
  }
}
main();

Prime Number in Typescript was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.