Over a million developers have joined DZone.

Thursday Code Puzzler: Finding Twin Primes

DZone's Guide to

Thursday Code Puzzler: Finding Twin Primes

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

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. 

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}