0
    contract PrimeNumberChecker {

     function isPrime(uint n) public pure returns(string memory){
        for(uint i = 2;i<n;i++){
             if(n % i ==0){
                return "Not a prime";
               }
             else{
                return "prime";
            }
          }
      }
   }

1 Answer 1

1

The code you have mentioned will always return in the first loop itself. The return prime statement should be outside the loop. This is the correct implementation.

contract PrimeNumberChecker {
    function isPrime(uint256 n) public pure returns (string memory) {
        for (uint256 i = 2; i < n; i++) {
            if (n % i == 0) {
                return "Not a prime";
            }
        }
        return "prime";
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.