in reply to getting my neighbours in an N-dimensional space

Just because it fits, doesn't mean it's true:
my $possibilities = 2 ** $N; # $N is number of dimensions
Nope! yes, it's true for $N=1. But look at your example. When $N=2, neighbors = 8. But, your equation says 3 dimensions has 8 neighbors - and Zero dimensions would have 1!
my $possibilities = (3 ** $N) - 1;

Think of the 3D example - 3 x 3 x 3 (- one in the middle)

.02

cLive ;-)

Or I could just be completely wrong coz I'm too tired to read through the other replies :)

Replies are listed 'Best First'.
Re: (cLive ;-) Re: getting my neighbours in an N-dimensional space
by dash2 (Hermit) on Jan 22, 2002 at 20:53 UTC
    Not quite. Remember, I was only looking for neighbours nearer the origin:
    0
    
      NN
      N*
    

    So it is 2, not 3, because you only have (0,-1) not (1,0,-1). That's why the "binary number" hack would work. And the was taken care of by doing for (1 .. not for (0 ...

    dave hj~

      1. 2 neighbors
      2. 8 neighbors
      3. 26 neighbors
      4. 80 neighbors
      That's for all neighbors. As for neighbors near the origin ... that's a completely different set of numbers. And, in 3-D, it's not (2**N - 1). It's some weirder formula.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.