2
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?

More than 5 years have passed since last update.

フィボナッチ数列を表示するシェルプログラム

Posted at

はじめに

Bashプログラミングの小ネタとして作成しました。
第5弾です。

フィボナッチ数列を表示

フィボナッチ数列とは何か、詳しくはこちらをご確認ください。

簡単にいうと、2つ前の項と1つ前の項を足し合わせていくことでできる数列です。

ソース

# !/bin/bash

m=0
n=1

echo -n "please input number => "
read num

if (( $num <= 1 || $num >= 2))
then
  echo $m
  echo $n
fi
(( num +=2 ))

for i in $(seq 1 $num)
do
  (( o = $m + $n ))
  echo $o
  m=$n
  n=$o
done

もっときれいに書けそうですね…

実行結果

$ ./fibonacci.sh
please input number => 8
0
1
1
2
3
5
8
13
21
34
55
89

おわりに

このプログラムを作成することで

  • readコマンド
  • 条件式
  • 条件演算子
  • 変数
  • Bashのお作法

が理解できました。

2
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
2
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?