diff --git a/Bit-Manipulation/BinaryCountSetBits.js b/Bit-Manipulation/BinaryCountSetBits.js index b879f3bd67..b959caf062 100644 --- a/Bit-Manipulation/BinaryCountSetBits.js +++ b/Bit-Manipulation/BinaryCountSetBits.js @@ -16,7 +16,7 @@ function BinaryCountSetBits(a) { let count = 0 while (a) { - a &= (a - 1) + a &= a - 1 count++ } diff --git a/DIRECTORY.md b/DIRECTORY.md index aea7b1548f..72b8a04415 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -166,6 +166,7 @@ * [Area](Maths/Area.js) * [ArithmeticGeometricMean](Maths/ArithmeticGeometricMean.js) * [ArmstrongNumber](Maths/ArmstrongNumber.js) + * [AutomorphicNumber](Maths/AutomorphicNumber.js) * [AverageMean](Maths/AverageMean.js) * [AverageMedian](Maths/AverageMedian.js) * [BinaryConvert](Maths/BinaryConvert.js) @@ -280,6 +281,7 @@ * [Problem016](Project-Euler/Problem016.js) * [Problem017](Project-Euler/Problem017.js) * [Problem018](Project-Euler/Problem018.js) + * [Problem019](Project-Euler/Problem019.js) * [Problem020](Project-Euler/Problem020.js) * [Problem021](Project-Euler/Problem021.js) * [Problem023](Project-Euler/Problem023.js) diff --git a/Maths/AutomorphicNumber.js b/Maths/AutomorphicNumber.js index d1b6608316..ba008271fc 100644 --- a/Maths/AutomorphicNumber.js +++ b/Maths/AutomorphicNumber.js @@ -35,6 +35,6 @@ export const isAutomorphic = (n) => { n = Math.floor(n / 10) n_sq = Math.floor(n_sq / 10) } - + return true } diff --git a/Maths/test/AutomorphicNumber.test.js b/Maths/test/AutomorphicNumber.test.js index 19b963388c..57f40d27ee 100644 --- a/Maths/test/AutomorphicNumber.test.js +++ b/Maths/test/AutomorphicNumber.test.js @@ -9,19 +9,19 @@ describe('AutomorphicNumber', () => { }) test.each([ - { n: -3 , expected: false }, - { n: -25 , expected: false }, + { n: -3, expected: false }, + { n: -25, expected: false } ])('should return false when n is negetive', ({ n, expected }) => { expect(isAutomorphic(n)).toBe(false) }) test.each([ - { n: 7 , expected: false }, - { n: 83 , expected: false }, - { n: 0 , expected: true }, - { n: 1 , expected: true }, - { n: 376 , expected: true }, - { n: 90625 , expected: true }, + { n: 7, expected: false }, + { n: 83, expected: false }, + { n: 0, expected: true }, + { n: 1, expected: true }, + { n: 376, expected: true }, + { n: 90625, expected: true } ])('should return $expected when n is $n', ({ n, expected }) => { expect(isAutomorphic(n)).toBe(expected) }) diff --git a/Project-Euler/Problem006.js b/Project-Euler/Problem006.js index 474de2ae96..804b165558 100644 --- a/Project-Euler/Problem006.js +++ b/Project-Euler/Problem006.js @@ -1,8 +1,8 @@ // https://projecteuler.net/problem=6 export const squareDifference = (num = 100) => { - let sumOfSquares = (num)*(num+1)*(2*num+1)/6 - let sums = (num*(num+1))/2 - + let sumOfSquares = (num * (num + 1) * (2 * num + 1)) / 6 + let sums = (num * (num + 1)) / 2 + return sums ** 2 - sumOfSquares // difference of square of the total sum and sum of squares } diff --git a/Project-Euler/Problem019.js b/Project-Euler/Problem019.js new file mode 100644 index 0000000000..79a3ad7c43 --- /dev/null +++ b/Project-Euler/Problem019.js @@ -0,0 +1,19 @@ +// https://projecteuler.net/problem=19 + +export const countSundays = (startYear = 1901, endYear = 2000) => { + let n = 0 + let dayOfWeek = 2 + const months = [31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + + for (let year = startYear; year <= endYear; year++) { + months[1] = 28 + ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) + + for (const month of months) { + dayOfWeek += month % 7 + if (dayOfWeek % 7 === 0) { + n++ + } + } + } + return n +} diff --git a/Project-Euler/test/Problem004.test.js b/Project-Euler/test/Problem004.test.js new file mode 100644 index 0000000000..cd6a55ac98 --- /dev/null +++ b/Project-Euler/test/Problem004.test.js @@ -0,0 +1,11 @@ +import { largestPalindromic } from '../Problem004.js' + +describe('Largest Palindromic Number', () => { + test('if digit is 2', () => { + expect(largestPalindromic(2)).toBe(9009) + }) + // Project Euler Condition Check + test('if digit is 3', () => { + expect(largestPalindromic(3)).toBe(906609) + }) +}) diff --git a/Project-Euler/test/Problem019.test.js b/Project-Euler/test/Problem019.test.js new file mode 100644 index 0000000000..6e1a050e53 --- /dev/null +++ b/Project-Euler/test/Problem019.test.js @@ -0,0 +1,11 @@ +import { countSundays } from '../Problem019.js' + +describe('Count Sundays', () => { + test('when start year is 1801 and end year is 1900', () => { + expect(countSundays(1801, 1900)).toBe(172) + }) + // Project Euler Condition Check + test('when start year is 1901 and end year is 2000', () => { + expect(countSundays()).toBe(171) + }) +}) diff --git a/Recursive/test/BinaryEquivalent.test.js b/Recursive/test/BinaryEquivalent.test.js index b79a455eed..ddabb7d477 100644 --- a/Recursive/test/BinaryEquivalent.test.js +++ b/Recursive/test/BinaryEquivalent.test.js @@ -1,29 +1,29 @@ -import { binaryEquivalent } from "../BinaryEquivalent"; +import { binaryEquivalent } from '../BinaryEquivalent' const tests = [ - { - test: 2, - expectedValue: "10" - }, - { - test: 0, - expectedValue: "0" - }, - { - test: 543, - expectedValue: "1000011111" - }, - { - test: 4697621023, - expectedValue: "100011000000000000000001000011111" - } + { + test: 2, + expectedValue: '10' + }, + { + test: 0, + expectedValue: '0' + }, + { + test: 543, + expectedValue: '1000011111' + }, + { + test: 4697621023, + expectedValue: '100011000000000000000001000011111' + } ] -describe("Binary Equivalent", () => { - test.each(tests)( - "of $test should be $expectedValue", - ({test, expectedValue}) => { - expect(binaryEquivalent(test)).toBe(expectedValue); - } - ) +describe('Binary Equivalent', () => { + test.each(tests)( + 'of $test should be $expectedValue', + ({ test, expectedValue }) => { + expect(binaryEquivalent(test)).toBe(expectedValue) + } + ) }) diff --git a/Search/InterpolationSearch.js b/Search/InterpolationSearch.js index e6deae496f..93f3b78b0e 100644 --- a/Search/InterpolationSearch.js +++ b/Search/InterpolationSearch.js @@ -36,4 +36,4 @@ export function interpolationSearch(arr, key) { } return -1 -} \ No newline at end of file +}