Skip to content

Instantly share code, notes, and snippets.

@Gankra
Last active December 26, 2015 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gankra/7089195 to your computer and use it in GitHub Desktop.
Save Gankra/7089195 to your computer and use it in GitHub Desktop.
How to code like a second year
Pseudocode iterative implementation of the recursive algorithm:
G(n) =
if(n>4)
doPair(n-1, n)
G(n-1)
doPair(n-1, n)
else
doBaseCase()
void function(n){
int cond = n - ((2n - 4) - n) //yes, this evaluates to "4".
int x = 0;
int m = n;
while(n>cond){
if(n==4){
doBaseCase()
}
doPair(n-1, n)
n--
x++
}
if(n==4){
doBaseCase()
}
while(x>0){
doPair(m-x, m-x-1)
x--
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment