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; } } |