Comment detail

正整数のゲーデル数化? (Nested Flatten)
こんな感じでしょうか。IEnumerator<int> がなんか気持ち悪いですが……
 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
using System;
using System.Collections.Generic;
static class Program {
    static void Main() {
        Console.WriteLine(Goedel(9));   // 512
        Console.WriteLine(Goedel(81));  // 768
        Console.WriteLine(Goedel(230)); // 108
    }
    static double Goedel(int num) {
        double goedel = 1;
        IEnumerator<int> p = Prime();
        foreach(char c in num.ToString()) {
            p.MoveNext();
            goedel *= Math.Pow(p.Current, c - '0');
        }
        return goedel;
    }
    static IEnumerator<int> Prime() {
        yield return 2;
        List<int> primes = new List<int>();
        for(int num = 3; ; num += 2) {
            foreach(int p in primes) {
                if (num % p == 0)
                    goto SKIP;
            }
            primes.Add(num);
            yield return num;
        SKIP:;
        }
    }
}

Index

Feed

Other

Link

Pathtraq

loading...