challenge 正整数のゲーデル数化?

正の整数 n を引数としてとり, 2^d1 * 3^d2 * 5^d3 ... * pk^dk を返す関数
goedel を定義してください.

ただし,n を10進表現で k 桁の数としたときの各桁の数が数列 [d1,d2,d3,...,dk]
をなすとし,dk が 1 の位,d1 が 10^(k-1) の位です.また,pk は k番目の素数です.

goedel   9  ⇒ 2^9             ⇒  512
goedel  81  ⇒ 2^8 * 3^1       ⇒  768
goedel 230  ⇒ 2^2 * 3^3 * 5^0 ⇒  108

Posted feedbacks - C#

こんな感じでしょうか。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:;
        }
    }
}

で、結局ゲーデル数って何ですか?(w

 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
//http://ja.doukaku.org/100/ 投稿用
using System;
using System.Collections.Generic;
class Program {
  static void Main(string[] args) {
    Console.WriteLine(goedel(9));
    Console.WriteLine(goedel(81));
    Console.WriteLine(goedel(230));
    Console.ReadLine();
  }

  static double goedel(int n) {
    string tmpStr = n.ToString();
    double r = 1;
    List<double> prime = new List<double>(new double[] { 2 });
    for(int i = 3; ; i += 2) {
      bool isPrime = true;
      for(int j = 3; j < i; j++) {
        if(i % j == 0) {
          isPrime = false;
          break;
        }
      }
      if(isPrime) prime.Add(i);
      if(prime.Count >= tmpStr.Length) break;
    }
    for(int i = 0; i < tmpStr.Length; i++) {
      r *= Math.Pow(prime[i], (double.Parse(tmpStr[i].ToString())));
    }
    return r;
  }
}

Index

Feed

Other

Link

Pathtraq

loading...