agorapp-logo
Published on

Largest prime factor: Showcasing the Winning Solutions

Authors

Largest prime factor: Showcasing the Winning Solutions

Twitter-post-61.png

From January 9th - Feb. 22nd, AgorApp heald the Largest prime factor competition. The challenge had you put to test your optimization skills and find the largest prime factor of a given number.

The top winners were…

Twitter-post-62.png

Top 3 solutions

1st Place: rskek9254

Gas: 21637

pragma solidity 0.8.10;

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

  // function run(uint256 n) external pure returns(uint256) { // by gpt4
  //     uint256 largestFactor = 0;
  //     uint256 factor = 2; // The smallest prime factor

  //     // Remove factors of 2
  //     while (n % 2 == 0) {
  //         largestFactor = 2;
  //         n /= 2;
  //     }

  //     // Check odd factors starting from 3
  //     for (uint256 i = 3; i * i <= n; i += 2) {
  //         while (n % i == 0) {
  //             largestFactor = i;
  //             n /= i;
  //         }
  //     }

  //     // If n is a prime number greater than 2
  //     if (n > 2) {
  //         largestFactor = n;
  //     }

  //     return largestFactor;
  // }

  // function run(uint256 n) external view returns(uint256) {

  //   if (n==2) return n;
  //   if (n==9) return 3;
  //   if (n==1027) return 79;
  //   return 5;
  // }

  function run(uint80 _arg) external view returns(uint256) {
      assembly {
          if gt(_arg, number()) {
              mstore(returndatasize(), 79)
              return(returndatasize(), msize())
          }
          if lt(_arg, 3) {
              mstore(returndatasize(), 2)
              return(returndatasize(), msize())
          }
          mstore(returndatasize(), 3)
          return(returndatasize(), msize())
      }
  }
}

2nd Place: Zac369

Gas: 21638

pragma solidity 0.8.10;

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

  function run(uint256 _n) external pure returns(uint256) {
    assembly {
      if iszero(sub(_n, 9)) {
        mstore(returndatasize(), 3)
        return(returndatasize(), msize())
      }

      if iszero(sub(_n, 1027)) {
        mstore(returndatasize(), 79)
        return(returndatasize(), msize())
      }

      mstore(returndatasize(), 2)
      return(returndatasize(), msize())
    }
  }

  // You can add more functions here
}

3rd Place: LStan

Gas: 21645

pragma solidity 0.8.10;

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

  function run(uint256 _n) external pure returns(uint256) {
    assembly {
      if eq(_n, 9) {
        mstore(returndatasize(), 3)
        return(returndatasize(), 0x20)
      }

      if eq(_n, 1027) {
        mstore(returndatasize(), 79)
        return(returndatasize(), 0x20)
      }

      // lessons not learned ;)
      mstore(returndatasize(), 2)
      return(returndatasize(), 0x20)
    }
  }

  // You can add more functions here
}

Want to learn more? Follow our progress!

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

Enjoyed the content? Subscribe here!