4

Can a contract encode a paid request to perform some calculation?

For example, I would like to pay 1 ether to calculate the 100th prime number, and I want the Ethereum blockchain to calculate it for me. In another language, the code might look like this:

boolean function isPrime(int n) {
    if(n < 2)
        return false;

    for (long i = 2; i * i <= n; i++)
        if (n % i == 0)
            return false;
    return true;
}

for(int i = 1; ; i++) {
    if(isPrime(i))
        counter++;

    if(counter == 100) {
        print(i);
        break;
    }
}

The question is not about a contract on doing some work, but about Ethereum's ability to handle computations.

3 Answers 3

1

Yes and no. Ethereum can do it, but it's probably not the tool for the task. It's not decentralized AWS.

Ethereum is Turing complete, so it can perform just about any calculation. However, it is not necessarily efficient to do so. Every step that the EVM takes is replicated across the entire network--that 100th prime number might be recalculated hundreds of times. In addition, every one of those steps costs gas. I wouldn't know off hand, and it would depend on gasPrice, but I'm guessing the above code would cost a lot more than 1 ETH to run.

If you had some method of verifying a number was the 100th prime, it would be easier to create a contract that used that method to verify someone's work from off-chain. Then you can use that 1 ETH as a bounty. (I am aware that this is not what you are asking, but it is the best practices for doing this kind of task.)

1

The answer above is correct. Here is an interesting table showing some examples of gas costs, for deploying various tasks on EVM: Gas cost examples. You can have your conclusions what is worth deploying and what's not.

1

Ethereum can do this, but I would honestly look at the GNT project for this. It runs of the ETH blockchain and provides computational tasks for GNT Token, and should give you a good idea how todo this. All the sourcecode is available at https://github.com/golemfactory

However, if you like to use plain Etherium, it is certainly doable, but rather expensive do to the gas costs, for Ethereum calculations. How complicated do you want your calculations to be and would a private ETH network maybe suited for you? They are really easy to setup and maintain at this time and will reduce your operation cost considerably.

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.