Content-Length: 2117 | pFad | http://github.com/TheAlgorithms/JavaScript/pull/1776.patch
thub.com
From 47e3ef49376b24fc3d9b1898b27012d8bd99ac33 Mon Sep 17 00:00:00 2001
From: zeal-2004
Date: Sat, 24 May 2025 23:05:26 +0530
Subject: [PATCH] feat: add distinctPowers function with test cases
(Problem029)
---
Project-Euler/Problem029.js | 17 +++++++++++++++++
Project-Euler/test/Problem029.test.js | 17 +++++++++++++++++
2 files changed, 34 insertions(+)
create mode 100644 Project-Euler/Problem029.js
create mode 100644 Project-Euler/test/Problem029.test.js
diff --git a/Project-Euler/Problem029.js b/Project-Euler/Problem029.js
new file mode 100644
index 0000000000..d7180ea434
--- /dev/null
+++ b/Project-Euler/Problem029.js
@@ -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
+}
diff --git a/Project-Euler/test/Problem029.test.js b/Project-Euler/test/Problem029.test.js
new file mode 100644
index 0000000000..df6c826349
--- /dev/null
+++ b/Project-Euler/test/Problem029.test.js
@@ -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)
+ })
+})
--- a PPN by Garber Painting Akron. With Image Size Reduction included!Fetched URL: http://github.com/TheAlgorithms/JavaScript/pull/1776.patch
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy