-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solidity_Code_2.sol
31 lines (28 loc) · 972 Bytes
/
Solidity_Code_2.sol
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
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Problem2 {
//Add First n values
uint public sum;
function add(uint n) public returns (uint){
//Requires More Gas
for(uint i=1;i<=n;i++){
sum+=i;
}
return sum;}
function add2(uint n) public returns (uint){
//Requires Less Gas
sum=(sum+n);
return (sum*(n/2));}
function addSeries(uint first_num,uint last_num,uint diff) public returns (uint){
require(diff > 0, "Common difference must be greater than 0");
uint n = ((last_num - first_num) / diff) + 1;
if (n % 2 == 0) {
// If there are an even number of terms, use the standard formula
sum = (n / 2) * (2 * first_num + (n - 1) * diff);
} else {
// If there are an odd number of terms, calculate the middle term and adjust
sum = ((n / 2)+1)*(first_num + (n - 1) * diff);
}
return sum;
}
}