-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
688680f
commit c39fed0
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.5.0 <0.9.0; | ||
//There is a series, S , where the next term is the sum of pervious three terms. Given the first three terms of the series, a ,b ,c and respectively, you have to output the nth term of the series. | ||
contract Day2 { | ||
//Create a function nthTerm(uint n, uint a, uint b, uint c) where n is the nth term to find and a,b,c are the three terms of the series. | ||
function nthTerm( | ||
uint256 n, | ||
uint256 a, | ||
uint256 b, | ||
uint256 c | ||
) public pure returns (uint256) { | ||
uint256[100] memory arr; | ||
uint256 i; | ||
//S(n) = a for n=1 | ||
//S(n) = b for n=2 | ||
//S(n) = c for n=3 | ||
//S(n) = S(n-1) + S(n-2) + S(n-3) for n>3 | ||
arr[1] = a; | ||
arr[2] = b; | ||
arr[3] = c; | ||
for (i = 4; i <= n; i++) { | ||
arr[i] = arr[i - 1] + arr[i - 2] + arr[i - 3]; | ||
} | ||
return arr[n]; | ||
} | ||
} |