From 358c3ed3cf953571bb12567d172be46e20ac7509 Mon Sep 17 00:00:00 2001 From: autoprettier Date: Thu, 26 Jan 2023 13:22:49 +0000 Subject: [PATCH 1/3] Update DIRECTORY.md --- DIRECTORY.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index b96d789d..b8d4792d 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) From 71f19d539090564319c04645037e9fa8047cddf1 Mon Sep 17 00:00:00 2001 From: Faisal Date: Thu, 26 Jan 2023 22:37:53 +0530 Subject: [PATCH 2/3] feat: Function to check a number is Square free --- maths/is_square_free.ts | 24 ++++++++++++++++++++++++ maths/test/is_square_free.test.ts | 11 +++++++++++ 2 files changed, 35 insertions(+) create mode 100644 maths/is_square_free.ts create mode 100644 maths/test/is_square_free.test.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 From c01c82bec5eaccdbb43d2690ffb9a202faccf4c1 Mon Sep 17 00:00:00 2001 From: autoprettier Date: Thu, 26 Jan 2023 17:08:43 +0000 Subject: [PATCH 3/3] Update DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index b8d4792d..3f34cc78 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -29,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)