here's an obfuscated way to get powers of 3 from multiplying 1 by 2 and powers of two and two arrays (with a little addition in the mix). $a is what power of three to calculate.
$a=24;@a=(1);sub d{push@a,map{2*$_}@a}; d for('1'.."$a");$b=$#a;$_=0; push@b,$_+=$a[$#a-$b--] until($b==-1); print$b[2**$a-1];
a simple note, it'll use 2*(2**$a) array elements, and is probably O(2**n) in memory usage and cpu time, so its probably one of the more ineffecient ways to calculate powers of three in existence

Replies are listed 'Best First'.
Re: Powers of three
by zshzn (Hermit) on Feb 07, 2006 at 02:35 UTC
    Neat!

    I wouldn't interpolate $a in the for loop. Or 1. Makes me kind of wonder what influenced you to do so.

      honestly i don't remember it was written in my statistics class at 9am
      i'm also partially curious as to why the program works, i know what its doing, i worked it out by hand before writing it in perl
        Nice code.

        Logically the state of any point is an XOR of its two parents taking a missing parent to be 0. (and seeding with a 1 at the point)