Skip to content

Instantly share code, notes, and snippets.

Avatar

Lauri Hartikka lhartikk

  • Helsinki, Finland
View GitHub Profile
View Prime.sol
contract Prime {
function isPrime(uint256 n) public view returns (bool){
if (n < 2) {
return false;
}
if (n == 2) {
return true;
}
return expmod(2, n - 1, n) == 1;
View miller-rabin-tests.js
var Prime = artifacts.require("./Prime.sol");
contract('Prime', function (accounts) {
it('reports correctly primes and composited for miller-rabin test in base2', async function () {
const contract = await Prime.new();
const isMillerRabin2Prime = (numberAsString) => contract.probablyPrime.call(numberAsString, '2')
// test for a simple prime
View miller-rabin.sol
pragma solidity >=0.4.21 <0.6.0;
contract Prime {
address public owner = msg.sender;
constructor() public {
}
View rubik.sol
pragma solidity ^0.4.8;
contract Rubik {
enum Color {Red, Blue, Yellow, Green, White, Orange}
Color[9][6] state;
address public owner = msg.sender;
uint8 constant FRONT = 0;
View fermat.sol
pragma solidity ^0.4.18;
/**
* A contract that pays off, if a user is able to produce a valid solution
* for the Fermat's last theorem
*/
contract Fermat {
/**
View fermat_init.sol
/**
* The release time is set to 8640000 seconds (= 100 days)
* in the future from the timestamp of the contract creation
*/
address public owner = msg.sender;
uint releaseTime = now + 8640000;
View fermat_withdraw.sol
/**
* Allow the owner of the contract to
* withdraw the bounty after the release time has passed
*/
function withdraw() public {
require(msg.sender == owner);
require(now >= releaseTime);
msg.sender.transfer(this.balance);
}
View fermat_claim.sol
/**
* The function that is used to claim the bounty.
* If the caller is able to provide satisfying values for a,b,c and n
* the balance of the contract (the bounty) is transferred to the caller
*/
function claim(int256 a, int256 b, int256 c, int256 n) public {
uint256 value = solve(a, b, c, n);
if (value == 0) {
msg.sender.transfer(this.balance);
}
View fermat_solve.sol
/*
* The "core" logic of the smart contract.
* Calculates the equation with provided values for Fermat's last theorem.
* Returns the value of a^n + b^n - c^n, n > 2
*/
function solve(int256 a, int256 b, int256 c, int256 n) pure public returns (uint256) {
assert(n > 2);
uint256 aExp = power(a, n);
uint256 bExp = power(b, n);
uint256 cExp = power(c, n);
View simple-chess-ai-step-3.js
var minimax = function (depth, game, isMaximisingPlayer) {
if (depth === 0) {
return -evaluateBoard(game.board());
}
var newGameMoves = game.ugly_moves();
if (isMaximisingPlayer) {
var bestMove = -9999;
for (var i = 0; i < newGameMoves.length; i++) {
game.ugly_move(newGameMoves[i]);
bestMove = Math.max(bestMove, minimax(depth - 1, game, !isMaximisingPlayer));