Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/01python/08if/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ sidebar_position: 8
---

import ViewSource from "@site/src/components/ViewSource";
import Hint from "@site/src/components/Hint";
import Answer from "@site/src/components/Answer";

# 条件分岐
Expand Down Expand Up @@ -143,6 +144,20 @@ else:

絶対値を求めるプログラムを作ってみましょう。

<Hint>

次の式を思い起こせば、作れそうです。

$$
|x|=
\begin{dcases}
x & \text{if $x\geq 0$,} \\
-x & \text{else.}
\end{dcases}
$$

</Hint>

<Answer>
<ViewSource path="/if/abs.ipynb" />

Expand Down
13 changes: 12 additions & 1 deletion docs/01python/10array/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ sidebar_position: 10
---

import ViewSource from "@site/src/components/ViewSource";
import Hint from "@site/src/components/Hint";
import Answer from "@site/src/components/Answer";

# 配列
Expand Down Expand Up @@ -92,6 +93,16 @@ len(配列名)
生徒の英語の点数が書かれた配列を受け取って、その平均点を返す関数を作ってみましょう。
実際に、点数をそれぞれ 26 点、78 点、83 点、20 点、10 点、11 点、22 点、16 点、41 点、95 点として計算してみましょう。

<Hint>

$n$ 個のデータを $x_1,x_2,\dots,x_n$、平均点を $\bar{x}$ とすると、次のようにして求められるのでした。

$$
\bar{x}=\frac{x_1+x_2+\dots +x_n}{n}=\frac{\sum_{i=1}^n x_i}{n}
$$

</Hint>

<Answer>
<ViewSource path="/array/average4.ipynb" />

Expand All @@ -106,7 +117,7 @@ len(配列名)

今度は、分散を求めるプログラムを書いてみましょう。

$n$ 個の観測データを $x_1,x_2,\cdots,x_n$、平均を $\bar{x}$ とすると、分散 $s^2$ は次のように与えられるとします。
$n$ 個のデータを $x_1,x_2,\dots,x_n$、平均を $\bar{x}$ とすると、分散 $s^2$ は次のように与えられるとします。

$$
s^2=\frac{\sum_{i=1}^n(x_i-\bar{x})^2}{n}
Expand Down
7 changes: 7 additions & 0 deletions docs/01python/11multi-array/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ sidebar_position: 11
---

import ViewSource from "@site/src/components/ViewSource";
import Hint from "@site/src/components/Hint";
import Answer from "@site/src/components/Answer";

# 多次元配列
Expand Down Expand Up @@ -57,6 +58,12 @@ A、B、C の 3 人の生徒がいて、それぞれの国語、数学、英語
| **B** | 73 | 53 | 84 |
| **C** | 63 | 48 | 64 |

<Hint>

まずそれぞれの生徒の得点の合計を求める関数を作ってから、その最高点を求める関数を作ると良いでしょう。

</Hint>

<Answer>
<ViewSource path="/multi-array/total_score_max.ipynb" />
</Answer>
Expand Down
29 changes: 28 additions & 1 deletion docs/01python/12practice/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,38 @@ $n$ 番目まで Fizz Buzz での正しい解を表示するプログラムを

最大公約数を求めるプログラムを作ってみましょう。最大公約数(greatest common divisor)は、GCD とよく略されます。

<Hint>

ユークリッドの互除法を用いれば簡単にできます。

ユークリッドの互除法を用いると、次のように最大公約数が計算できるのでした。
$\mathrm{gcd}(a, b)$ を $a$ と $b$ の最大公約数とします。

例:$30$ と $18$ の最大公約数を求める。

$$
\begin{align*}
\mathrm{gcd}(30, 18) &= \mathrm{gcd}(18, 30 - 18\times 1) \\
&= \mathrm{gcd}(18, 12) \\
&= \mathrm{gcd}(12, 18 - 12\times 1) \\
&= \mathrm{gcd}(12, 6) \\
&= \mathrm{gcd}(6, 12 - 6\times 2) \\
&= \mathrm{gcd}(6, 0) \\
&= 6
\end{align*}
$$

よって、最大公約数は $6$

</Hint>

<Answer>

ユークリッドの互除法を使うと、最大公約数を求めるプログラムは次のようになります。

<ViewSource path="/practice/gcd.ipynb" />

再帰を使うと次のようにもできます。再帰は後の項で説明します
再帰を使うと次のようにもできます。再帰を使ったプログラムは、後の項で紹介します

<ViewSource path="/practice/gcd_recursive.ipynb" />

Expand Down
22 changes: 1 addition & 21 deletions docs/04algorithms/02recursion/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -214,33 +214,13 @@ $$

<Answer>

ユークリッドの互除法を用いれば簡単にできます。$\mathrm{gcd}(a, b)$ を $a$ と $b$ の最大公約数とします。

例:$30$ と $18$ の最大公約数を求める。

$$
\begin{align*}
\mathrm{gcd}(30, 18) &= \mathrm{gcd}(18, 30 - 18\times 1) \\
&= \mathrm{gcd}(18, 12) \\
&= \mathrm{gcd}(12, 18 - 12\times 1) \\
&= \mathrm{gcd}(12, 6) \\
&= \mathrm{gcd}(6, 12 - 6\times 2) \\
&= \mathrm{gcd}(6, 0) \\
&= 6
\end{align*}
$$

よって、最大公約数は $6$

これを使うと、最大公約数を求めるプログラムは次のようになります。

<ViewSource path="/recursion/gcd.ipynb" />

:::note

このプログラムは、はじめの引数が $a < b$ の場合でも動きます。一度目の再帰で、`gcd(b, a % b)` = `gcd(b, a)` が返されるからです。

つまり、$18$ と $30$ の最大公約数を求めるとき、
つまり、$18$ と $30$ の最大公約数を求めるとき、$\mathrm{gcd}(a, b)$ を $a$ と $b$ の最大公約数とすると、

$$
\begin{align*}
Expand Down