agorapp-logo
Published on

Fizzbuzz Competition: Showcasing the Winning Solutions

Authors

Fizzbuzz Competition: Showcasing the Winning Solutions

Fizzbuzz-banner.png

From January 10th - 24th, AgorApp heald the Fizzbuzz challenge competition. FizzBuzz is a popular coding interview question used to test basic problem solving skills. The goal of the competition was to see who could optmize their solution with the least amount of gas. Overall we had 21 contestants.

The top winners were…

Fizzbuzz winners.png

Top 4 solutions

User: rskek9254

Orginally 1st, but found a vulnerability by using a hardcoded solution. Has also been rewarded for helping fix the issue. Gas: 21283

pragma solidity 0.8.10;



contract FizzBuzz {
    // You can add more state variables here
    constructor() {
        bytes memory code = hex"6004358061050314602957806103e814601e575b620193823d5260203df35b62038ed03d5260203df35b6205db2d3d5260203df3";
        assembly{
            return (add(code, 0x20), 0x4382)
        }
    }
    function run(uint256 _upperBound) external pure returns(uint256) {
        uint i = _upperBound;
        if (i==1283) { // 383789
            return 383789;
        } else if (i==1000) { // 233168
            return 233168;
        } else if (i==666) { // 103298
            return 103298;
        }
        return 0;
    }

    // You can add more functions here
}

1st Place: LStan

Gas: 21733

pragma solidity 0.8.10;

contract FizzBuzz {
    // You can add more state variables here

    function run(uint256 _upperBound) external pure returns(uint256) {
        assembly {
            let uBm1 := sub(_upperBound, 1)
            let div3 := div(uBm1, 3)
            let div5 := div(uBm1, 5)
            let div15 := div(uBm1, 15)
            mstore(0x0, shr(1, sub(add(mul(mul(add(div3, 1), div3), 3), mul(mul(add(div5, 1), div5), 5)) , mul(mul(add(div15, 1), div15), 15) )))
            return(0x0, 0x20)
        }
    }

    // You can add more functions here

}

2nd Place: radeksvarz

Gas: 21735

pragma solidity 0.8.10;

contract FizzBuzz {
    // You can add more state variables here

    function run(uint256 _upperBound) external pure returns(uint256) {
        // ADD YOUR CODE HERE
        assembly {

// Should be there, but it is not tested :)
//
//        if iszero(_upperBound) {
//          return (0x60,32)
//          }

            let u:=sub(_upperBound, 1)

            let n3:=div(u, 3)
            let n5:=div(u, 5)
            let n15:=div(u, 15)

        // let s3:=mul(3, add(n3, mul(n3, n3)))
        // let s5:=mul(5, add(n5, mul(n5, n5)))
        // let s15:=mul(15, add(n15, mul(n15, n15)))
        // mstore(0, div(sub(add(s3, s5), s15), 2))
        // Following mstore line is just that - saving 9 gas when in 1 line
            mstore(0, div(sub(add(mul(3, add(n3, mul(n3, n3))), mul(5, add(n5, mul(n5, n5)))), mul(15, add(n15, mul(n15, n15)))), 2))

            return(0, 32)

        }
    }

    // You can add more functions here
}

3rd Place: Otaiki1

Gas: 21740

pragma solidity 0.8.10;

contract FizzBuzz {
    function run(uint256 _upperBound) external pure returns (uint256) {
        assembly {

            _upperBound:= sub(_upperBound, 1)
            let p3 := div(_upperBound, 3)
            let p5 := div(_upperBound, 5)
            let p15 := div(_upperBound, 15)

            mstore(0x0, div(sub(add(mul(mul(3, p3), add(p3, 1)), mul(mul(5, p5), add(p5, 1))), mul(mul(15, p15), add(p15, 1))), 2))
            return(0x0, 32)
        }
    }

}

Thanks for all who participated in the challenge!

Want to learn more? Follow our progress!

For the latest news and developments, including course & challenge and track launches as well as our job board, stay tuned. Join the AgorApp Discord and communicate with the team or follow us on Telegram, Twitter or Linkedin.

Enjoyed the content? Subscribe here!