Last active
January 3, 2016 21:59
-
-
Save rwst/8525436 to your computer and use it in GitHub Desktop.
The general Binet form of the nth element of a binary recurrence over the integers is, given a(n) = c*a(n-1) + d*a(n-2).
The Pari code is blinding fast, dependent on precision. It can maybe jump in when a floating point result is sufficient. The Sage function gives an expression in square roots.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(Pari/GP) | |
a(c,d,a0,a1,n)=my(r1,r2,s);s=sqrt(c^2+4*d);r1=2*d/(-c+s);r2=2*d/(-c-s);return(((a1-c*a0+a0*r1)*r1^n-(a1-c*a0+a0*r2)*r2^n)/s) | |
(Sage) | |
def a(c,d,a0,a1,n): | |
r1=2*d/(-c+sqrt(c^2+4*d)) | |
r2=2*d/(-c-sqrt(c^2+4*d)) | |
return ((a1-c*a0+a0*r1)*r1^n-(a1-c*a0+a0*r2)*r2^n)/sqrt(c^2+4*d) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Published in Google+:
https://plus.google.com/116161272350788205123/posts/bwFmjjhzRWd