r/codegolf • u/Pyroan • Oct 12 '17
Collatz Sequence Calculator in C (134 Bytes) - My first attempt at Golf
https://pastebin.com/kphtK1xB
5
Upvotes
2
u/defenastrator Oct 14 '17
#include<stdio.h>
int main(){for(int t;;){for(scanf("%d",&t);t>1;t=t&1?3*t+1:t/2)printf("%d\n",t);puts("1\n\n");}}Slightly shorter.
3
u/gastropner Oct 22 '17 edited Oct 25 '17
A bit shorter still:
#include<stdio.h> int main(int t){for(scanf("%d",&t);t>1;t=t&1?3*t+1:t/2)printf("%d\n",t);main(puts("1\n"));}EDIT: Could remove one of the \n in puts(), since it adds one anyway.
2
u/[deleted] Feb 27 '18
Protip: although this feature is highly discouraged, the C standard doesn't require you to include the standard headers in order to call standard library functions.
Furthermore, if types aren't provided for function parameter / return types, int is assumed.