Created
May 14, 2021 12:52
-
-
Save humamfauzi/d67cd30c71d3c4e679fab55b9fb03c9b to your computer and use it in GitHub Desktop.
Staircase Problem with Arbitrary Steps
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" | |
) | |
var count int64 | |
func Fibonacci(number int64) int64 { | |
count++ | |
if number == int64(0) { | |
return int64(1) | |
} else if number < int64(0) { | |
return int64(0) | |
} | |
subtract := Fibonacci(number-1) + Fibonacci(number-3) + Fibonacci(number-2) | |
return int64(subtract) | |
} | |
var countExtra int64 | |
func FibonacciExtra(number int64, steps []int64) int64 { | |
countExtra++ | |
if number == int64(0) { | |
return int64(1) | |
} else if number < int64(0) { | |
return int64(0) | |
} | |
var total int64 | |
for _, num := range steps { | |
total += FibonacciExtra(number-num, steps) | |
} | |
return int64(total) | |
} | |
var countArr int64 | |
func FibonacciArray(number int64) int64 { | |
var current int | |
before_3 := 0 | |
before_1 := 1 | |
before_2 := 1 | |
buffer := 0 | |
for i := 1; i < int(number); i++ { | |
countArr++ | |
current = before_1 + before_2 + before_3 | |
buffer = before_1 | |
before_1 = current | |
before_2 = buffer | |
} | |
return int64(current) | |
} | |
var countArrExtra int64 | |
func FibonacciArrayExtra(number int64, steps []int64) int64 { | |
var beforeArray []int64 | |
beforeArray = make([]int64, steps[len(steps)-1]) | |
beforeArray[0] = 1 | |
beforeArray[1] = 1 | |
for i := 1; i < int(number); i++ { | |
var current int64 | |
for _, v := range steps { | |
countArrExtra++ | |
current += beforeArray[v-1] | |
} | |
beforeArray = append([]int64{current}, beforeArray[:len(beforeArray)-1]...) | |
} | |
return int64(beforeArray[0]) | |
} | |
func main() { | |
fmt.Println("Hello, playground") | |
asdf := Fibonacci(int64(5)) | |
fmt.Println(asdf, count) | |
asdff := FibonacciExtra(int64(14), []int64{1, 3}) | |
fmt.Println("===>", asdff, countExtra) | |
pp := FibonacciArray(int64(5)) | |
fmt.Println(pp, countArr) | |
ppo := FibonacciArrayExtra(int64(14), []int64{1, 3}) | |
fmt.Println("====>", ppo, countArrExtra) | |
asds := []int64{1, 2, 3, 4} | |
fmt.Println(asds[len(asds)-1]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment