pragma solidity ^0.4.24; // import "./SafeMath.sol"; contract GCDSample { using SafeMath for uint; /** * @dev greatest common divisor. Using Euclidean Algorithm. */ function gcd(uint a, uint b) public pure returns(uint) { (uint min, uint max) = sort(a, b); if(min == 0) { return max; } uint mod = max % min; while(mod > 0) { (min, max) = sort(mod, min); mod = max % min; } return min; } /** * @dev least common multiple */ function lcm(uint a, uint b) public pure returns(uint) { uint _gcd = gcd(a, b); if(_gcd == 1) { return a*b; } return a.div(_gcd).mul(b.div(_gcd)).mul( _gcd); } function sort(uint a, uint b) public pure returns(uint, uint) { return a < b ? (a, b) : (b, a); } } /** * @title SafeMath * @dev Math operations with safety checks that throw on error */ library SafeMath { /** * @dev Multiplies two numbers, throws on overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256 c) { // Gas optimization: this is cheaper than asserting 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522 if (a == 0) { return 0; } c = a * b; require(c / a == b, "mul is overflow!"); return c; } /** * @dev Integer division of two numbers, truncating the quotient. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { // assert(b > 0); // Solidity automatically throws when dividing by 0 // uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return a / b; } /** * @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend). */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { require(b <= a, "subtractor is smaller than subtractee"); return a - b; } /** * @dev Adds two numbers, throws on overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256 c) { c = a + b; require(c >= a, "added is overflow."); return c; } }
0.4.24