4 ms·
Obvious, but passing arguments to this will cause it to break. One other mod: (j/1000) could be replaced with (j>999) which is still non conditional, but fast
by afhof 16y ago
Obvious, but passing arguments to this will cause it to break.
One other mod: (j/1000) could be replaced with (j>999) which is still non conditional, but faster.
- dchest 16y agoWell, just move the logic into its own function. #include <stdio.h> #include <stdlib.h> void pr(int j) { printf("%d\n", j); (pr + (exit - pr)*(j/1000))(j+1); } int main(int argc, char** argv) { pr(1); return 0; }
- acqq 16y agoI like it this way, it compiles on the compilers where the parent's doesn't (see http://news.ycombinator.com/item?id=2323964 http://news.ycombinator.com/item?id=2323964), I believe it fits to all standards, doesn't depend on any undefined behavior, it doesn't assume any convenient type size, it doesn't call exit, it doesn't crash and also it doesn't use any relational operators. It even uses only two arithmetic operators, and only on integers. #include <stdio.h> #include <stdlib.h> int pk( int i ) { static int (*v[])( int ) = { pk, abs }; printf( "%d\n", i ); return v[ i/1000 ]( i+1 ); } int main( void ) { pk( 1 ); return 0; }
- acqq 16y agoAnd interestingly enough Perl version can be almost the same, only without declarations: use POSIX; sub pk { print "$_[0]\n"; return ( \&pk, \&fabs )[ $_[0] / 1000 ]( $_[0] + 1 ); } pk( 1 );
- onedognight 16y agoFWIW, there's no need to use a library. perl -e '($pk=sub { print "$_[0]\n"; ($pk, sub{}) [$_[0]/1000] ($_[0]+1) }) -> (1)'
- acqq 16y agoThanks. Of course (just for the readers who don't know) even much shorter Perl doesn't have "loops" (in C sense) fits the requirements and works: $,="\n"; print 1..1000 I don't know if it can be made shorter. My previous example was (also for the readers who don't know that) to demonstrate the "C inspired" features in Perl and almost 1-1 mapping to the C solution, minus declarations.