in reply to Fractal structure: PDL, memory, time
Try this. It's correct at levels 1, 2, & 3, and Ithink it is correct for all levels, but it gets harder to discern.
I found this really tough to get right, whether through recursion or iteration. I'd given up and moved on before the pattern dawned on me. I think a mathematician may be able to reduce this to a formula, maybe involving some kind of polynomial, but it's beyond my abilities.
#! perl -slw use strict; $|++; our $DEPTH ||= 3; sub f{ use integer; my( $x, $y, $depth ) = @_; my $bsize = 4**( $depth-1 ); { if( $x == $y ) { return $depth; } elsif( $x/4 == $y/4 ) { return $depth-1; } elsif( $x/$bsize == $y/$bsize ) { $x %= 4; $y %= 4; --$depth; redo; } else { $x %= $bsize; $y %= $bsize; --$depth; redo; } } } for my $depth ( $DEPTH ) { for my $y ( 0 .. 4**$depth-1 ) { my $c = 0; for my $x ( 0 .. 4 ** $depth -1 ) { printf +( ( ++$c % 4 ) == 0 ? " %2d " : " %2d" ), f( $x, $y, $depth ); } print +( $y % 4 ) == 3 ? "\n" : ''; } print ''; } __END__
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Fractal structure: PDL, memory, time
by FFRANK (Beadle) on Jul 23, 2007 at 01:45 UTC | |
by BrowserUk (Patriarch) on Jul 24, 2007 at 14:57 UTC | |
by FFRANK (Beadle) on Jul 25, 2007 at 17:08 UTC | |
by BrowserUk (Patriarch) on Jul 25, 2007 at 18:47 UTC | |
by FFRANK (Beadle) on Jul 26, 2007 at 02:21 UTC | |
by BrowserUk (Patriarch) on Jul 23, 2007 at 06:26 UTC |