Skip to content

Commit

Permalink
Fibonacci.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Dec 26, 2017
1 parent 8c02270 commit 7512f32
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions expreduce/builtin_numbertheory.go
Expand Up @@ -151,5 +151,6 @@ func GetNumberTheoryDefinitions() (defs []Definition) {
defs = append(defs, Definition{Name: "IntegerPart"})
defs = append(defs, Definition{Name: "PowerMod"})
defs = append(defs, Definition{Name: "EulerPhi"})
defs = append(defs, Definition{Name: "Fibonacci"})
return
}
7 changes: 7 additions & 0 deletions expreduce/evalstate.go
Expand Up @@ -348,6 +348,13 @@ func ruleSpecificity(lhs Ex, rhs Ex, name string) int {
if name == "Rubi`Int" {
return 100
}
// Special case for single integer arguments.
expr, isExpr := lhs.(*Expression).Parts[1].(*Expression)
if isExpr && len(expr.Parts) == 2 {
if _, isInt := expr.Parts[1].(*Integer); isInt {
return 110
}
}
// I define complexity as the length of the Lhs.String()
// because it is simple, and it works for most of the common cases. We wish
// to attempt f[x_Integer] before we attempt f[x_]. If LHSs map to the same
Expand Down
4 changes: 2 additions & 2 deletions expreduce/resources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions expreduce/resources/numbertheory.m
Expand Up @@ -252,3 +252,14 @@
ESameTest[1,EulerPhi[-1]],
]
};

Fibonacci::usage = "`Fibonacci[n]` computes the Fibonacci number for `n`";
Fibonacci[0] = 0; Fibonacci[1] = 1;
(*TODO: implement as RootReduce@(((1 + Sqrt[5])/2)^n - ((1 - Sqrt[5])/2)^n)/Sqrt[5]*)
Fibonacci[n_] := Fibonacci[n] = Fibonacci[n - 1] + Fibonacci[n - 2];
Attributes[Fibonacci] = {Listable, NumericFunction, Protected, ReadProtected};
Tests`Fibonacci = {
ESimpleExamples[
ESameTest[6765, Fibonacci[20]]
]
};

0 comments on commit 7512f32

Please sign in to comment.