Skip to content

Instantly share code, notes, and snippets.

@moritz
Created April 28, 2012 20:00
Show Gist options
  • Save moritz/2521684 to your computer and use it in GitHub Desktop.
Save moritz/2521684 to your computer and use it in GitHub Desktop.
Weird fibonacci calculation for masak++
class AN {
# stores a number of the form (a + b * sqrt(5)) / 2**$.c;
has Int $.a;
has Int $.b;
has Int $.c;
method Intize() {
die "a not zero, cannot produce an Int" if $.a;
my $pow = 2 ** $.c;
die "Result is not an int, because b/2**c is not integer" if $.b % $pow;
$.b div $pow;
}
}
multi sub infix:<+>(AN:D $x, AN:D $y) {
my Int $max-denom = $x.c max $y.c;
AN.new(
a => ($x.a * 2**($max-denom - $x.c) + $y.a * 2**($max-denom - $y.c)),
b => ($x.b * 2**($max-denom - $x.c) + $y.b * 2**($max-denom - $y.c)),
c => $max-denom,
)
}
multi sub infix:<->(AN:D $x, AN:D $y) {
my Int $max-denom = $x.c max $y.c;
AN.new(
a => ($x.a * 2**($max-denom - $x.c) - $y.a * 2**($max-denom - $y.c)),
b => ($x.b * 2**($max-denom - $x.c) - $y.b * 2**($max-denom - $y.c)),
c => $max-denom,
)
}
multi sub infix:<*>(AN:D $x, AN:D $y) {
AN.new(
a => ($x.a * $y.a + 5 * $x.b * $y.b),
b => ($x.a * $y.b + $x.b * $y.a),
c => ($x.c + $y.c),
);
}
multi sub infix:<**>(AN:D $x, Int $pow) {
[*] $x xx $pow;
}
my $phi = AN.new(a => 1, b => 1, c => 1);
my $psi = AN.new(a => 1, b => -1, c => 1);
say ($phi ** 8 - $psi ** 8).Intize;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment