Files
hello-algo/ja/codes/go/chapter_computational_complexity/iteration.go
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
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
}