mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-12-19 06:58:15 +08:00
chore: Merge pull request #799 from VinWare/master
Extended Eucliedian Algorithm added to Maths folder
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
// Foot: https://en.wikipedia.org/wiki/Foot_(unit)
|
||||
const feetToMeter = (feet) => {
|
||||
return feet*0.3048;
|
||||
return feet * 0.3048
|
||||
}
|
||||
|
||||
const meterToFeet = (meter) => {
|
||||
return meter/0.3048;
|
||||
return meter / 0.3048
|
||||
}
|
||||
|
||||
export { feetToMeter, meterToFeet }
|
||||
export { feetToMeter, meterToFeet }
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { meterToFeet, feetToMeter } from "../MeterToFeetConversion";
|
||||
import { meterToFeet, feetToMeter } from '../MeterToFeetConversion'
|
||||
|
||||
describe('Testing conversion of Meter to Feet', () => {
|
||||
it('with feet value', () => {
|
||||
@@ -10,4 +10,4 @@ describe('Testing conversion of Feet to Meter', () => {
|
||||
it('with feet value', () => {
|
||||
expect(feetToMeter(10)).toBe(3.048)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -5,15 +5,15 @@
|
||||
* @param {number[]} arrayItems
|
||||
* @returns number
|
||||
*/
|
||||
export function maxProductOfThree(arrayItems) {
|
||||
export function maxProductOfThree (arrayItems) {
|
||||
// if size is less than 3, no triplet exists
|
||||
let n = arrayItems.length
|
||||
const n = arrayItems.length
|
||||
if (n < 3) throw new Error('Triplet cannot exist with the given array')
|
||||
let max1 = arrayItems[0],
|
||||
max2 = -1,
|
||||
max3 = -1,
|
||||
min1 = arrayItems[0],
|
||||
min2 = -1
|
||||
let max1 = arrayItems[0]
|
||||
let max2 = -1
|
||||
let max3 = -1
|
||||
let min1 = arrayItems[0]
|
||||
let min2 = -1
|
||||
for (let i = 1; i < n; i++) {
|
||||
if (arrayItems[i] > max1) {
|
||||
max3 = max2
|
||||
@@ -32,7 +32,7 @@ export function maxProductOfThree(arrayItems) {
|
||||
min2 = arrayItems[i]
|
||||
}
|
||||
}
|
||||
let prod1 = max1 * max2 * max3,
|
||||
prod2 = max1 * min1 * min2
|
||||
const prod1 = max1 * max2 * max3
|
||||
const prod2 = max1 * min1 * min2
|
||||
return Math.max(prod1, prod2)
|
||||
}
|
||||
|
||||
71
Maths/ExtendedEuclideanGCD.js
Normal file
71
Maths/ExtendedEuclideanGCD.js
Normal file
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Problem statement and explanation: https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
|
||||
*
|
||||
* This algorithm plays an important role for modular arithmetic, and by extension for cyptography algorithms
|
||||
*
|
||||
* Basic explanation:
|
||||
* The Extended Euclidean algorithm is a modification of the standard Euclidean GCD algorithm.
|
||||
* It allows to calculate coefficients x and y for the equation:
|
||||
* ax + by = gcd(a,b)
|
||||
*
|
||||
* This is called Bézout's identity and the coefficients are called Bézout coefficients
|
||||
*
|
||||
* The algorithm uses the Euclidean method of getting remainder:
|
||||
* r_i+1 = r_i-1 - qi*ri
|
||||
* and applies it to series s and t (with same quotient q at each stage)
|
||||
* When r_n reaches 0, the value r_n-1 gives the gcd, and s_n-1 and t_n-1 give the coefficients
|
||||
*
|
||||
* This implementation uses an iterative approach to calculate the values
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Number} arg1 first argument
|
||||
* @param {Number} arg2 second argument
|
||||
* @returns Array with GCD and first and second Bézout coefficients
|
||||
*/
|
||||
const extendedEuclideanGCD = (arg1, arg2) => {
|
||||
if (typeof arg1 !== 'number' || typeof arg2 !== 'number') throw new TypeError('Not a Number')
|
||||
if (arg1 < 1 || arg2 < 1) throw new TypeError('Must be positive numbers')
|
||||
|
||||
// Make the order of coefficients correct, as the algorithm assumes r0 > r1
|
||||
if (arg1 < arg2) {
|
||||
const res = extendedEuclideanGCD(arg2, arg1)
|
||||
const temp = res[1]
|
||||
res[1] = res[2]
|
||||
res[2] = temp
|
||||
return res
|
||||
}
|
||||
|
||||
// At this point arg1 > arg2
|
||||
|
||||
// Remainder values
|
||||
let r0 = arg1
|
||||
let r1 = arg2
|
||||
|
||||
// Coefficient1 values
|
||||
let s0 = 1
|
||||
let s1 = 0
|
||||
|
||||
// Coefficient 2 values
|
||||
let t0 = 0
|
||||
let t1 = 1
|
||||
|
||||
while (r1 !== 0) {
|
||||
const q = Math.floor(r0 / r1)
|
||||
|
||||
const r2 = r0 - r1 * q
|
||||
const s2 = s0 - s1 * q
|
||||
const t2 = t0 - t1 * q
|
||||
|
||||
r0 = r1
|
||||
r1 = r2
|
||||
s0 = s1
|
||||
s1 = s2
|
||||
t0 = t1
|
||||
t1 = t2
|
||||
}
|
||||
return [r0, s0, t0]
|
||||
}
|
||||
|
||||
export { extendedEuclideanGCD }
|
||||
@@ -1,20 +1,19 @@
|
||||
/**
|
||||
Problem Statment and Explanation :
|
||||
Problem Statment and Explanation :
|
||||
Triangular => https://en.wikipedia.org/wiki/Triangular_number
|
||||
Tetrahedral => https://en.wikipedia.org/wiki/Tetrahedral_number
|
||||
Pentatope => https://en.wikipedia.org/wiki/Pentatope_number
|
||||
|
||||
|
||||
Example:
|
||||
Example:
|
||||
Triangular => (0, 1, 3, 6, 10, 15, 21, 28, 36, 45)
|
||||
Tetrahedral => (1, 4, 10, 20, 35, 56, 84, 120, 165,)
|
||||
Pentatope => (1, 5, 15, 35, 70, 126, 210, 330, 495)
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} number
|
||||
* @returns
|
||||
*
|
||||
* @param {*} number
|
||||
* @returns
|
||||
*/
|
||||
const isTriangular = (number) => {
|
||||
for (let i = 0; i <= number; i++) {
|
||||
@@ -28,9 +27,9 @@ const isTriangular = (number) => {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} number
|
||||
* @returns
|
||||
*
|
||||
* @param {*} number
|
||||
* @returns
|
||||
*/
|
||||
const isTetrahedral = (number) => {
|
||||
for (let i = 1; i <= number; i++) {
|
||||
@@ -43,9 +42,9 @@ const isTetrahedral = (number) => {
|
||||
return false
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param {*} number
|
||||
* @returns
|
||||
*
|
||||
* @param {*} number
|
||||
* @returns
|
||||
*/
|
||||
const isPentatope = (number) => {
|
||||
for (let i = 1; i <= number; i++) {
|
||||
@@ -59,11 +58,11 @@ const isPentatope = (number) => {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} number
|
||||
* @returns
|
||||
*
|
||||
* @param {*} number
|
||||
* @returns
|
||||
*/
|
||||
let checkAll = (number) => {
|
||||
const checkAll = (number) => {
|
||||
return {
|
||||
isTriangular: isTriangular(number),
|
||||
isTetrahedral: isTetrahedral(number),
|
||||
|
||||
16
Maths/test/ExtendedEuclideanGCD.test.js
Normal file
16
Maths/test/ExtendedEuclideanGCD.test.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import { extendedEuclideanGCD } from '../ExtendedEuclideanGCD'
|
||||
|
||||
describe('extendedEuclideanGCD', () => {
|
||||
it('should return valid values in order for positive arguments', () => {
|
||||
expect(extendedEuclideanGCD(240, 46)).toMatchObject([2, -9, 47])
|
||||
expect(extendedEuclideanGCD(46, 240)).toMatchObject([2, 47, -9])
|
||||
})
|
||||
it('should give error on non-positive arguments', () => {
|
||||
expect(() => extendedEuclideanGCD(0, 240)).toThrowError(new TypeError('Must be positive numbers'))
|
||||
expect(() => extendedEuclideanGCD(46, -240)).toThrowError(new TypeError('Must be positive numbers'))
|
||||
})
|
||||
it('should give error on non-numeric arguments', () => {
|
||||
expect(() => extendedEuclideanGCD('240', 46)).toThrowError(new TypeError('Not a Number'))
|
||||
expect(() => extendedEuclideanGCD([240, 46])).toThrowError(new TypeError('Not a Number'))
|
||||
})
|
||||
})
|
||||
@@ -54,7 +54,7 @@ describe('FigurateNumber', () => {
|
||||
})
|
||||
/** End */
|
||||
|
||||
it('Check All : should return all true',() => {
|
||||
it('Check All : should return all true', () => {
|
||||
expect(checkAll(1)).toEqual({
|
||||
isTriangular: true,
|
||||
isTetrahedral: true,
|
||||
@@ -62,7 +62,7 @@ describe('FigurateNumber', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('Check All : should return all true,true,false',() => {
|
||||
it('Check All : should return all true,true,false', () => {
|
||||
expect(checkAll(15)).toEqual({
|
||||
isTriangular: true,
|
||||
isTetrahedral: false,
|
||||
|
||||
Reference in New Issue
Block a user