LoginSignup
2
2

More than 1 year has passed since last update.

バイナリ・カウント問題 53バイト

Last updated at Posted at 2015-08-26

オリジナルは既に見れないのですが、PDFが公開されていたのでやってみます。
みんなのコード:http://togetter.com/li/823763

53バイト。

<?for(;$i++<4;$l+=substr_count(decbin($i),1))?><?=$l;

引数版60バイト。

<?for(;$i++<$argv[1];$l+=substr_count(decbin($i),1))?><?=$l;

バイナリカウント(10^3) = 4938
バイナリカウント(10^10) = 164293127179

長いよsubstr_count。

単純数え上げなので、10^3は一瞬で求まりますが、10^10は手元のマシンで1時間かかりました。
まともな時間で求めるなら、もっと効率のいいアルゴリズムを実装する必要があるでしょう。

この問題はこれ以上縮めようがない気がするんだがどうだろうか。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2