mirror of
https://github.com/golang/go
synced 2024-11-19 12:34:47 -07:00
math/big: incorporate feedback by josharian (Example_fibonacci)
Change-Id: I376ff39594b532a5490f13e3985b7a6ff4b6761d Reviewed-on: https://go-review.googlesource.com/11191 Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
This commit is contained in:
parent
cfa3eda587
commit
bbf79575a5
@ -63,15 +63,21 @@ func Example_fibonacci() {
|
||||
|
||||
// loop while fib1 is smaller than 1e100
|
||||
for fib1.Cmp(&limit) < 0 {
|
||||
// Compute the next Fibonacci number:
|
||||
// t1 := fib2
|
||||
// t2 := fib1.Add(fib1, fib2) // Note that Add "assigns" to fib1!
|
||||
// fib1 = t1
|
||||
// fib2 = t2
|
||||
// Using Go's multi-value ("parallel") assignment, we can simply write:
|
||||
fib1, fib2 = fib2, fib1.Add(fib1, fib2)
|
||||
}
|
||||
|
||||
fmt.Println(fib1) // 100-digits fibonacci number
|
||||
fmt.Println(fib1) // 100-digit Fibonacci number
|
||||
|
||||
// Test fib1 for primality. The ProbablyPrimes parameter sets the number
|
||||
// of Miller-Rabin rounds to be performed. 20 is a good value.
|
||||
isPrime := fib1.ProbablyPrime(20)
|
||||
fmt.Println(isPrime) // false
|
||||
fmt.Println(isPrime)
|
||||
|
||||
// Output:
|
||||
// 1344719667586153181419716641724567886890850696275767987106294472017884974410332069524504824747437757
|
||||
|
Loading…
Reference in New Issue
Block a user