0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

コード

フィボナッチ数列を計算する

package main

import (
	"fmt"
)

func fibonacci(n int) int {
	if n <= 1 {
		return n
	}
	return fibonacci(n-1) + fibonacci(n-2)
}

func main() {
	n := 10
	fmt.Printf("fibonacci(%d) = %d\n", n, fibonacci(n))
}

動かしてみる

go run main.go

fibonacci(10) = 55

コード

階乗を計算する

package main

import (
	"fmt"
)

func factorial(n int) int {
    if n <= 1 {
        return 1
    }
    return n * factorial(n-1)
}

func main() {
	n := 10
	fmt.Printf("factorial(%d) = %d\n", n, factorial(n))
}

動かしてみる

go run main.go

factorial(5) = 120
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?