diff --git a/DIRECTORY.md b/DIRECTORY.md index b96d789d..3f34cc78 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) @@ -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) diff --git a/maths/is_square_free.ts b/maths/is_square_free.ts new file mode 100644 index 00000000..0e55ab7f --- /dev/null +++ b/maths/is_square_free.ts @@ -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; +} \ No newline at end of file diff --git a/maths/test/is_square_free.test.ts b/maths/test/is_square_free.test.ts new file mode 100644 index 00000000..bbb16991 --- /dev/null +++ b/maths/test/is_square_free.test.ts @@ -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); + }); +}); \ No newline at end of file