Rails4 + devise + cancancan + rails_adminが最強過ぎたので自分用にメモ。deviseで認証をして cancancanでロール毎のパーミッションを割り当てて rails_adminをdeviseとcancancanでadminユーザのみに使わせるまた、自分はrvmを使っているのでその辺りの設…
とあるHP*1にて発見した問題。 久しぶりにCで書いたらSEGVで落ちまくり結局1時間弱かかってしまった。。。問題 ゲーム「ぷよぷよ」で、フィールドの状態がテキストで与えられたとき、消える「ぷよ」を消して次のフィールドの状態を出力するプログラムを書け…
テスト用に使ったコード。 REQUEST_TOKEN_URI = 'https://api.twitter.com/oauth/request_token' CALLBACK_URI = 'http://localhost/callback' CONSUMER_KEY = '**********************' CONSUMER_SECRET = '**********************' や場合によってはいろい…
math.hのsqrt(f)を使わずに整数の平方根を求めてみた。(intが4byteであるマシンでのみ動作) sqrtmod0はアルゴリズム上0x40000000以上の値の平方根を計算でようなので、0x40000000以上の値はKaratsuba開平を利用して求めている。↓は乱数で平方根と余りを求め…
検証用に使っていた物です。 len = ARGV[0].to_i B = 10 ** len B2 = B << 1 pi = (len * 8 + 1).step(3, -2).inject(B) {|a, i| (i >> 1) * (a + B2) / i} - B puts "3.#{pi}" # time ruby pi.rb 100 3.14159265358979323846264338327950288419716939937510…
toupperとtolowerって、cが英字であることが担保されていればこれでいい #define TOUPPER(c) ((c) & ~0x20) #define TOLOWER(c) ((c) | 0x20)
ユーザ確認 各ユーザには大体/home下に自分専用のディレクトリがあるので、 ls -l /home で確認できる。 ユーザ追加 useradd ユーザ名 グループ、パスワードも一気に useradd -g グループ名 -p パスワード ユーザ名 ユーザに利用有効期限を付ける useradd -e…
パスワードファイル新規作成 htpasswd -c ファイル名 ユーザ名 New password: パスワード Re-type new password: パスワード ファファイルに追加(パスワード変更) htpasswd ファイル名 ユーザ名 New password: パスワード Re-type new password: パスワー…
ちょっと必要だったので、小数も指定基数でto_sできるようにしてみた。 ただ、指定基数によっては無限小数になる場合があるので、第2引数でto_sの最大長を指定できるようにしてみた。 class Float alias :to_s10 :to_s def to_s(base=10, max=20) raise Arg…
ファイルを空にしたい。 自分だったら一体どんなコマンドを打つだろうか? rm hoge.txt touch hoge.txt なんか、アホらしい。。 パーミッションとか変わってしまうしw echo > hoge.txt ちょっと進歩。 でもなんか改行?1バイト入ってる・・・ : > hoge.txt …
http://rubyist.g.hatena.ne.jp/moira/20090729/1248837435 を見て別のアプローチで再帰使わずにやってみた。 (階乗の計算では再帰使っているけどw かなり速い!! class Numeric def fact return 1 if self.zero? return @cache until @cache.nil? @cache …
PCを別の環境へ接続して作業したりする場合にProxyを切り替える、なんてことはよくある。FireFox3.5にするまではSwitchProxy Tool 1.4.1を使っていたが、 いつまでたっても3.5対応がされない。。 強制インストールするとFireFoxが変な動きするし(汗 が、見…
この記事によれば、iTunes 8.2がUpdateにより適用されるはず。 アップル、iPhone 3.0に対応するiTunes 8.2を公開 - AV Watch だが。。。iTunes 8.2bをインストールしているせいなのか、 Updateの対象にならない・・・なぜですか?まぁとりあえずもう少し待っ…
こんな感じかな? (ワンライナーじゃないwww) URLエンコード コード perl -e '$ARGV[0]=~s/([^\w ])/"%".unpack("H2",$1)/eg;$ARGV[0]=~s/ /\+/g;print"$ARGV[0]\n"' 【エンコードしたい文字列】 テスト > perl -e '$ARGV[0]=~s/([^\w ])/"%".unpack("H…
電卓はこんなのですよね? だから、結構電卓は最終ページに追いやられている場合がほとんどです。 (自分も含めて) 3.0が出たので適当にいろんなことを試していると・・・ 電卓を横向きにすると、なんと高機能な関数電卓になったではありませんか!! コレ…
問題 The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of consecutive primes below one-thou…
問題 The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another. There are n…
問題 The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317. Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000. ソース puts (1..1000).inject(0){|s, i| s + i**i} % (10 ** 10) 解答 9110846700 感想 ここまで来て1行プ…
問題 The first two consecutive numbers to have two distinct prime factors are: 14 = 2 × 7 15 = 3 × 5 The first three consecutive numbers to have three distinct prime factors are: 644 = 2^2 × 7 × 23 645 = 3 × 5 × 43 646 = 2 × 17 × 19. Find …
問題 It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×1^2 15 = 7 + 2×2^2 21 = 3 + 2×3^2 25 = 7 + 2×3^2 27 = 19 + 2×2^2 33 = 31 + 2×1^2 It turns out tha…
問題 Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: Triangle T(n)=n(n+1)/2 1, 3, 6, 10, 15, ... Pentagonal P(n)=n(3n−1)/2 1, 5, 12, 22, 35, ... Hexagonal H(n)=n(2n−1) 1, 6, 15, 28, 45, ... It can be ve…
問題 Pentagonal numbers are generated by the formula, P_(n)=n(3n−1)/2. The first ten pentagonal numbers are: 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... It can be seen that P(4) + P(7) = 22 + 70 = 92 = P(8). However, their difference, 70 −…
問題 The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property. Let d(1) be the 1st digit, d(2) be the 2nd …
問題 The n'th term of the sequence of triangle numbers is given by, t(n) = 1/2 n(n+1); so the first ten triangle numbers are: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... By converting each letter in a word to a number corresponding to its alp…
問題 We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. What is the largest n-digit pandigital prime that exists? ソース cla…
問題 An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If d(n) represents the n'th digit of the fraction…
問題 If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120. {20,48,52}, {24,45,51}, {30,40,50} For which value of p ≤ 1000, is the number of solutions maxim…
問題 Take the number 192 and multiply it by each of 1, 2, and 3: 192 × 1 = 192 192 × 2 = 384 192 × 3 = 576 By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1…
問題 The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 37…
問題 The decimal number, 585 = 1001001001_(2) (binary), is palindromic in both bases. Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2. (Please note that the palindromic number, in either base…