Blockchain: Integration of maths functionsWith Ethereum we can add maths functions onto the blockchain with a contract. For example [Intro][Hello World]: Here is the initial code for the contract: pragma solidity ^0.4.0; contract mymath { function sqrt(uint x) constant returns (uint y) { uint z = (x + 1) / 2; y = x; while (z < y) { y = z; z = (x / z + z) / 2; } } function sqr(uint a) constant returns (uint) { uint c = a * a; return c; } function mul(uint a, uint b) constant returns (uint) { uint c = a * b; return c; } function sub(uint a, uint b) constant returns (uint) { return a - b; } function add(uint a, uint b) constant returns (uint) { uint c = a + b; return c; } } We then compile this with the Solidity compiler to give: var test_sol_mymathContract = web3.eth.contract([{"constant":true,"inputs":[{"name":"x","type":"uint256"}],"name":"sqrt","outputs":[{"name":"y","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"uint256"}],"name":"add","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"a","type":"uint256"}],"name":"sqr","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"uint256"}],"name":"sub","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"uint256"}],"name":"mul","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"}]); var test_sol_mymath = test_sol_mymathContract.new( { from: web3.eth.accounts[0], data: '0x6060604052341561000c57fe5b5b...d5217a13a07400029', gas: '4700000' }, function (e, contract){ console.log(e, contract); if (typeof contract.address !== 'undefined') { console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } }) We can then add this onto the Blockchain with: > web3.eth.accounts ["0xc7552f45deb093cafb47286a0bc9415845ca3735", "0x0851db3e133a15cd1c32531ffff96b 4526e3cbcd"] > personal.unlockAccount('0xc7552f45deb093cafb47286a0bc9415845ca3735','Qwerty') true > > loadScript('mymath.js') null [object Object] true > web3.eth.defaultAccount = '0xc7552f45deb093cafb47286a0bc9415845ca3735' "0xc7552f45deb093cafb47286a0bc9415845ca3735" > miner.start() null > null [object Object] Contract mined! address: 0xb7d8bcde9849896b9887dc31863c64875945fce5 transactionH ash: 0xd5bd0ffed4b1d8ab199b93815c44ee9bec635c69a7ab8bcd21de21b0e732ed5f > miner.stop() true > test_sol_mymath { abi: [{ constant: true, inputs: [{...}], name: "sqrt", outputs: [{...}], payable: false, type: "function" }, { constant: true, inputs: [{...}, {...}], name: "add", outputs: [{...}], payable: false, type: "function" }, { constant: true, inputs: [{...}], name: "sqr", outputs: [{...}], payable: false, type: "function" }, { constant: true, inputs: [{...}, {...}], name: "sub", outputs: [{...}], payable: false, type: "function" }, { constant: true, inputs: [{...}, {...}], name: "mul", outputs: [{...}], payable: false, type: "function" }], address: "0xb7d8bcde9849896b9887dc31863c64875945fce5", transactionHash: "0xd5bd0ffed4b1d8ab199b93815c44ee9bec635c69a7ab8bcd21de21b0e7 32ed5f", add: function(), allEvents: function(), mul: function(), sqr: function(), sqrt: function(), sub: function() } > test_sol_mymath.sqrt(9) 3 > test_sol_mymath.sqrt(12) 3 > test_sol_mymath.sqrt(16) 4 > test_sol_mymath.sqrt(81) 9 > test_sol_mymath.add(3,4) 7 > test_sol_mymath.add(4,2) 6 > test_sol_mymath.sqr(4) 16 > test_sol_mymath.mul(4,3) 12 > test_sol_mymath { abi: [{ constant: true, inputs: [{...}], name: "sqrt", outputs: [{...}], payable: false, type: "function" }, { constant: true, inputs: [{...}, {...}], name: "add", outputs: [{...}], payable: false, type: "function" }, { constant: true, inputs: [{...}], name: "sqr", outputs: [{...}], payable: false, type: "function" }, { constant: true, inputs: [{...}, {...}], name: "sub", outputs: [{...}], payable: false, type: "function" }, { constant: true, inputs: [{...}, {...}], name: "mul", outputs: [{...}], payable: false, type: "function" }], address: "0xb7d8bcde9849896b9887dc31863c64875945fce5", transactionHash: "0xd5bd0ffed4b1d8ab199b93815c44ee9bec635c69a7ab8bcd21de21b0e7 32ed5f", add: function(), allEvents: function(), mul: function(), sqr: function(), sqrt: function(), sub: function() } > |