Files
hello-algo/ja/codes/go/chapter_computational_complexity/iteration.go
Yudong Jin d7b2277d2b Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4

* Retranslate Japanese code with GPT-5.4
2026-03-30 07:30:15 +08:00

60 lines
1.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// File: iteration.go
// Created Time: 2023-08-28
// Author: Reanon (793584285@qq.com)
package chapter_computational_complexity
import "fmt"
/* for ループ */
func forLoop(n int) int {
res := 0
// 1, 2, ..., n-1, n を順に加算する
for i := 1; i <= n; i++ {
res += i
}
return res
}
/* while ループ */
func whileLoop(n int) int {
res := 0
// 条件変数を初期化する
i := 1
// 1, 2, ..., n-1, n を順に加算する
for i <= n {
res += i
// 条件変数を更新する
i++
}
return res
}
/* while ループ2回更新 */
func whileLoopII(n int) int {
res := 0
// 条件変数を初期化する
i := 1
// 1, 4, 10, ... を順に加算する
for i <= n {
res += i
// 条件変数を更新する
i++
i *= 2
}
return res
}
/* 二重 for ループ */
func nestedForLoop(n int) string {
res := ""
// i = 1, 2, ..., n-1, n とループする
for i := 1; i <= n; i++ {
for j := 1; j <= n; j++ {
// j = 1, 2, ..., n-1, n とループする
res += fmt.Sprintf("(%d, %d), ", i, j)
}
}
return res
}