Files
hello-algo/ja/codes/go/chapter_computational_complexity/iteration.go
T
Yudong Jin 7257641e0e Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese

* Fit Japanese homepage labels and localize heading permalinks

* Point Japanese contribution instructions to the Japanese preview

* Replace locale conditionals with multilingual UI tables
2026-10-30 02:12:26 +08:00

60 lines
1.1 KiB
Go

// File: iteration.go
// Created Time: 2023-08-28
// Author: Reanon ([email protected])
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
}