mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00
algorithm: Square free integer (#1104)
* feat: Add square free integer implementation * test: Add unit tests for square free integer * refactor: Remove trailing whitespace from line 27 * refactor: Fix comments
This commit is contained in:
23
Maths/IsSquareFree.js
Normal file
23
Maths/IsSquareFree.js
Normal file
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
|
||||
* Square free integer: https://en.wikipedia.org/wiki/Square-free_integer
|
||||
* function to check if an integer has repeated prime factors.
|
||||
* return false if the number as repeated prime factors.
|
||||
* else true
|
||||
*/
|
||||
|
||||
/**
|
||||
* @function isSquareFree
|
||||
* @description -> Checking if number is square free using prime factorization
|
||||
* @param {number} number
|
||||
* @returns {boolean} true if the number has unique prime factors, otherwise false
|
||||
*/
|
||||
|
||||
import { PrimeFactors } from './PrimeFactors.js'
|
||||
export const isSquareFree = (number) => {
|
||||
const primeFactorsArray = PrimeFactors(number)
|
||||
if (number <= 0) {
|
||||
throw new Error('Number must be greater than zero.')
|
||||
}
|
||||
return primeFactorsArray.length === new Set(primeFactorsArray).size
|
||||
}
|
20
Maths/test/IsSquareFree.test.js
Normal file
20
Maths/test/IsSquareFree.test.js
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user