-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathcode_2.cpp
More file actions
42 lines (39 loc) · 841 Bytes
/
code_2.cpp
File metadata and controls
42 lines (39 loc) · 841 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
34
35
36
37
38
39
40
41
42
//
// code_2.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 23/11/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
using namespace std;
int Jacobsthal(int n) {
int dp[3];
dp[0] = 0;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[2] = dp[1] + 2 * dp[0];
dp[0] = dp[1];
dp[1] = dp[2];
}
return dp[1];
}
int Jacobsthal_Lucas(int n) {
int dp[3];
dp[0] = 2;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[2] = dp[1] + 2 * dp[0];
dp[0] = dp[1];
dp[1] = dp[2];
}
return dp[1];
}
int main() {
int n;
cout << "\nEnter n\t:\t";
cin >> n;
cout << "\nJacobsthal number\t:\t" << Jacobsthal(n) << endl;
cout << "\nJacobsthal-Lucas number\t:\t" << Jacobsthal_Lucas(n) << endl;
return 0;
}