Skip to content
Open
Show file tree
Hide file tree
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
41 changes: 22 additions & 19 deletions Maths/LucasSeries.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
/*
Program to get the Nth Lucas Number
Article on Lucas Number: https://en.wikipedia.org/wiki/Lucas_number
Examples:
> loopLucas(1)
1
> loopLucas(20)
15127
> loopLucas(100)
792070839848372100000
*/

/**
* @param {Number} index The position of the number you want to get from the Lucas Series
* Calculates the Nth Lucas Number.
* The Lucas sequence is defined as L_0 = 2, L_1 = 1, and L_n = L_{n-1} + L_{n-2} for n >= 2.
*
* Preconditions:
* - Line 21: `if (index < 0)` throws TypeError('Index cannot be Negative')
* - Line 24: `if (Math.floor(index) !== index)` throws TypeError('Index cannot be a Decimal')
*
* Refactoring Note:
* Clarified state management in the iterative recurrence calculation by replacing
* ambiguous variables `a`, `b`, and `temp` with semantically descriptive variables
* `current` (representing L_i) and `next` (representing L_{i+1}).
*
* @param {number} index The 0-based position of the number in the Lucas sequence.
* @returns {number} The Nth Lucas Number.
* @throws {TypeError} If index is negative or decimal.
* @see https://en.wikipedia.org/wiki/Lucas_number
*/
function lucas(index) {
// index can't be negative
Expand All @@ -21,14 +24,14 @@ function lucas(index) {
if (Math.floor(index) !== index)
throw new TypeError('Index cannot be a Decimal')

let a = 2
let b = 1
let current = 2
let next = 1
for (let i = 0; i < index; i++) {
const temp = a + b
a = b
b = temp
const nextLucas = current + next
current = next
next = nextLucas
}
return a
return current
}

export { lucas }
71 changes: 65 additions & 6 deletions Maths/test/LucasSeries.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,74 @@
import { lucas } from '../LucasSeries'

describe('Nth Lucas Number', () => {
it('should return the 20th Lucas Number', () => {
expect(lucas(20)).toBe(15127)
describe('Existing tests', () => {
it('should return the 20th Lucas Number', () => {
expect(lucas(20)).toBe(15127)
})

it('should return the 0th Lucas Number', () => {
expect(lucas(0)).toBe(2)
})

it('should return the 100th Lucas Number', () => {
expect(lucas(100)).toBe(792070839848372100000)
})
})

it('should return the 20th Lucas Number', () => {
expect(lucas(0)).toBe(2)
describe('Stated Fixture: Smallest valid input and ordinary input', () => {
it('handles smallest valid index: 0 (Precondition line 21: index >= 0)', () => {
const input = 0
// Independently calculated: L_0 = 2
const expected = 2
const result = lucas(input)

expect(result).toBe(expected)
})

it('handles ordinary input: 5 (Precondition line 21 & 24)', () => {
const input = 5
// Independently calculated: L_0=2, L_1=1, L_2=3, L_3=4, L_4=7, L_5=11
const expected = 11
const result = lucas(input)

expect(result).toBe(expected)
})

it('handles ordinary input: 20', () => {
const input = 20
const expected = 15127
const result = lucas(input)

expect(result).toBe(expected)
})
})

it('should return the 20th Lucas Number', () => {
expect(lucas(100)).toBe(792070839848372100000)
describe('Recurrence Sequence & Invariant Checks', () => {
it('verifies the first 8 Lucas numbers explicitly: [2, 1, 3, 4, 7, 11, 18, 29]', () => {
const expectedSeries = [2, 1, 3, 4, 7, 11, 18, 29]
for (let n = 0; n < expectedSeries.length; n++) {
expect(lucas(n)).toBe(expectedSeries[n])
}
})

it('verifies the recurrence invariant L_n = L_{n-1} + L_{n-2} for n >= 2', () => {
for (let n = 2; n <= 25; n++) {
expect(lucas(n)).toBe(lucas(n - 1) + lucas(n - 2))
}
})
})

describe('Precondition enforcement: Negative and Decimal inputs', () => {
it('throws TypeError("Index cannot be Negative") for negative numbers (Line 21)', () => {
expect(() => lucas(-1)).toThrow(TypeError)
expect(() => lucas(-1)).toThrow('Index cannot be Negative')
expect(() => lucas(-10)).toThrow('Index cannot be Negative')
})

it('throws TypeError("Index cannot be a Decimal") for non-integer numbers (Line 24)', () => {
expect(() => lucas(0.5)).toThrow(TypeError)
expect(() => lucas(0.5)).toThrow('Index cannot be a Decimal')
expect(() => lucas(3.14159)).toThrow('Index cannot be a Decimal')
})
})
})
Loading