[math-fun] Tiny C Code to generate Sierpinsky
main(c,r){for(r=32;r;)printf(++c>31?c=!r--,"\n":c<r?" ":~c&r?" `":" #");} Cheers! -Robert
After de-obfuscation we see that the one interesting trick is to use the test whether the bitset of bit-and(r,c) is empty for determining the Pascal triangle mod 2: ----------------- // gcc -O2 -W -Wall -std=c99 sierpinsky.c && ./a.out #include <stdio.h> #if 0 main(c,r){for(r=32;r;)printf(++c>31?c=!r--,"\n":c<r?" ":~c&r?" `":" #");} #else int main() { int N = 32; int r = N; do // rows: { --r; for (int c=0; c<=r; ++c) printf(" "); // padding to make triangle for (int c=0; c<N-r; ++c) // columns: { if ( (c & r) != 0 ) printf(" `"); else printf(" #"); } printf("\n"); } while ( r != 0 ); } #endif /* Orig output, (mine has the bottom row added): # # # # ` # # # # # # ` ` ` # # # ` ` # # # ` # ` # ` # # # # # # # # # # ` ` ` ` ` ` ` # # # ` ` ` ` ` ` # # # ` # ` ` ` ` ` # ` # # # # # ` ` ` ` # # # # # ` ` ` # ` ` ` # ` ` ` # # # ` ` # # ` ` # # ` ` # # # ` # ` # ` # ` # ` # ` # ` # # # # # # # # # # # # # # # # # # ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` # # # ` ` ` ` ` ` ` ` ` ` ` ` ` ` # # # ` # ` ` ` ` ` ` ` ` ` ` ` ` ` # ` # # # # # ` ` ` ` ` ` ` ` ` ` ` ` # # # # # ` ` ` # ` ` ` ` ` ` ` ` ` ` ` # ` ` ` # # # ` ` # # ` ` ` ` ` ` ` ` ` ` # # ` ` # # # ` # ` # ` # ` ` ` ` ` ` ` ` ` # ` # ` # ` # # # # # # # # # ` ` ` ` ` ` ` ` # # # # # # # # # ` ` ` ` ` ` ` # ` ` ` ` ` ` ` # ` ` ` ` ` ` ` # # # ` ` ` ` ` ` # # ` ` ` ` ` ` # # ` ` ` ` ` ` # # # ` # ` ` ` ` ` # ` # ` ` ` ` ` # ` # ` ` ` ` ` # ` # # # # # ` ` ` ` # # # # ` ` ` ` # # # # ` ` ` ` # # # # # ` ` ` # ` ` ` # ` ` ` # ` ` ` # ` ` ` # ` ` ` # ` ` ` # # # ` ` # # ` ` # # ` ` # # ` ` # # ` ` # # ` ` # # ` ` # # # ` # ` # ` # ` # ` # ` # ` # ` # ` # ` # ` # ` # ` # ` # ` # */ ----------------- May use this to show my students how hard to read badly documented and formatted code can be. * quad <quadricode@gmail.com> [Jul 18. 2011 18:28]:
main(c,r){for(r=32;r;)printf(++c>31?c=!r--,"\n":c<r?" ":~c&r?" `":" #");}
Cheers!
-Robert
_______________________________________________ math-fun mailing list math-fun@mailman.xmission.com http://mailman.xmission.com/cgi-bin/mailman/listinfo/math-fun
Silly Mr. Arndt, the proper de-obfuscation is clearly this: (defun sierpinski (n) (do ((r n (1- r))) ((minusp r)) (princ (make-string (1+ r) :initial-element #\space)) (dotimes (c (- n r) (terpri)) (format t "~:[ #~; `~]" (zerop (logand c r)))))) (sierpinsky 32) ;-) Sincerely yours, Robert Smith On 7/18/2011 11:00 AM, Joerg Arndt wrote:
After de-obfuscation we see that the one interesting trick is to use the test whether the bitset of bit-and(r,c) is empty for determining the Pascal triangle mod 2:
-----------------
// gcc -O2 -W -Wall -std=c99 sierpinsky.c&& ./a.out
#include<stdio.h>
#if 0 main(c,r){for(r=32;r;)printf(++c>31?c=!r--,"\n":c<r?" ":~c&r?" `":" #");} #else
int main() { int N = 32; int r = N; do // rows: { --r; for (int c=0; c<=r; ++c) printf(" "); // padding to make triangle
for (int c=0; c<N-r; ++c) // columns: { if ( (c& r) != 0 ) printf(" `"); else printf(" #"); } printf("\n"); } while ( r != 0 ); } #endif
/* Orig output, (mine has the bottom row added):
# # # # ` # # # # # # ` ` ` # # # ` ` # # # ` # ` # ` # # # # # # # # # # ` ` ` ` ` ` ` # # # ` ` ` ` ` ` # # # ` # ` ` ` ` ` # ` # # # # # ` ` ` ` # # # # # ` ` ` # ` ` ` # ` ` ` # # # ` ` # # ` ` # # ` ` # # # ` # ` # ` # ` # ` # ` # ` # # # # # # # # # # # # # # # # # # ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` # # # ` ` ` ` ` ` ` ` ` ` ` ` ` ` # # # ` # ` ` ` ` ` ` ` ` ` ` ` ` ` # ` # # # # # ` ` ` ` ` ` ` ` ` ` ` ` # # # # # ` ` ` # ` ` ` ` ` ` ` ` ` ` ` # ` ` ` # # # ` ` # # ` ` ` ` ` ` ` ` ` ` # # ` ` # # # ` # ` # ` # ` ` ` ` ` ` ` ` ` # ` # ` # ` # # # # # # # # # ` ` ` ` ` ` ` ` # # # # # # # # # ` ` ` ` ` ` ` # ` ` ` ` ` ` ` # ` ` ` ` ` ` ` # # # ` ` ` ` ` ` # # ` ` ` ` ` ` # # ` ` ` ` ` ` # # # ` # ` ` ` ` ` # ` # ` ` ` ` ` # ` # ` ` ` ` ` # ` # # # # # ` ` ` ` # # # # ` ` ` ` # # # # ` ` ` ` # # # # # ` ` ` # ` ` ` # ` ` ` # ` ` ` # ` ` ` # ` ` ` # ` ` ` # # # ` ` # # ` ` # # ` ` # # ` ` # # ` ` # # ` ` # # ` ` # # # ` # ` # ` # ` # ` # ` # ` # ` # ` # ` # ` # ` # ` # ` # ` #
*/
-----------------
May use this to show my students how hard to read badly documented and formatted code can be.
* quad<quadricode@gmail.com> [Jul 18. 2011 18:28]:
main(c,r){for(r=32;r;)printf(++c>31?c=!r--,"\n":c<r?" ":~c&r?" `":" #");}
Cheers!
-Robert
_______________________________________________ math-fun mailing list math-fun@mailman.xmission.com http://mailman.xmission.com/cgi-bin/mailman/listinfo/math-fun
_______________________________________________ math-fun mailing list math-fun@mailman.xmission.com http://mailman.xmission.com/cgi-bin/mailman/listinfo/math-fun
* Robert Smith <quadricode@gmail.com> [Jul 19. 2011 08:47]:
Silly Mr. Arndt, the proper de-obfuscation is clearly this:
(defun sierpinski (n) (do ((r n (1- r))) ((minusp r)) (princ (make-string (1+ r) :initial-element #\space)) (dotimes (c (- n r) (terpri)) (format t "~:[ #~; `~]" (zerop (logand c r))))))
(sierpinsky 32)
;-)
(eval (stupidp (me (how-could ((miss that) self))))) 8-)) Seriously, how to I run that code? % clisp lisp-obf.el *** - EVAL: undefined function SIERPINSKY And emacs, upon M-x eval-buffer, says: call-interactively: Invalid read syntax: "#"
Sincerely yours,
Robert Smith
[...]
Oops, I put "sierpinsky", not "sierpinski". So much for spelling. With that typo fixed, clisp should work. On Jul 19, 2011, at 1:53 AM, Joerg Arndt <arndt@jjj.de> wrote:
* Robert Smith <quadricode@gmail.com> [Jul 19. 2011 08:47]:
Silly Mr. Arndt, the proper de-obfuscation is clearly this:
(defun sierpinski (n) (do ((r n (1- r))) ((minusp r)) (princ (make-string (1+ r) :initial-element #\space)) (dotimes (c (- n r) (terpri)) (format t "~:[ #~; `~]" (zerop (logand c r))))))
(sierpinsky 32)
;-)
(eval (stupidp (me (how-could ((miss that) self)))))
8-))
Seriously, how to I run that code? % clisp lisp-obf.el *** - EVAL: undefined function SIERPINSKY
And emacs, upon M-x eval-buffer, says: call-interactively: Invalid read syntax: "#"
Sincerely yours,
Robert Smith
[...]
_______________________________________________ math-fun mailing list math-fun@mailman.xmission.com http://mailman.xmission.com/cgi-bin/mailman/listinfo/math-fun
* quad <quadricode@gmail.com> [Jul 18. 2011 18:28]:
main(c,r){for(r=32;r;)printf(++c>31?c=!r--,"\n":c<r?" ":~c&r?" `":" #");}
Cheers!
-Robert
Here is some non-obfuscated code doing a mildly more nontrivial thing (ulong shall be some unsigned integer type): ------------------------------- bool bit_paper_fold(ulong k) // Return element number k of the paper-folding sequence: // k= 0, 1, 2, 3, 4, 5, ... // seq(k)= 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, ... // Sequence (for k>=1) is entry A014577 of the OEIS. // Also: for k>=1, the dragon curve turns left if seq(k)=1, else right. // Also fixed point of morphism (for k>0, identify + with 1 and - with 0) // L |--> L+R+L-R, R |--> L+R-L-R, + |--> +, - |--> - { ulong h = k & -k; k &= (h<<1); return ( k==0 ); } ------------------------------- Two renderings of the curve on p.89 of the friendly fxtbook. A generalization is: ------------------------------- bool bit_paper_fold_general(ulong k, ulong w) // Return element number k of the general paper-folding sequence: // bit number x of the words w determines whether // a left or right fold is made at the step x. // With w==0 the result is ! bit_paper_fold(k). { ulong h = k & -k; // == lowest_one(k) h <<= 1; ulong t = h & (k^w); return ( t!=0 ); } ------------------------------- Rendering with control word (binary) ....1010101010 gives a triangular shaped variant of the paperfold curve (see p.91). Terdragon curve, just for the kicks (p.93): ------------------------------- bool bit_dragon3_turn(ulong &x) // Increment the radix-3 word x and // return (tr) whether the number of ones in x is decreased. // tr determines whether to turn left or right (by 120 degrees) // with the terdragon fractal. // // Starting with x==0: // x tr // ........ 0 // .......1 0 // ......1. 1 // .....1.. 0 // .....1.1 0 // .....11. 1 // ....1... 1 // ....1..1 0 // ....1.1. 1 // ...1.... 0 // The sequence tr (for x>=1) is entry A080846 in the OEIS, // the fixed point of the morphism 0 |--> 010, 1 |--> 011. // See also A060236 (== 1,2,1,1,2,2,1,2,1,1,2,1,1,2,2,1,2,2,1,2, ...). // Also fixed point of morphism (for k>0, identify + with 0 and - with 1) // F |--> F+F-F, + |--> +, - |--> - { ulong s = 0; while ( (x & 3) == 2 ) { x >>= 2; ++s; } // scan over nines // if ( (x & 3) == 0 ) ==> incremented word will have one more 1 // if ( (x & 3) == 1 ) ==> incremented word will have one less 1 bool tr = ( (x & 3) != 0 ); // incremented word will have one less 1 ++x; // increment next digit x <<= (s<<1); // shift back return tr; } ------------------------------- Jawohl!
participants (3)
-
Joerg Arndt -
quad -
Robert Smith