オイラーとかゼータ関数とか
オイラーとかゼータ関数とか
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
const PI = 3.14159265359; function sum_zeta_2(n){ var sum = 0; for(var i=1; i<=n; i++){ sum += 1.0 / (i * i); } return sum; } function main(){ console.log(sum_zeta_2(10000)); console.log(PI * PI / 6.0); } main(); |
1 2 |
1.6448340718480652 1.6449340668484431 |
c言語でクイックソート(久しぶり)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
#include <stdio.h> #include <stdlib.h> #include <time.h> void yuzuinit(){ srand(time(NULL)); } double getRandomDouble(){ return rand() * 1.0 / RAND_MAX; } int getRandomIntMinMax(int min, int max){ if(max < min){ return max; } double randomDouble = getRandomDouble(); double randomDoubleOffset = randomDouble * (max - min + 1); double randomInt = min + randomDoubleOffset; return randomInt; } int getRandomInt(){ return getRandomIntMinMax(0, 9); } void pint(int n){ printf("%d\n", n); } void initArray(int *a, int len){ for(int i=0; i<len; i++){ a[i] = 0; } } void dispArrayList(int *a, int len){ for(int i=0; i<len; i++){ printf("%d => %d\n", i, a[i]); } } void dispArrayLine(int *a, int len){ for(int i=0; i<len; i++){ printf("%d ", a[i]); } printf("\n"); } void dispArrayLineWithSum(int *a, int len){ int sum = 0; for(int i=0; i<len; i++){ printf("%d ", a[i]); sum += a[i]; } printf(": %d\n", sum); } void setRandomForArray(int *a, int len){ for(int i=0; i<len; i++){ a[i] = getRandomInt(); } } int getPivot(int a, int b, int c){ if(a>b){ if(b>c){ return b; }else{ return a>c ? c:a; } }else{ if(a>c){ return a; }else{ return b>c ? c:b; } } } void swap(int *a, int l, int r){ int t = a[l]; a[l] = a[r]; a[r] = t; } void quicksort_core(int *a, int len, int left, int right){ if(right <= left){ return; } int l = left; int r = right; int p = getPivot(a[l], a[l+1], a[r]); while(1){ while(a[l]<p){ l++; } while(p<a[r]){ r--; } if(r<=l){ break; } swap(a, l, r); l++; r--; } quicksort_core(a, len, left, l-1); quicksort_core(a, len, r+1, right); } void quicksort(int *a, int len){ int left = 0; int right = len - 1; quicksort_core(a, len, left, right); } int yuzumain(){ yuzuinit(); int len = 20; int a[len]; setRandomForArray(a, len); dispArrayLineWithSum(a, len); quicksort(a, len); dispArrayLineWithSum(a, len); return 0; } int main(void){ return yuzumain(); } |
1 2 |
9 2 2 9 2 7 2 8 7 7 4 5 4 3 9 1 4 5 4 4 : 98 1 2 2 2 2 3 4 4 4 4 4 5 5 7 7 7 8 9 9 9 : 98 |
requireの第2引数にエラーメッセージを載せる(contract SimpleBank)
SimpleBankのaddress(ropsten)
1 2 3 |
ROPSTEN (Revival) TESTNET Contract 0x1A5a2b35E7AEde8cC54DEFd50F145B06181c10De |
https://ropsten.etherscan.io/address/0x1a5a2b35e7aede8cc54defd50f145b06181c10de
このやり方↓
require(false, “エラーメッセージ”);
がスマートかどうかは自信がない。
ところで、truffle consoleやetherscanではこの”エラーメッセージ”を読む方法がわからない。
remix(browser-solidity)では読める。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
pragma solidity ^0.4.18; contract SimpleBank { /** storages */ // 預金残高 mapping(address => uint) public bank; /** events */ event Deposit( address indexed addr, uint oldBalance, uint value, uint newBalance ); event Withdraw( address indexed addr, uint oldBalance, uint value, uint newBalance ); /** methods */ // 残高確認 function getMyBalance() public view returns(uint) { return bank[msg.sender]; } // 預金 function deposit(uint _value) public { address msgSender = msg.sender; uint oldBalance = bank[msgSender]; uint newBalance = oldBalance + _value; // オーバーフローチェック if( newBalance < oldBalance || newBalance < _value ){ // オーバーフロー発生! string memory errmsg = "error! オーバーフロー! oldBalance:"; errmsg = _strConnect(errmsg, _uint2str(oldBalance)); errmsg = _strConnect(errmsg, ", _value:"); errmsg = _strConnect(errmsg, _uint2str(_value)); errmsg = _strConnect(errmsg, ", newBalance:"); errmsg = _strConnect(errmsg, _uint2str(newBalance)); require(false, errmsg); }else{ // オーバーフローセーフ bank[msgSender] = newBalance; emit Deposit(msgSender, oldBalance, _value, newBalance); } } // 出金 function withdraw(uint _value) public { address msgSender = msg.sender; uint oldBalance = bank[msgSender]; // 残高確認 if(oldBalance < _value){ // 残高不足 string memory errmsg = "error! 残高不足! oldBalance:"; errmsg = _strConnect(errmsg, _uint2str(oldBalance)); errmsg = _strConnect(errmsg, ", _value:"); errmsg = _strConnect(errmsg, _uint2str(_value)); require(false, errmsg); }else{ // 残高は足りている uint newBalance = oldBalance - _value; bank[msgSender] = newBalance; emit Withdraw(msgSender, oldBalance, _value, newBalance); } } function _strConnect(string _str1, string _str2) private pure returns(string) { bytes memory strbyte1 = bytes(_str1); bytes memory strbyte2 = bytes(_str2); bytes memory str = new bytes(strbyte1.length + strbyte2.length); uint8 point = 0; for(uint8 j = 0; j < strbyte1.length;j++){ str[point] = strbyte1[j]; point++; } for(uint8 k = 0; k < strbyte2.length;k++){ str[point] = strbyte2[k]; point++; } return string(str); } function _uint2str(uint i) private pure returns (string){ if (i == 0) return "0"; uint j = i; uint length; while (j != 0){ length++; j /= 10; } bytes memory bstr = new bytes(length); uint k = length - 1; while (i != 0){ bstr[k--] = byte(48 + i % 10); i /= 10; } return string(bstr); } } |
solidityでquicksort(使いやすい)
https://ethfiddle.com/_cKcOFL8HU
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
pragma solidity ^0.4.18; contract SimpleStore { uint[] private a = new uint[](10); function getA() public view returns(uint[]) { return a; } function setLen(uint _len) public { require(_len < 32); a.length = _len; } function _getRandomHash() private view returns (bytes32) { return keccak256(now); } function _bytes32ToUintArray(bytes32 b) private pure returns(uint[32] memory ret){ for(uint i=0; i<32; i++){ ret[i] = uint(b[i]); } } function init() public { uint[32] memory b = _bytes32ToUintArray(_getRandomHash()); for(uint i=0; i<a.length; i++){ a[i] = b[i]; } } function _calcCloneArray(uint[] _a) private pure returns(uint[]) { uint aLen = _a.length; uint[] memory b = new uint[](aLen); for(uint i=0; i<aLen; i++){ b[i] = _a[i]; } return b; } function doQuicksort() public { uint[] memory b = _calcCloneArray(a); _quicksort(b); a = b; } function _quicksort(uint[] _a) private pure { uint left = 0; uint right = _a.length - 1; _quicksort_core(_a, left, right); } function _quicksort_core(uint[] _a, uint _left, uint _right) private pure { if(_right <= _left){ return; } uint l = _left; uint r = _right; uint p = _getPivot(_a[l], _a[l+1], _a[r]); while(true){ while(_a[l] < p){ l++; } while(p < _a[r]){ r--; } if(r <= l){ break; } _swap(_a, l, r); l++; r--; } _quicksort_core(_a, _left, l - 1); _quicksort_core(_a, r + 1, _right); } function _getPivot(uint _a, uint _b, uint _c) private pure returns(uint) { if(_a < _b){ if(_b < _c){ return _b; }else{ return _a < _c ? _a : _c; } }else{ if(_a < _c){ return _a; }else{ return _b < _c ? _b : _c; } } } function _swap(uint[] _a, uint _l, uint _r) private pure { uint t = _a[_l]; _a[_l] = _a[_r]; _a[_r] = t; } } |