I'd say it's an oddity because it overides opperator+() at all. For large object it would makes sence to return a reference, instead of copying the object, unless the object is changed when operator+ is called.Originally Posted by Mario F.
I'd say it's an oddity because it overides opperator+() at all. For large object it would makes sence to return a reference, instead of copying the object, unless the object is changed when operator+ is called.Originally Posted by Mario F.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Okey, my turn now:
And my other still not solved problem in a reduced form:Code:y > x ? x >= 0 && y <= 1 : x <= 1 && y >= 0
Code:const double h = 0.001; std::complex<double> z(1.0, 0.0), f(1.0, h); int i = 0; do {z *= f; i++;} while (imag(z) > 0); printf("%1.9lf\n", i*h - imag(z)/real(z));
Last edited by TriKri; 11-07-2006 at 03:26 PM.
I answered two of these so Here's mine.
Write this function without recusion. (Thereby making apparent what the function does.)
Code:char whatDoIDo(char (*func)(char, char), char a, const char * xs) { return *xs ? whatDoIDo(func, func(a, *xs) , xs+1) : a; }
Last edited by King Mir; 11-08-2006 at 09:11 AM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Bingo.Originally Posted by TriKri
Result is true if x and y are in [0,1], false otherwise; an obfuscated way of writing this?Originally Posted by TriKri
Code:(x == 0 || x == 1) && (y == 0 || y == 1)An approximation of pi?Originally Posted by TriKri
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
The below two programs are based on a common theme.
Code:unsigned i, j; for(i = 64; i; --i) { for(j = 0; j < 64; ++j) putchar("-#"[!(i - 1 & j)]); putchar('\n'); }
This one is completely incomprehensible. The program is summarized in a one-word C-style comment which makes the last line exactly 80 characters long ("10-letters" is a placeholder); you can try guessing it instead
It makes several nonportable assumptions: unsigned default char or two's complement, ASCII, and general reliability of memory alloc. It is, however, written in a small number of simple, easy-to-understand operations, thus increasing its accessibility to beginning programmers.Code:int main(){char*c=calloc(102,1);++c;*c+=8;while(*c){--*c;--c;*c+=4;c+=2;*c+=4;++ c;++*c;c-=2;}c+=2;*c+=2;++c;*c+=2;c-=4;while(*c){--*c;while(*c){--*c;++c;++*c;-- c;}++c;while(*c)--*c,++c,putchar(*c),c-=2,++*c,++c;c+=3;while(*c){--*c;while(*c) --*c,++c,++*c,--c;++*c;++c;while(*c){--c;++*c;++c;*c+=7;while(*c)--*c,++c,*c+=6, --c;++c;--*c;putchar(*c);--*c;while(*c)--*c,--c,*c+=2,++c;--c;putchar(*c);while( *c){--*c;}}*c+=2;--c;while(*c)--*c,++c,--*c,--c;*c+=2;++c;while(*c){--c;--*c;++c ;*c+=7;while(*c)--*c,++c,*c+=4,--c;++c;putchar(*c);putchar(*c);while(*c)--*c;--c ;}c+=2;}++*c;c-=3;while(*c){--*c;while(*c){--*c;++c;++*c;--c;}++*c;++c;while(*c) {--*c;--c;++*c;c+=3;--*c;while(*c)--*c,++c,++*c,--c;*c+=2;++c;while(*c)--*c,--c, --*c,++c;c-=3;}c-=4;}c+=2;putchar(*c);c-=3;}getchar();return 0;}/* 10-letters */
Last edited by jafet; 11-08-2006 at 04:31 AM.
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
12655 decimal digits approx. == 42039 bits. CHAR_BIT converts number of bytes (provided by sizeof) to number of bits, but this could as easily be replaced by 8 on most conventional systems. The last part gets the sign; it doubles the original value if the variable is unsigned, meaning it can store twice a larger range. Salem forgot a multiplication operator after 42039. Thus this finds the number of decimal digits attainable in some integral variable type, assuming internal binary representation.Code:( sizeof(itype) * (CHAR_BIT * 12655ul) / 42039 ((itype) -1 < 0) + 1 )
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
> Salem forgot a multiplication operator after 42039
Who? Me?
Quines are pretty mind-bending things
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
> Result is true if x and y are in [0,1], false otherwise; an obfuscated way of writing this?
That's right. No, not really an obfuscated way, just a way I first thought may be faster, but it wasn't...
>No, that wouldn't work, my x and y are floats!Code:(x == 0 || x == 1) && (y == 0 || y == 1)
> An approximation of pi?
Yes! An approximation of pi done by simulating a point moving in the complex plane.
Last edited by TriKri; 11-08-2006 at 04:21 PM.
I don't think many biginning programmers would get these.Originally Posted by jafet
I compiled both on my machine. Very cool. The 10 letter word is Sierpinski. But I still don't get how it's done.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Very hard to debugOriginally Posted by jafet
Edit: Your first program was very cool, though, I think I'll have to figure out how it works...
Last edited by TriKri; 11-08-2006 at 04:03 PM.
The latter looks like compiled-to-C version of a brain........ program.
A lot more compact. I hope I didn't make any mistakes.Code:>++++++++[-<++++>>++++>+<<]>>++>++<<<<[-[->+<]>[->.<<+>>>]>[-[->+<]+> [<+>+++++++[->++++++<]>-.-[-<++>]<.[-]]++<[->-<]++>[<->+++++++[->++++<]>.. [-]<]>>]+<<<[-[->+<]+>[-<+>>>-[->+<]++>[-<-+]<<<]<<<<]>>.<<<]
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Omg!
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
Dear lord! *passes out*
Ok, a new one in C++:
Maybe not in the most compact form... (^_^);;Code:#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ofstream fout("x.cpp"); if (!fout) { cout << "Couldn't create \"source.cpp\"" << endl; system("pause"); exit(1); } string s1 = "#include <iostream>\n#include <fstream>\n#include <string>\nusing namespace std;\n\nint main() {\n ofstream fout(\"x.cpp\");\n if (!fout) {\n cout << \"Couldn't create \\\"source.cpp\\\"\" << endl;\n system(\"pause\");\n exit(1);\n }\n string s1 = \"\", s2, s;\n int i;\n for (i = 0; i < s1.size(); i++) {\n switch (s1[i]) {\n case '\\\\':\n s2 += \"\\\\\\\\\";\n break;\n case '\\n':\n s2 += \"\\\\n\";\n break;\n case '\"':\n s2 += \"\\\\\\\"\";\n break;\n default:\n s2 += s1[i];\n }\n }\n s = s1.substr(0, 260) + s2 + s1.substr(260, 999);\n fout << s;\n fout.close();\n return 0;\n}\n", s2, s; int i; for (i = 0; i < s1.size(); i++) { switch (s1[i]) { case '\\': s2 += "\\\\"; break; case '\n': s2 += "\\n"; break; case '"': s2 += "\\\""; break; default: s2 += s1[i]; } } s = s1.substr(0, 260) + s2 + s1.substr(260, 999); fout << s; fout.close(); return 0; }
Last edited by TriKri; 11-08-2006 at 05:33 PM.
Methinks this prints it's own source code, taking into acount // and whatnot. It then writes it out to x.cpp.