Help us understand the problem. What is going on with this article?

等差数列と等比数列とフィボナッチ数列

More than 1 year has passed since last update.

作った経緯

私のTwitter見てくれてる方は知っていると思うんですが、今、恋愛シミュレーションゲームを作っていて、とある計算に等差数列、等比数列、階差数列を導入しようと考えて作りました。ただ、階差数列はまだバグがあるのでまたの機会に。フィボナッチ数列はおまけみたいな感じですね。導入も検討したのですけどやめました。

注意事項

  • paiza.IOでバグ修正はしましたが型の違いによる警告の修正はしてないので適宜やってください。
  • コピーは、等差数列はできますが、等比数列とフィボナッチ数列は値を保存している関係でできません。(コピーしまくるとメモリが:;(∩´﹏`∩);:)

プログラム

まず等差数列からです。

CommonDifferenceSequence.hpp
template<typename T> class CommonDifferenceSequence {
private:
    T FirstTerm, CommonDifference;
public:
    CommonDifferenceSequenceDifference(const T FirstTerm, const T CommonDifference) : FirstTerm(FirstTerm), CommonDifference(CommonDifference) {}
    T GetTerm(const size_t TermNum) { return this->FirstTerm + (TermNum - 1) * CommonDifference; }
};

続いて等比数列です。

CommonRatioSequence.hpp
#include <vector>

template<typename T> class CommonRatioSequence {
private:
    T CommonRatio;
    std::vector<T> arr;
public:
    CommonRatioSequence(const T FirstTerm, const T CommonRatio) : CommonRatio(CommonRatio){ this->arr.emplace_back(FirstTerm); }
    CommonRatioSequence(const CommonRatioSequence&) = delete;
    CommonRatioSequence(CommonRatioSequence&&) = default;
    CommonRatioSequence& operator = (const CommonRatioSequence&) = delete;
    CommonRatioSequence& operator = (CommonRatioSequence&&) = delete;
    T GetTerm(const size_t TermNum) {
        if (TermNum <= this->arr.size()) return this->arr[TermNum - 1];
        // pow関数の戻り値が浮動小数点型だったから律儀に計算させた
        for (size_t i = this->arr.size(); i < TermNum; i++) this->arr.emplace_back(this->arr.back() * this->CommonRatio);
        return this->arr.back();
    }
};

最後にフィボナッチ数列です。
これはこちらのサイトを参考にしました。

FibonacciSequence.hpp
#include <vector>

template<typename T> class Fibonacci {
private: 
    std::vector<T> arr;
public:
    Fibonacci(const T ZeroTerm = 0, const T FirstTerm = 1) {
        this->arr.push_back(ZeroTerm);
        this->arr.push_back(FirstTerm);
    }
    Fibonacci(const Fibonacci&) = delete;
    Fibonacci(Fibonacci&&) = default;
    Fibonacci& operator = (const Fibonacci&) = delete;
    Fibonacci& operator = (Fibonacci&&) = delete;
    T GetTerm(const size_t TermNum) {
        // すでに計算された部分にあればさっさと返す
        if (TermNum <= this->arr.size() - 1) return this->arr[TermNum];
        // ないなら計算する
        for (size_t i = this->arr.size() - 1; i <= TermNum - 1; i++) this->arr.push_back(this->arr[i] + this->arr[i - 1]);
        return this->arr[TermNum];
    }
};

このプログラムも少しずつ改良や新機能追加(といっても何かあるか?)していきます。

更新履歴

2017年01月29日 01:00 フィボナッチ数列のクラスの不具合の修正
2017年01月30日 16:49 フィボナッチ数列のクラスの不具合の修正

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away