Over a million developers have joined DZone.

Thursday Code Puzzler: Finding Twin Primes

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

Time for our weekly code puzzle. The idea is simple: solve the coding problem as efficiently as you can, in any language or framework that you think is suitable. Today problem is to find all the twin primes within a certain range. A twin prime is a pair of prime numbers that differs by two. Two examples would be (3,5) and (5,7).  

What you have to do is write a method/function that takes an integer input (let's call it limit) and finds all the twin primes up to that limit.

As you are free to use any language, it will be interesting to see which approach will give the most readable code.

Note: Even though there really is nothing stopping you from finding a solution to this on the internet, try to keep honest, and come up with your own answer.  It's all about the participation!

Thanks to everyone who participated in the last few code puzzlers. 

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}