0

C#で数学パズル - 割り切れる4桁の逆転数

More than 1 year has passed since last update.

以下の問題をC#で解いてみたいと思います。

問題

4桁の数値を順序を逆転させた数値(例えば、5432の場合は2345が逆転させた数値)で割ったときに、割り切れる4桁の数を求めよ。 (5432 / 2345 は割り切れないので求める答えではない)
ただし、商が1のものや、割る数が4桁でないものは除外する。

C#のコード

class Program {
    static void Main(string[] args) {
        for (int n = 1000; n < 9999; n++) {
            int r = ReverseNumber(n);
            if (n != r && (n % r == 0) && (r > 1000))
                Console.WriteLine(n);
        }
    }
    private static int ReverseNumber(int n) {
        int ans = 0;    
        while (n > 0) {
            int remainder = n % 10;
            ans = ans * 10 + remainder;
            n /= 10;    
        }    
        return ans;
    }
}

実行結果

8712
9801

へー、NEC PC-9801(1980年代から90年代に一世を風靡したPC)の9801と何か関係があるのかな。それとも偶然?

4桁以外でも求めてみる

この問題をもう少し一般化してみます。

問題

N桁の数値を順序を逆転させた数値(例えば、5432の場合は2345が逆転させた数値)で割ったときに、割り切れるN桁の数を求めよ。
ただし、商が1のものや、割る数がN桁でないものは除外する。
なお、Nは、2 <= N <= 9 の自然数とする。

C#のコード

class Program {
    static void Main(string[] args) {
        for (int n = 2; n < 10; n++) 
            DivisibleReverseNumber(n);
    }

    private static void DivisibleReverseNumber(int digit) {
        var start = (int)Math.Pow(10, digit-1);
        var end = (int)Math.Pow(10, digit);
        for (int n = start; n < end; n++) {
            int r = ReverseNumber(n);
            if (n != r && (n % r == 0) && (r.ToString().Length == digit))
                Console.WriteLine(n);
        }
    }
    private static int ReverseNumber(int n) {
        int ans = 0;
        while (n > 0) {
            int remainder = n % 10;
            ans = ans * 10 + remainder;
            n /= 10;
        }
        return ans;
    }
}

先ほど書いた、Mainメソッドの中身を一般化して、DivisibleReverseNumberメソッドを定義しました。引数は桁数です。

Mainメソッドでは、これを2から9まで回します。

実行結果

8712
9801
87912
98901
879912
989901
8799912
9899901
87128712
87999912
98019801
98999901
871208712
879999912
980109801
989999901

一部を抜き出すと、8712, 87912, 879912, 8799912, 87999912, 879999912 という数があります。9801についても、9801, 98901, 989901... と同じようなパターンがあります。

なかなか興味深い結果がでましたね。不思議です。


この記事は、Gushwell's C# Programming Pageで公開したものをに加筆・修正したものです。

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