Accept an arg for the number of fibs to generate.
This commit is contained in:
parent
7092135ad0
commit
7de645a7af
@ -1,6 +1,13 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var usage string = "usage: " + os.Args[0] + " <number of fibs>"
|
||||
|
||||
// fibonacci is a function that returns
|
||||
// a function that returns an int.
|
||||
@ -11,7 +18,7 @@ func fibonacci() func() int {
|
||||
nxt_fib := 0
|
||||
return func() int {
|
||||
if nxt_fib == 0 {
|
||||
nxt_fib = 1
|
||||
nxt_fib = 1
|
||||
return 0
|
||||
}
|
||||
tmp := cur_fib
|
||||
@ -22,8 +29,18 @@ func fibonacci() func() int {
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
if len(flag.Args()) != 1 {
|
||||
fmt.Fprintln(os.Stderr, usage)
|
||||
os.Exit(1)
|
||||
}
|
||||
count, err := strconv.Atoi(flag.Arg(0))
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
f := fibonacci()
|
||||
for i := 0; i < 20; i++ {
|
||||
for i := 0; i < count; i++ {
|
||||
fmt.Println(f())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user