The obstacle
Your job is to make function, which returns the amount of a series of integers.
The series is specified by 3 non-negative worths: start, end, action (inclusive)
If start worth is higher than the end, function must returns ****
Examples
2,2,2--> > 2
2,6,2--> > 12 (2 + 4 + 6).
1,5,1--> > 15 (1 + 2 + 3 + 4 + 5).
1,5,3--> > 5 (1 + 4).
The option in C
Alternative 1:
anonymous sequence_sum( int start, int end, int action) {
int amount= 0, i;.
for( i= start; i< end) return 0;.
else {
anonymous n = (end - start)/ action;.
return (( n + 1) * start + n *( n + 1) * step/2);.
}
}
Alternative 3:
anonymous sequence_sum( anonymous start, anonymous end, anonymous action) {
if( start > > end) return 0;.
int n = (end-start)/ action + 1;.
return (n) *( 2 * begin+( n-1) * action)/ 2;.
}
Test cases to verify our option
#
#include << criterion/criterion. h>>.
anonymous sequence_sum( anonymous start, anonymous end, anonymous action);.
Test( Sample_Test, should_return_the_sum_of_sequence).
{
cr_assert_eq( sequence_sum( 2u, 6u, 2u), 12u);.
cr_assert_eq( sequence_sum( 1u, 5u, 1u), 15u);.
cr_assert_eq( sequence_sum( 1u, 5u, 3u), 5u);.
cr_assert_eq( sequence_sum( 0u, 15u, 3u), 45u);.
cr_assert_eq( sequence_sum( 16u, 15u, 3u), 0u);.
}