Last active
March 16, 2017 14:43
-
-
Save 5pecia1/5c22f845113b319528fb790b822aec10 to your computer and use it in GitHub Desktop.
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
#include <stdio.h> | |
#include <stdlib.h> | |
int sum (int n) { | |
if (n <= 0) | |
return 0; | |
else | |
return n + sum(n - 1); | |
} | |
int sumLoop (int n) { | |
int sum = 0; | |
for (int i = n; i > 0; i--) | |
sum += i; | |
return sum; | |
} | |
void main (int argc, char *argv[]) { | |
int n = atoi(argv[1]); | |
printf("sum(%d) is %d\n", n, sum(n)); | |
printf("sumLoop(%d) is %d\n", n, sumLoop(n)); | |
} |
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
sum' :: Integer -> Integer | |
sum' 0 = 0 | |
sum' x = x + sum' (x - 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment