Created
November 18, 2022 15:49
-
-
Save marclop/ff769a2a647d58f8c767d54f45c26b3b to your computer and use it in GitHub Desktop.
This file contains 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
package main | |
import ( | |
"fmt" | |
"log" | |
"math/big" | |
"os" | |
"strconv" | |
"go.elastic.co/apm/v2" | |
) | |
func main() { | |
tracer := apm.DefaultTracer() | |
defer tracer.Flush(nil) | |
number := parseInput() | |
log.Printf("Calculant fibonacci del numero %d amb `fib()`", number) | |
tx := tracer.StartTransaction("impl-1", "fibonacci") | |
res1 := fib(number, tx, nil) | |
tx.End() | |
log.Printf("Calculant fibonacci del numero %d amb `fib2()`", number) | |
tx = tracer.StartTransaction("impl-2", "fibonacci") | |
res2 := fib2(number, tx, nil) | |
tx.End() | |
log.Printf("Resultats\nfib():\t%d\nfib2()\t%d", res1, res2) | |
} | |
func fib(number int, tx *apm.Transaction, parent *apm.Span) int { | |
span := tx.StartSpan(fmt.Sprintf("calcul-%d", number), "fib", parent) | |
defer span.End() | |
if number <= 1 { | |
return number | |
} | |
return fib(number-2, tx, span) + fib(number-1, tx, span) | |
} | |
func fib2(n int, tx *apm.Transaction, parent *apm.Span) *big.Int { | |
span := tx.StartSpan(fmt.Sprintf("calcul-%d", n), "fib", parent) | |
defer span.End() | |
fn := make(map[int]*big.Int) | |
for i := 0; i <= n; i++ { | |
var f = big.NewInt(0) | |
if i <= 2 { | |
f.SetUint64(1) | |
} else { | |
f = f.Add(fn[i-1], fn[i-2]) | |
} | |
fn[i] = f | |
} | |
return fn[n] | |
} | |
func parseInput() int { | |
if len(os.Args) <= 1 { | |
return 1 | |
} | |
n, e := strconv.Atoi(os.Args[1]) | |
if e != nil { | |
panic(e) | |
} | |
return n | |
} |
This file contains 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
package main | |
import ( | |
"log" | |
"math/big" | |
"os" | |
"strconv" | |
) | |
func main() { | |
number := parseInput() | |
log.Printf("Calculant fibonacci del numero %d amb `fib()`", number) | |
res1 := fib(number) | |
log.Printf("Calculant fibonacci del numero %d amb `fib2()`", number) | |
res2 := fib2(number) | |
log.Printf("Resultats\nfib():\t%d\nfib2()\t%d", res1, res2) | |
} | |
func fib(number int) int { | |
if number <= 1 { | |
return number | |
} | |
return fib(number-2) + fib(number-1) | |
} | |
func fib2(n int) *big.Int { | |
fn := make(map[int]*big.Int) | |
for i := 0; i <= n; i++ { | |
var f = big.NewInt(0) | |
if i <= 2 { | |
f.SetUint64(1) | |
} else { | |
f = f.Add(fn[i-1], fn[i-2]) | |
} | |
fn[i] = f | |
} | |
return fn[n] | |
} | |
func parseInput() int { | |
if len(os.Args) <= 1 { | |
return 1 | |
} | |
n, e := strconv.Atoi(os.Args[1]) | |
if e != nil { | |
panic(e) | |
} | |
return n | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment