Skip to content

Instantly share code, notes, and snippets.

@5pecia1
Last active March 16, 2017 14:43
Show Gist options
  • Save 5pecia1/5c22f845113b319528fb790b822aec10 to your computer and use it in GitHub Desktop.
Save 5pecia1/5c22f845113b319528fb790b822aec10 to your computer and use it in GitHub Desktop.
#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));
}
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