Skip to content

Commit

Permalink
feat: Function to check a number is Square free (#94)
Browse files Browse the repository at this point in the history
* Update DIRECTORY.md

* feat: Function to check a number is Square free

* Update DIRECTORY.md

Co-authored-by: autoprettier <[email protected]>
  • Loading branch information
MohdFaisalBidda and actions-user authored Jan 27, 2023
1 parent c609dab commit b1ac5d6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
7 changes: 5 additions & 2 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
* [Xor Cipher](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/ciphers/xor_cipher.ts)

## Data Structures
* [Stack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/stack.ts)
* [Array Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/array_queue.ts)
* [Linkedlist Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/linkedlist_queue.ts)
* [Linked Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/linked_queue.ts)
* [Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/queue.ts)
* [Stack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/stack.ts)
* [Stack Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/stack_queue.ts)

## Dynamic Programming
* [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/knapsack.ts)
Expand All @@ -27,6 +29,7 @@
* [Is Even](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_even.ts)
* [Is Leap Year](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_leap_year.ts)
* [Is Odd](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_odd.ts)
* [Is Square Free](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_square_free.ts)
* [Lowest Common Multiple](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/lowest_common_multiple.ts)
* [Perfect Square](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_square.ts)
* [Pronic Number](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/pronic_number.ts)
Expand Down
24 changes: 24 additions & 0 deletions maths/is_square_free.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @function isSquareFree
* @description A number is said to be square-free if no prime factor divides it more than once, i.e., the largest power of a prime factor that divides n is one.
* @param {number} n - A number.
* @return {boolean} - True if given number is a square free.
* @see https://www.geeksforgeeks.org/square-free-number/
* @example isSquareFree(10) = true
* @example isSquareFree(20) = false
*/

export const isSquareFree = (n: number): boolean => {

if (n < 0) throw new Error("number must be a natural number > 0");
if (n % 2 === 0) n = n / 2;
if (n % 2 === 0) return false;

for (let i: number = 3; i < Math.sqrt(n); i = i + 2) {
if (n % i === 0) {
n = n / i;
if (n % i === 0) return false;
}
}
return true;
}
11 changes: 11 additions & 0 deletions maths/test/is_square_free.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { isSquareFree } from '../is_square_free';

describe('isSquareFree', () => {
test('should return correct boolean value', () => {
expect(isSquareFree(1)).toBe(true);
expect(isSquareFree(10)).toBe(true);
expect(isSquareFree(20)).toBe(false);
expect(isSquareFree(26)).toBe(true);
expect(isSquareFree(48)).toBe(false);
});
});

0 comments on commit b1ac5d6

Please sign in to comment.