Skip to content

feat: Add algorithm and tests for Project Euler Problem 29 (Distinct Powers) #1776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Project-Euler/Problem029.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// https://projecteuler.net/problem=29

/*
How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?
*/

export const distinctPowers = (limit = 100) => {
if (limit < 2) throw new Error('Power out of scope')
// A Set data structure to keep track of distinct powers
let distinctPowerSet = new Set()
for (let a = 2; a <= limit; a++) {
for (let b = 2; b <= limit; b++) {
distinctPowerSet.add(Math.pow(a, b))
}
}
return distinctPowerSet.size
}
17 changes: 17 additions & 0 deletions Project-Euler/test/Problem029.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { distinctPowers } from '../Problem029'

describe('Distinct numbers of a ^ b where a and b in range [2,100]', () => {
it('should throw error when number is less than 2', () => {
expect(() => distinctPowers(0)).toThrowError('Power out of scope')
})
it('should throw error when number is negative', () => {
expect(() => distinctPowers(-3)).toThrowError('Power out of scope')
})
test('if the number is greater than or equal to 2', () => {
expect(distinctPowers(5)).toBe(15)
})
// Project Euler Condition Check
test('if the number is greater than or equal to 2', () => {
expect(distinctPowers(100)).toBe(9183)
})
})

Unchanged files with check annotations Beta

* A Subsequence is sequence obtained by deleting some or no elements without changing the order of elements
* Example: Given a string = "abcd"
* 1. "abc" is a subsequence
* 2. "abd" is a subsequence

Check failure on line 8 in Recursive/SubsequenceRecursive.js

GitHub Actions / Check for spelling errors

abd ==> and, bad
* 3. But "ba" is not a subsequence (because order is changed)
*
* What is lexicographical order?
* In simple terms, lexicographical order is dictionary order.
* Example: Given a string = "abcd"
* 1. "abc" will come before "abcd".
* 2. "abd" will come before "ac".

Check failure on line 15 in Recursive/SubsequenceRecursive.js

GitHub Actions / Check for spelling errors

abd ==> and, bad
*
* References for meaning of subsequence & lexicographical:
* https://en.wikipedia.org/wiki/Subsequence
* like "2A3*3a2", "2A3 3a2", and "2_A3*3#A2"
*
* But the catch is, we have to check only if the alphanumeric characters
* are palindrome i.e remove spaces, symbols, punctuations etc

Check failure on line 14 in String/AlphaNumericPalindrome.js

GitHub Actions / Check for spelling errors

punctuations ==> punctuation, punctuation's
* and the case of the characters doesn't matter
*/
const alphaNumericPalindrome = (str) => {