Skip to content

Commit c7f9bf9

Browse files
algorithm: Square free integer (TheAlgorithms#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
1 parent cf0593f commit c7f9bf9

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Maths/IsSquareFree.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
3+
* Square free integer: https://en.wikipedia.org/wiki/Square-free_integer
4+
* function to check if an integer has repeated prime factors.
5+
* return false if the number as repeated prime factors.
6+
* else true
7+
*/
8+
9+
/**
10+
* @function isSquareFree
11+
* @description -> Checking if number is square free using prime factorization
12+
* @param {number} number
13+
* @returns {boolean} true if the number has unique prime factors, otherwise false
14+
*/
15+
16+
import { PrimeFactors } from './PrimeFactors.js'
17+
export const isSquareFree = (number) => {
18+
const primeFactorsArray = PrimeFactors(number)
19+
if (number <= 0) {
20+
throw new Error('Number must be greater than zero.')
21+
}
22+
return primeFactorsArray.length === new Set(primeFactorsArray).size
23+
}

Maths/test/IsSquareFree.test.js

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy