solidityで平方根(整数、小数点以下切り捨て)を取得
https://ethfiddle.com/iklY1QezRl
solidityで平方根(整数、小数点以下切り捨て)を取得
https://ethfiddle.com/iklY1QezRl
1 2 3 4 5 6 7 8 9 10 11 12 |
pragma solidity ^0.4.18; contract SimpleStore { function sqrt(uint x) public pure returns(uint) { uint z = (x + 1 ) / 2; uint y = x; while(z < y){ y = z; z = ( x / z + z ) / 2; } return y; } } |
底の円の半径r(メートル), 円錐の高さh(メートル)の円錐の表面積
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> #include <math.h> #define PI 3.14159265 // 底の円の半径r(メートル), 円錐の高さh(メートル) double getSurfaceAreaOfCone(double r, double h){ double S = PI * r * (r + sqrt(pow(r, 2) + pow(h, 2))); return S; } int main(void){ double r = 3; double h = 4; double S = getSurfaceAreaOfCone(r, h); printf("底の円の半径%.2f[m]、高さ%.2f[m]の円錐の表面積は%.2f[m^2]", r, h, S, S/PI ); } |
1 |
底の円の半径3.00[m]、高さ4.00[m]の円錐の表面積は75.40[m^2] |
成功率5%のことに1回でも成功する確率が80%以上になるのは、何回挑戦した時か
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 |
#include <stdio.h> #include <math.h> /** * 任意の整数を底とする対数を計算する * y = log a x * @param[in] base 底 a * @param[in] antilog 真数 x */ double logn(double base, double antilog) { return log(antilog) / log((double)base); } /** * 成功率5%のことに1回でも成功する確率が80%以上になるのは、何回挑戦した時か */ int main(void){ int p = 5; // 成功率 int q = 80; // 目標成功率 double l = 1.0 - p/100.0; double r = 1.0 - q/100.0; int cnt = 1; double ll = l; while(1){ cnt++; ll *= l; if(ll <= r){ break; } } double n = cnt - 1; double offset = 0.0000001; while(1){ n += offset; if(pow(l, n) < r){ break; } } printf("計算解 : %f 回\n", n); double a = l; double x = r; double ans = logn(a, x); printf("解析解 : %f 回\n", ans); } |
1 2 |
計算解 : 31.377160 回 解析解 : 31.377160 回 |
JavaScriptで素数判定(高速ver)の時間測定
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 |
function isPrime(n){ if(false===Number.isInteger(n)){ return false; } if(n<=1){ return false; } if(2===n){ return true; } if(0===n%2){ return false; } var square_root = Math.floor(Math.sqrt(n)); for(var i=3; i<=square_root; i+=2){ if(0===n%i){ return false; } } return true; } function getPrimeCnt(n){ var cnt = 0; for(var i=0; i<=n; i++){ if(isPrime(i)){ cnt++; } } return cnt; } var n = 1000000; var time_old = new Date().getTime(); var cnt = getPrimeCnt(n); var time_now = new Date().getTime(); var msec = time_now - time_old; console.log(n + "以下の素数の数 : " + cnt + "個数"); console.log("計算時間 : " + msec + "ミリ秒"); |
1 2 |
1000000以下の素数の数 : 78498個数 計算時間 : 224ミリ秒 |
素数判定 高速化
素数判定の高速化に挑戦
1 2 3 4 5 6 7 8 |
〜判定の流れ〜 (nが素数ならtrueを返す) nが1以下ならfalse nが2または3ならtrue nが2の倍数ならfalse nの平方根を整数で取得(5なら2, 10なら3) nがnの平方根以下の、3以上の奇数(3,5,7,9,11,...)で1回でも割れたらfalse 上記すべて回避したらtrue |
高速化できた。現状、ユズノハ史上最速の素数判定。
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 |
#include <stdio.h> #include <math.h> #include <time.h> /* 〜判定の流れ〜 (nが素数ならtrueを返す) nが1以下ならfalse nが2または3ならtrue nが2の倍数ならfalse nの平方根を整数で取得(5なら2, 10なら3) nがnの平方根以下の、3以上の奇数(3,5,7,9,11,...)で1回でも割れたらfalse 上記すべて回避したらtrue */ int isPrime_new(int n){ // nが1以下ならfalse if(n<=1){ return 0; } // nが2または3ならtrue if(2==n || 3==n){ return 1; } // nが2の倍数ならfalse if(0==n%2){ return 0; } // nの平方根を整数で取得(5なら2, 10なら3) int square_root = (int)sqrt(n); // nがnの平方根以下の、3以上の奇数(3,5,7,9,11,...)で1回でも割れたらfalse for(int i=3; i<=square_root; i+=2){ if(0==n%i){ return 0; } } // 上記すべて回避したらtrue return 1; } /* 高速化前の素数判定アルゴリズム */ int isPrime_old(int n){ if(n<=1){ return 0; } if(2==n){ return 1; } int square_root = (int)sqrt(n); for(int i=2; i<=square_root; i++){ if(0==n%i){ return 0; } } return 1; } int getTimeSpanNanoSec(struct timespec tsStart, struct timespec tsEnd){ int nsec = tsEnd.tv_nsec - tsStart.tv_nsec; int sec = tsEnd.tv_sec - tsStart.tv_sec; if(0 < sec){ nsec += sec * 1000000000; } return nsec; } void dispTimeForPrimeSearch_old(int n){ printf("(OLD) "); int cnt = 0; struct timespec tsStart, tsEnd; timespec_get(&tsStart, TIME_UTC); for(int i=-5; i<=n; i++){ if(isPrime_old(i)){ cnt++; } } timespec_get(&tsEnd, TIME_UTC); int nsec = getTimeSpanNanoSec(tsStart, tsEnd); printf("%d以下の素数の数 : %d個\t", n, cnt); printf("所要時間 %f ミリ秒\n", nsec / 1000000.0); } void dispTimeForPrimeSearch_new(int n){ printf("(NEW) "); int cnt = 0; struct timespec tsStart, tsEnd; timespec_get(&tsStart, TIME_UTC); for(int i=-5; i<=n; i++){ if(isPrime_new(i)){ // landmark cnt++; } } timespec_get(&tsEnd, TIME_UTC); int nsec = getTimeSpanNanoSec(tsStart, tsEnd); printf("%d以下の素数の数 : %d個\t", n, cnt); printf("所要時間 %f ミリ秒\n", nsec / 1000000.0); } int main(){ int n = 1000000; // n以下の素数 dispTimeForPrimeSearch_new(n); dispTimeForPrimeSearch_old(n); return 0; } |
1 2 |
(NEW) 1000000以下の素数の数 : 78498個 所要時間 105.270360 ミリ秒 (OLD) 1000000以下の素数の数 : 78498個 所要時間 224.728172 ミリ秒 |
c言語でナノ秒時間測定
1 2 3 4 5 |
int nsec = tsEnd.tv_nsec - tsStart.tv_nsec; int secSpan = tsEnd.tv_sec - tsStart.tv_sec; if(0 < secSpan){ nsec += secSpan * 1000000000; } |
がポイント
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 |
#include <stdio.h> #include <math.h> #include <time.h> // int timespec_get (struct timespec *ts, int base) int isPrime(int n){ if(n<=1){ return 0; } if(2==n || 3==n){ return 1; } int square_root = (int)sqrt(n); for(int i=2; i<=square_root; i++){ if(0==n%i){ return 0; } } return 1; } int dispIsPrime(int n){ printf("%dは", n); if(isPrime(n)){ printf(" ... 素数"); } printf("\n"); } /* 1000000万以下の素数の個数を算出するアルゴリズムの速度を求める */ int main(){ struct timespec tsStart, tsEnd; timespec_get(&tsStart, TIME_UTC); int n = 1000000; int cnt = 0; for(int i=-5; i<=n; i++){ if(isPrime(i)){ cnt++; } } printf("%d以下の素数の数 : %d\n", n, cnt); timespec_get(&tsEnd, TIME_UTC); printf("%ld %09ld\n", tsStart.tv_sec, tsStart.tv_nsec); printf("%ld %09ld\n", tsEnd.tv_sec, tsEnd.tv_nsec); int nsec = tsEnd.tv_nsec - tsStart.tv_nsec; int secSpan = tsEnd.tv_sec - tsStart.tv_sec; if(0 < secSpan){ nsec += secSpan * 1000000000; } printf("所要時間 %d ナノ秒\n", nsec); } |
1 2 3 4 |
1000000以下の素数の数 : 78498 1521134295 797056797 1521134296 022770585 所要時間 225713788 ナノ秒 |
RPGの成長システム
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 |
function p(a){ console.log(a); } function probabilityOf(d){ return Math.random() * 100 < d; } function main(){ var lvUpPercents = { str : 50, int : 50, tec : 50, agl : 50, def : 50, res : 50 } var character = { lv : 0, str : 0, int : 0, tec : 0, agl : 0, def : 0, res : 0 } p(character); for(var i=0; i<100; i++){ character.lv++; character.str += probabilityOf(lvUpPercents.str) ? 1 : 0; character.int += probabilityOf(lvUpPercents.int) ? 1 : 0; character.tec += probabilityOf(lvUpPercents.tec) ? 1 : 0; character.agl += probabilityOf(lvUpPercents.agl) ? 1 : 0; character.def += probabilityOf(lvUpPercents.def) ? 1 : 0; character.res += probabilityOf(lvUpPercents.res) ? 1 : 0; } p(character); } main(); |
1 2 |
{ lv: 0, str: 0, int: 0, tec: 0, agl: 0, def: 0, res: 0 } { lv: 100, str: 51, int: 53, tec: 51, agl: 49, def: 54, res: 48 } |
JavaScriptの標準入力
1 2 3 4 5 6 7 8 9 10 11 12 13 |
process.stdin.resume(); process.stdin.setEncoding('utf8'); process.stdin.on('data', function (chunk) { var lines = chunk.toString().split('\n'); main(lines); }); function main(args){ for(var k in args){ var v = args[k]; console.log(k + " => " + v); } } |
1 2 |
1行目 2行目 |
1 2 |
0 => 1行目 1 => 2行目 |
[DApps]ゆずのは銀行(仮)
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 |
// (Yuzubank.sol) pragma solidity ^0.4.18; contract Yuzubank { mapping(address => uint) yuzubank; event Transfer(address indexed from, address indexed to, uint value); // 振り込み function deposit() public payable { yuzubank[msg.sender] += msg.value; emit Transfer(msg.sender, address(this), msg.value); } // 確認 function check() public view returns(uint) { return yuzubank[msg.sender]; } // 銀行の総額 function checkTotal() public view returns(uint) { return address(this).balance; } // 引き出し function withdraw(uint _value) public { // 足りていること require(_value <= yuzubank[msg.sender]); // 引き落とす yuzubank[msg.sender] -= _value; // 送金 msg.sender.transfer(_value); emit Transfer(address(this), msg.sender, _value); } } |
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 |
// (index.html) <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>solidity2</title> <link rel="stylesheet" type="text/css" href="css.css"> </head> <body> <h2>ユズノハ銀行(仮)</h2> <p id="wallet_data"></p> <a href="#" class="square_btn" onclick="update();return false;">更新</a> <br> <br> value : <input id="value_deposit" type="number" > <a href="#" class="square_btn" onclick="deposit();return false;">預金</a> <br> <br> value : <input id="value_withdraw" type="number" > <a href="#" class="square_btn" onclick="withdraw();return false;">引き出し</a> <br> <script src="./const.js"></script> <script type="text/javascript"> // global start if (typeof web3 !== 'undefined') { window.web3 = new Web3(web3.currentProvider); } else { document.write("Please install metamask"); } // コントラクト var contract = web3.eth.contract(JSON.parse(ABI)).at(CONTRACT_ADDR); //イベント監視 contract.Transfer().watch(function (error, result) { console.log('result : '); console.log(result); // イベントキャッチのタイミングで自動更新 update(); }); // global end new Promise((resolve)=>{ window.web3.eth.getAccounts((err, accounts) => { resolve(accounts); }); }).then((accounts)=>{ update(); }); function update(){ var wallet_data = document.getElementById("wallet_data"); var address = web3.eth.defaultAccount; var s = ""; new Promise((resolve)=>{ web3.eth.getBalance(address, (err, value)=>{ s += "アドレス : " + address + "<br>"; s += "保持金額 : " + value + " wei<br>"; resolve(); }); }).then(()=>{ return new Promise((resolve)=>{ contract.check((err, value)=>{ s += "預金残高 : " + value + " wei<br>"; resolve(); }); }); }).then(()=>{ return new Promise((resolve)=>{ contract.checkTotal((err, value)=>{ s += "預金総額 : " + value + " wei<br>"; resolve(); }); }); }).then(()=>{ wallet_data.innerHTML = s; }); } function deposit(){ var value_deposit = document.getElementById("value_deposit").value; // ガード if(value_deposit<=0){ alert("valueは1以上の整数を入力してください"); return; } // 書き込み var txObj = {value: value_deposit}; contract.deposit(txObj, function(err, value){ console.log("deposit tx : " + value); }); } function withdraw(){ var value = document.getElementById("value_withdraw").value; // ガード if(value<=0){ alert("valueは1以上の整数を入力してください"); return; } // 書き込み contract.withdraw(value, function(err, value){ console.log("withdraw tx : " + value); }); } </script> </body> </html> |
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 |
// (const.js) var CONTRACT_ADDR = "0xc02374f85917191281c308a1c0b6c5f63074434b"; var OWNER_ADDR = "0x5ed767cfebf5953835340e012fee4048661b7d1b"; var ABI = `[ { "constant": true, "inputs": [], "name": "checkTotal", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "check", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "from", "type": "address" }, { "indexed": true, "name": "to", "type": "address" }, { "indexed": false, "name": "value", "type": "uint256" } ], "name": "Transfer", "type": "event" }, { "constant": false, "inputs": [], "name": "deposit", "outputs": [], "payable": true, "stateMutability": "payable", "type": "function" }, { "constant": false, "inputs": [ { "name": "_value", "type": "uint256" } ], "name": "withdraw", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" } ]`; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// (css.css) @charset "UTF-8"; .square_btn{ display: inline-block; padding: 0.5em 1em; text-decoration: none; color: #FFF; background-image: -webkit-linear-gradient(#6795fd 0%, #67ceff 100%); background-image: linear-gradient(#6795fd 0%, #67ceff 100%); transition: .4s; } .square_btn:hover{ background-image: -webkit-linear-gradient(#6795fd 0%, #67ceff 70%); background-image: linear-gradient(#6795fd 0%, #67ceff 70%); } |
素数判定
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 |
function isPrime(n){ if(false === Number.isInteger(n)){ return false; } if(n <= 1){ return false; } if(2 === n || 3 === n){ return true; } var limit = Math.floor(Math.sqrt(n)); for(var i=2; i<=limit; i++){ if(0 === n%i){ return false; } } return true; } for(var i=-2; i<100; i++){ var msg = ""; if(isPrime(i)){ msg = " ... 素数"; } console.log(i + msg); } |
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 |
-2 -1 0 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 |