Project Euler Problem #97

September 5, 2010

Read the details of the problem here

Summary

Find the last ten digits of the non-Mersenne prime: 28433 × 27830457 + 1.

Solution

Easy to translate straight into code as it stands. No working out necessary!

def PREC = 10G ** 10
def answer =
    (2G.modPow(7830457G, PREC) * 28433G + 1G).mod(PREC)

This runs in 18 ms. A Java version, using long-hand BigInteger syntax takes about 1 ms – so around 18x faster. That’s the cost of syntactic sugar in Groovy.

Conclusion

This was one of those “if you’ve got it, flaunt it” problems. The combination of the powerful JVM and convenient Groovy syntax for BigInteger support made for a simple solution albeit less performant than a native Java implementation.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: