-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
lucas_number.c
49 lines (42 loc) · 995 Bytes
/
lucas_number.c
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
/*
C program to find the N'th Lucas Number.
Lucas Numbers is a sequence similar to Fibonacci numbers and is defined
as the sum of its two immediately previous terms.
The first and second numbers in Lucas sequence are 2 and 1 respectively.
*/
#include <stdio.h>
int lucas_num(int n)
{
int l1 = 2, l2 = 1;
if (n == 0)
return l1;
for (int i = 2; i <= n; i++)
{
int next = l1 + l2;
l1 = l2;
l2 = next;
}
return l2;
}
int main()
{
int n;
printf("Enter the value of n(where you need the nth lucas number): ");
scanf("%d", &n);
if (n < 0)
{
printf("Invalid Value of n !!!");
return 0;
}
int res = lucas_num(n);
printf("The %d'th Lucas Number is %d.", n, res);
return 0;
}
/*
Time Complexity - O(n), where `n` is the given number.
Space Complexity - O(1)
SAMPLE INPUT AND OUTPUT
SAMPLE I
Enter the value of n(where you need the nth lucas number): 25
The 25'th Lucas Number is 167761.
*/