Skip to content

Instantly share code, notes, and snippets.

@ga2arch
Created January 14, 2012 14:32
Show Gist options
  • Select an option

  • Save ga2arch/1611663 to your computer and use it in GitHub Desktop.

Select an option

Save ga2arch/1611663 to your computer and use it in GitHub Desktop.
ex-1.11 SICP in C
#include <stdio.h>
/*
f(0) = 0
f(1) = 1
f(2) = 2
f(3) = f(2) + 2f(1) + 3f(0)
\ \ \
f(4) = f(3) + 2f(2) + 3f(1)
\ \ \
f(5) = f(4) + 2f(3) + 3f(2)
*/
int main(int argc, char* argv[])
{
printf("%d\n", func(5));
}
int func(int n)
{
int a = 2;
int b = 1;
int c = 0;
int i = 2;
if (n<3) {
return n;
}
while (i != n) {
int ta = a + 2*b + 3*c;
int tb = a;
int tc = b;
a = ta;
b = tb;
c = tc;
i++;
}
return a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment