in reply to More simple math.

#!/usr/bin/perl use warnings; use strict; use Test::More; sub f { my $n = shift; $n % 4 + 4 * int((15 - $n) / 4) } my @in = qw( 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15); my @out = qw(12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3); for my $i (0 .. $#in) { is(f($in[$i]), $out[$i], "_$i"); } done_testing(0 + @in);

TDD FTW!

Update: Also notice that f(f($x)) == $x.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: More simple math.
by BrowserUk (Patriarch) on Mar 12, 2015 at 18:44 UTC

    Thank you. That adapts:

    sub f { my( $n, $g, $m ) = @_; $n % $g + $g * int(($m - $n) / $g); };;

    to every situation I can think I might need:

    [0] Perl> print join ' ', map f( $_, 8, 15 ), 0 .. 15;; 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 [0] Perl> print join ' ', map f( $_, 4, 15 ), 0 .. 15;; 12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3 [0] Perl> print join ' ', map f( $_, 2, 15 ), 0 .. 15;; 14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1 [0] Perl> print join ' ', map f( $_, 1, 15 ), 0 .. 15;; 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 [0] Perl> print join ' ', map f( $_, 12, 23 ), 0 .. 23;; 12 13 14 15 16 17 18 19 20 21 22 23 0 1 2 3 4 5 6 7 8 9 10 11 [0] Perl> print join ' ', map f( $_, 8, 23 ), 0 .. 23;; 16 17 18 19 20 21 22 23 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 [0] Perl> print join ' ', map f( $_, 6, 23 ), 0 .. 23;; 18 19 20 21 22 23 12 13 14 15 16 17 6 7 8 9 10 11 0 1 2 3 4 5 [0] Perl> print join ' ', map f( $_, 4, 23 ), 0 .. 23;; 20 21 22 23 16 17 18 19 12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3 [0] Perl> print join ' ', map f( $_, 2, 23 ), 0 .. 23;; 22 23 20 21 18 19 16 17 14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1 [0] Perl> print join ' ', map f( $_, 1, 23 ), 0 .. 23;; 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

    Even the basket cases:

    [0] Perl> print join ' ', map f( $_, 9, 26 ), 0 .. 26;; 18 19 20 21 22 23 24 25 26 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7 8 [0] Perl> print join ' ', map f( $_, 6, 26 ), 0 .. 26;; 24 25 26 21 22 23 18 19 20 15 16 17 12 13 14 9 10 11 6 7 8 3 4 5 0 1 2 [0] Perl> print join ' ', map f( $_, 3, 26 ), 0 .. 26;; 24 25 26 21 22 23 18 19 20 15 16 17 12 13 14 9 10 11 6 7 8 3 4 5 0 1 2

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked