Skip to content

Instantly share code, notes, and snippets.

@gyk
Created September 17, 2014 13:52
Show Gist options
  • Save gyk/8a1f302994379ae21472 to your computer and use it in GitHub Desktop.
Save gyk/8a1f302994379ae21472 to your computer and use it in GitHub Desktop.
Oneliner that counts forward and backward
#include <stdio.h>
#include <stdlib.h>
/********
func(1, 1); -> 1
func(1, 3); -> 1 2 3 2 1
func(1, 6); -> 1 2 3 4 5 6 5 4 3 2 1
`func` should be implemented without any loop, if-statement or
conditional operation.
********/
int func(int i, int n)
{
return printf("%d %.0s", (n - abs(i - n)), i + 1 == n * 2 || func(i + 1, n));
}
int main(int argc, char const *argv[])
{
func(1, 1); puts("");
func(1, 3); puts("");
func(1, 6); puts("");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment