僕の備忘録(PC、UN*X、ネットワーク関連が中心)なんです。
自分の書いたところは適当(な時とか)に書き換えますので御了承を。
Schemeにて。
(define myash (lambda (n) (cond ((= 1 n) 1) (else (+ (ash 1 (- n 1) ) (myash (- n 1)))))))
(myash 1) gosh> 1 (myash 2) gosh> 3 (myash 3) gosh> 7 . . . (myash 62) gosh> 4611686018427387903 (myash 63) gosh> 9223372036854775807 (myash 64) gosh> 18446744073709551615
site:kuzuore.com で18446744073709551615を 検索すると、 2006年6月17日とか 2010年4月12日とか 2011年11月29日とか その他2の64乗よりひとつ小さい数ということで 2015年3月19日とか(他は省略)出てきた。
(define powr (lambda (n m) (cond ((zero? m) 1) (else (* n (powr n (- m 1)))))))
(- (powr 2 64) 1) gosh> 18446744073709551615
再帰。単純な順列(0+1+2+ ... +n)だけど。
#include<stdio.h> #include<stdlib.h> int rec(); int rec(int n){ if (n == 0) return 0; else return n + rec(n - 1); } int main(int argc , char **argv) { int arg_num = atoi(argv[1]); printf("%d\n", rec(arg_num)); return 0; }
$ ./rec 10 55 $ ./rec 65535 2147450880 $ ./rec 65536 -2147450880
2^31以内なら、たぶん正確なはず。
ちなみに過大な入力値によっては、なぜか「時々」(毎回でなく)
セグメント違反で落ちた。閾値までは確認できなかったが。
$ for x in `seq 10` > do echo -n $x" ": > ./rec 261900 > done 1 :-63802418 2 :Segmentation fault 3 :-63802418 4 :-63802418 5 :-63802418 6 :-63802418 7 :Segmentation fault 8 :-63802418 9 :Segmentation fault 10 :Segmentation fault
64BitのDebian Stretch。
例によってgccのオプションに-Wall。何も警告は出なかった。
$ gcc --version gcc (Debian 5.3.1-14) 5.3.1 20160409 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Debian jessie (64Bit)でも同様。
$ gcc --version gcc (Debian 4.9.2-10) 4.9.2 Copyright (C) 2014 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
リンクはご自由にどうぞ。でもURLや内容が変った場合はあしからず。