Add tags

Add tags to the following comment

f(837799)=524 4468ms

意外と速度出ました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//http://ja.doukaku.org/120/ 投稿用
using System;
class Program {
    static void Main(string[] args) {
        long ticks = DateTime.Now.Ticks;//時間記録
        double z = Math.Pow(2, 20);//2^20

        int step = 0;//最大ステップ数
        double n = 0;//ステップが最大のn

        for(double i = 1; i <= z; i++) {//2^20までループ
            int stepTmp = fStep(i);
            if(step < stepTmp) {//大きければ更新
                step = stepTmp;
                n = i;
            }
        }
        Console.WriteLine("f(" + n + ")=" + step);
        Console.WriteLine((DateTime.Now.Ticks - ticks) / 10000L + "ms");
        Console.ReadLine();
    }

    static int fStep(double z) {
        int step = 0;
        double n = z;
        while(n != 1) {//1になるまで
            if(n % 2 == 0) n /= 2;//偶数だったら
            else n = n * 3 + 1;//奇数だったら
            step++;//ステップを数える
        }
        return step;
    }
}

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...