C vs C++ vs Rust (and Clojure, Python & Go)
source
int calculate (int bottom , int top )
{
if (top > bottom )
{
int sum = 0 ;
for (int number = bottom ; number <= top ; number ++ )
{
if (number % 2 == 0 )
{
sum += number ;
}
}
return sum ;
}
else
{
return 0 ;
}
}
int calculate (int bottom , int top ) {
int sum = 0 ;
if (top > bottom ) {
for (int number = bottom ; number <= top ; number ++ ) {
if (number % 2 == 0 ) {
sum += number ;
}
}
}
return sum ;
}
int calculate (int bottom, int top) {
if (top > bottom) {
auto even = [](auto e) { return e % 2 == 0 ; };
auto xs = std::views::iota (bottom, top + 1 )
| std::views::filter (even);
return std::accumulate (xs.begin (), xs.end (), 0 );
}
return 0 ;
}
fn calculate ( bottom : i32 , top : i32 ) -> i32 {
return ( bottom..=top) . filter ( |n| n % 2 == 0 ) . sum ( )
}
(defn calculate [bottom top]
(->> (range bottom (inc top)) (filter even?) (apply +)))
def calculate (bottom , top ):
return sum (n for n in range (bottom , top + 1 ) if n % 2 == 0 )
func calculate (bottom , top int ) (sum int ) {
if top > bottom {
for number := bottom ; number <= top ; number ++ {
if number % 2 == 0 {
sum += number
}
}
}
return
}