forked from amiashraf/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLucasSeries.js
More file actions
33 lines (29 loc) · 703 Bytes
/
LucasSeries.js
File metadata and controls
33 lines (29 loc) · 703 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
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
*/
function lucas (index) {
// index can't be negative
if (index < 0) throw new TypeError('Index cannot be Negative')
// index can't be a decimal
if (Math.floor(index) !== index) throw new TypeError('Index cannot be a Decimal')
let a = 2
let b = 1
for (let i = 0; i < index; i++) {
const temp = a + b
a = b
b = temp
}
return a
}
export { lucas }