- ログイン画面にパスワードを入力する
- トークンデバイスに表示されるワンタイムパスワードを入力する
- カード情報を入力後、クレジットカード会社のページにリダイレクトするので、パスワードを入力する
- 登録している携帯電話のSMSに送付されるワンタイムパスワードを入力する
- パスワードを入力する
- スマホにインストールしている Google アプリに「あなたは今サインインしようとしていますか?」のようなポップアップが出るので、「はい」を選択する
def seriesSum(n):
ret = 0
for x in range(1, n+1):
ret += x
return ret
def seriesSum(n):
ret = 0
for x in range(1, n+1):
ret += x*x
return ret
def genSeriesSum(_f):
func = _f
def seriesSum(n):
ret = 0
for x in range(1, n+1):
ret += func(x)
return ret
return seriesSum
では、説明してみます。
def square(x):
return x*x
squareSum = genSeriesSum(square)
print squareSum(10)
どうでしょうか?
def genDeriverable(_f, _dx):
f = _f
dx = _dx
def deriverable(x):
return (f(x + dx) - f(x)) / dx
return deriverable
In traditional recursion, the typical model is that you perform your recursive calls first, and then you take the return value of the recursive call and calculate the result. In this manner, you don't get the result of your calculation until you have returned from every recursive call.
In tail recursion, you perform your calculations first, and then you execute the recursive call, passing the results of your current step to the next recursive step. This results in the last statement being in the form of "(return (recursive-function params))" (I think that's the syntax for Lisp). Basically, the return value of any given recursive step is the same as the return value of the next recursive call.
The consequence of this is that once you are ready to perform your next recursive step, you don't need the current stack frame any more. This allows for some optimization. In fact, with an appropriately written compiler, you should never have a stack overflow snicker with a tail recursive call. Simply reuse the current stack frame for the next recursive step. I'm pretty sure Lisp does this.
ざっくり日本語で言うと、
上のコードは階乗を求めるソースです。普通は、上のように書くでしょう。。
int factorial(int n) {
if (!n)
return 1;
return n * factorial(n-1);
}
かるく衝撃を覚えるほどのソースコードです。returnするときには、現在の関数のスタックは必要じゃなくなってますね。。ちょっと動的計画を彷彿とさせるような感じですが。。
int factorial2(int n, int acc=1) {
if (!n)
return acc;
return factorial2(n-1, n*acc);
}
引用サイト: http://stackoverflow.com/questions/33923/what-is-tail-recursion
#define EPS 1e-7
int main() {
// case 1
double x = 1e16;
printf("%lf\n", x + 1);
// case 2
double y1 = 123456123456.1234588623046875;
double y2= 123456123456.1234741210937500;
printf("%d\n", ABS(y1 - y2) < EPS);
printf("%d\n", ABS(y1 - y2) < EPS * y1);
// case 3
double z1 = 1e-1072;
double z2 = -1e-1072;
printf("%d\n", ABS(z1 - z2) < EPS * z1);
return 0;
}
void getBin(int n, char bin[]) {
REP (i, 16)
bin[15 - i] = (n >> i & 1) + '0';
bin[16] = '\0';
}
int main() {
int n, bit;
char calc, bin[16 + 1];
cout << "Input an integer." << endl;
cin >> n;
getBin(n, bin);
printf("%s\n", bin);
// Input one of the manipulations below:
// q exit
// u bit change bit-th bit to '1'
// d bit change bit-th bit to '0'
// x bit reverse bit-th bit
// c reverse all bits
// where q, u, d, x and c are characters themselves, bit is an integer and 0-based.
while (cin >> calc) {
switch (calc) {
case 'q':
cout << "Bye!" << endl;
return 0;
case 'u':
cin >> bit;
n |= 1 << bit;
break;
case 'd':
cin >> bit;
n &= ~(1 << bit);
break;
case 'x':
cin >> bit;
n ^= 1 << bit;
break;
case 'c':
n = ~n;
break;
default:
cerr << "Syntax Error." << endl;
cerr << "Try again." << endl;
continue;
}
getBin(n, bin);
printf("%s\n", bin);
}
return 0;
}