Edelweissの日記 RSSフィード

2011-01-11

[]素数

演算、出力とも1秒くらいでした。

馬鹿正直にエラトステネスのふるいを実行…どう考えても奇数だけ処理した方が早いでしょう。

using System;
using System.IO;

namespace PN1 {
	class Program {
		const int MAX = 10000000;
		static readonly double SQRTMAX = Math.Sqrt(MAX);
		static readonly double TPS = TimeSpan.TicksPerSecond;
		static bool[] p;
		static void Main(string[] args) {
			var t0 = DateTime.Now.Ticks;
			p = new bool[MAX];//初期化
			p[0] = true;//1は素数でない
			for (int i = 1; SQRTMAX > i; i++) {
				if (!p[i - 1]) {//素数ならば
					for (int j = i * 2; MAX > j; j += i) {
						p[j - 1] = true;//倍数は非素数にする
					}
				}
			}
			var t1 = DateTime.Now.Ticks;
			using (var sw = new StreamWriter("PN1.txt")) {
				for (int i = 1; MAX > i; i++) {
					if (!p[i - 1]) {
						sw.WriteLine(i);
					}
				}
			}
			var t2 = DateTime.Now.Ticks;
			Console.WriteLine((t1 - t0) / TPS);
			Console.WriteLine((t2 - t1) / TPS);
		}
	}
}

[]素数その2

奇数のみでやってみると当然半分くらい。

using System;
using System.IO;

namespace PN2 {
	class Program {
		const int MAX = 10000000;
		static readonly double SQRTMAX = Math.Sqrt(MAX);
		static readonly double TPS = TimeSpan.TicksPerSecond;
		static bool[] p;
		static void Main(string[] args) {
			var t0 = DateTime.Now.Ticks;
			p = new bool[MAX / 2];//初期化
			for (var i = 1; SQRTMAX / 2 > i; i++) {
				if (!p[i]) {
					for (var j = i * 3 + 1; MAX / 2 > j; j += i * 2 + 1) {
						p[j] = true;
					}
				}
			}
			var t1 = DateTime.Now.Ticks;
			using (var sw = new StreamWriter("PN2.txt")) {
				sw.WriteLine(2);//2をお忘れなく
				for (var i = 1; MAX / 2 > i; i++) {
					if (!p[i]) {
						sw.WriteLine(i * 2 + 1);
					}
				}
			}
			var t2 = DateTime.Now.Ticks;
			Console.WriteLine((t1 - t0) / TPS);
			Console.WriteLine((t2 - t1) / TPS);
		}
	}
}

2010-03-20

[]

シリアライズ:バージョンによって内容が変わったらどうする?

#define v1
#define v2
#define w
#define r
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace serialize {
  class Program {
    static void Main(string[] args) {
      SortedDictionary<ulong, status> d;
      var f = new BinaryFormatter();
#if (v1 && w)
      d = new SortedDictionary<ulong, status>();
      d.Add(1, new status() { id = 1, text = "1" });
      d.Add(2, new status() { id = 2, text = "2" });
      d.Add(3, new status() { id = 3, text = "3" });
      using (var s = new FileStream("test.dat", FileMode.Create)) {
        f.Serialize(s, d);
      }
#elif (v2 && w)
      d = new SortedDictionary<ulong, status>();
      d.Add(1, new status() { id = 11, text = "aa", created_at = new DateTime(1, 2, 3) });
      d.Add(2, new status() { id = 12, text = "bb", created_at = new DateTime(2010, 1, 1, 12, 34, 56) });
      d.Add(3, new status() { id = 13, text = "cc", created_at = DateTime.Now });
      using (var s = new FileStream("test.dat", FileMode.Create)) {
        f.Serialize(s, d);
      }
#elif r
      using (var s = new FileStream("test.dat", FileMode.Open)) {
        d = (SortedDictionary<ulong, status>)f.Deserialize(s);
      }
      foreach (var v in d) {
        Console.WriteLine(v);
      }
#endif
    }
  }
  [Serializable]
  public class status {
    public ulong id;
    public string text;
#if v1
    public override string ToString() {
      return string.Format("id:{0} text:{1}", id, text);
    }
#elif v2
    [OptionalField(VersionAdded = 2)]
    public DateTime created_at;
    [OnDeserializing] // 初期値を入れる
    internal void OnDeserializingMethod(StreamingContext context) {
      created_at = DateTime.Now;
    }
    [OnDeserialized] // 読み込んだ他のデータを参照して値を入れるならこっち
    internal void OnDeserializedMethod(StreamingContext context) {
      if (id == 1)
        created_at = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));
    }
    public override string ToString() {
      return string.Format("id:{0} text:{1} created_at:{2}", id, text, created_at);
    }
#endif
  }
}

こんな感じ

VersionAddedの値はどこで使うんだろう?

2010-02-18

[]

class Program {
	static void Main(string[] args) {
		var h = new Hoge();
		h.set(2);
		Console.WriteLine(h.piyo);
		var f = new Fuga();
		f.set(2);
		Console.WriteLine(f.piyo);
	}
}
class Hoge {
	public int piyo;
}
struct Fuga {
	public int piyo;
}
static class Extension {
	public static void set(this Hoge h, int i) {
		h.piyo = i;
	}
	public static void set(this Fuga f, int i) {
		f.piyo = i;
	}
}

structは値渡し

すっかり忘れていた…

2009-06-11

[][][]

ターゲットの同盟ID(複数)で総人口の少ないユーザ10人分の村を抽出する

List<x_world> lxw = new List<x_world>(50000);
//map.sql読み込み
List<int> eaid = new List<int>() {[敵aid群]};

var v = (
  from xw in lxw
  where eaid.Contains(xw.aid)
  group xw by xw.uid into grp
  orderby grp.Sum(s => s.population)
  select new { xw = grp }
).Take(10);

LINQって訳判らん…どうにかでっち上げてみた