-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathLucasSeries.cpp
49 lines (40 loc) · 924 Bytes
/
LucasSeries.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Lucas Number is a sequence of numbers similar to the Fibonacci Series and is defined as the sum of previous two digits
starting with "2" and "1".
*/
#include <bits/stdc++.h>
using namespace std;
void lucas(int n) {
int x = 2;
int y = 1;
int z, count;
if (n == 0)
cout << " ";
else if (n == 1)
cout << x;
else {
cout << x << " " << y << " ";
for (count = 2; count < n; count++) {
z = x + y;
x = y;
y = z;
cout << y << " ";
}
}
}
int main() {
int n;
cout << "Enter the number of terms of Lucas Numbers you want to print: ";
cin >> n;
cout << "The Lucas Series of " << n << " terms is: ";
lucas(n);
}
/*
Time Complexity: O(n)
Space Complexity: O(1)
where n is the input number (number of terms)
Sample Input:
Enter the number of terms of Lucas Numbers you want to print: 9
Sample Output:
The Lucas Series of 9 terms is: 2 1 3 4 7 11 18 29 47
*/