Skip to content

Commit b1ac5d6

Browse files
feat: Function to check a number is Square free (#94)
* Update DIRECTORY.md * feat: Function to check a number is Square free * Update DIRECTORY.md Co-authored-by: autoprettier <[email protected]>
1 parent c609dab commit b1ac5d6

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

DIRECTORY.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
* [Xor Cipher](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/ciphers/xor_cipher.ts)
44

55
## Data Structures
6-
* [Stack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/stack.ts)
76
* [Array Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/array_queue.ts)
8-
* [Linkedlist Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/linkedlist_queue.ts)
7+
* [Linked Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/linked_queue.ts)
8+
* [Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/queue.ts)
9+
* [Stack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/stack.ts)
10+
* [Stack Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/stack_queue.ts)
911

1012
## Dynamic Programming
1113
* [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/knapsack.ts)
@@ -27,6 +29,7 @@
2729
* [Is Even](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_even.ts)
2830
* [Is Leap Year](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_leap_year.ts)
2931
* [Is Odd](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_odd.ts)
32+
* [Is Square Free](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_square_free.ts)
3033
* [Lowest Common Multiple](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/lowest_common_multiple.ts)
3134
* [Perfect Square](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_square.ts)
3235
* [Pronic Number](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/pronic_number.ts)

maths/is_square_free.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @function isSquareFree
3+
* @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.
4+
* @param {number} n - A number.
5+
* @return {boolean} - True if given number is a square free.
6+
* @see https://www.geeksforgeeks.org/square-free-number/
7+
* @example isSquareFree(10) = true
8+
* @example isSquareFree(20) = false
9+
*/
10+
11+
export const isSquareFree = (n: number): boolean => {
12+
13+
if (n < 0) throw new Error("number must be a natural number > 0");
14+
if (n % 2 === 0) n = n / 2;
15+
if (n % 2 === 0) return false;
16+
17+
for (let i: number = 3; i < Math.sqrt(n); i = i + 2) {
18+
if (n % i === 0) {
19+
n = n / i;
20+
if (n % i === 0) return false;
21+
}
22+
}
23+
return true;
24+
}

maths/test/is_square_free.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { isSquareFree } from '../is_square_free';
2+
3+
describe('isSquareFree', () => {
4+
test('should return correct boolean value', () => {
5+
expect(isSquareFree(1)).toBe(true);
6+
expect(isSquareFree(10)).toBe(true);
7+
expect(isSquareFree(20)).toBe(false);
8+
expect(isSquareFree(26)).toBe(true);
9+
expect(isSquareFree(48)).toBe(false);
10+
});
11+
});

0 commit comments

Comments
 (0)