Win a copy of Design for the Mind this week in the Design forum!

# Find 127th twin-prime pair?

Aashu Mahajan
Ranch Hand
Posts: 114
source - chetanasforum (a Job Portal site)
Find 127th twin-prime pair(For more information on what is twin prime, look at Wikipedia)

Please could anybody tell me to code this program in java??
I actually could not understand what is twin-prime pair??

Stephan van Hulst
Bartender
Posts: 5553
53
Twin prime pairs are two prime numbers that have a difference of two. Have you read the Wikipedia page? It's pretty clear.

What you can do is generate prime numbers, and subtract each new prime number by the number you generated before that. If the result is two, then the two numbers are a twin prime pair.

Aashu Mahajan
Ranch Hand
Posts: 114
Thanks Stephan van Hulst
I understood now but found it difficult to code. Could you please code it, i tried many with many loop but failed.

fred rosenberger
lowercase baba
Bartender
Posts: 12097
30
We don't do that here. Handing out a solution does nobody any good.

Show us what you've tried, ask specific questions, tell us what exactly isn't working, and you'll get tons of help, but nobody here will (or should) provide you with the solution.

If they do, and I see it, I will delete it.

Aashu Mahajan
Ranch Hand
Posts: 114
Hello guys, After so many tries i think i came to near it, Here i need your help.

Output :

But the output should be (2,5) (5,7) (11,13) (17,19)
Please correct me where i am wrong??
>

Stephan van Hulst
Bartender
Posts: 5553
53
Aashu, don't worry about the pairs yet. Start by writing a program that prints a list of prime numbers.

Hint: a number x is prime if there are no other prime numbers that divide it. So it may be useful to keep track of the prime numbers you've generated so far.

Aashu Mahajan
Ranch Hand
Posts: 114
Please guys help me, i already spent more than 2hr on this code to get output according to given question Please what else i can do in existing code to show the ouput like (2,5) (5,7) (11,13) (17, 19).

fred rosenberger
lowercase baba
Bartender
Posts: 12097
30
Stephan already told you what to do...

In ANY programming problem, you break down what you are trying to do into simpler pieces. In this case, the two main things you need to do are:

a) Find prime numbers
b) given a list of prime numbers, print out twin primes.

Both are COMPLETELY independent of each other. I don't have to know anything about twin primes to generate a list of primes. Further, if someone was handing me a list of primes, I don't care HOW it was generated, I just need to look for the twins in that list.

So...to start, write a method that will generate a list of primes. I would write it so that I could give it an upper limit - i.e. only generate 100 primes to start. Stephan's suggestion was to keep track of all the ones you find, and use those to test for the next one.

fred rosenberger
lowercase baba
Bartender
Posts: 12097
30
Aashu Mahajan wrote:Please guys help me, i already spent more than 2hr on this code to get output according to given question Please what else i can do in existing code to show the ouput like (2,5) (5,7) (11,13) (17, 19).

also...(2,5) is wrong...it should be (3,5).

Aashu Mahajan
Ranch Hand
Posts: 114
• 1
Hey Guys Finally i did it, i did it, i solved this problem

Thanks to fred rosenberger, Stephan van Hulst for you suggetion
First of all i constrcut an array of int of length 100 and then i made a method that generate prime no. Now when the method is called is it would generate a prime no. series and this those prime series are stored in array then after the problem get very clear. I happy now

kiran kirankukmar
Greenhorn
Posts: 7
• 1
Aashu Mahajan wrote:Hey Guys Finally i did it, i did it, i solved this problem

Thanks to fred rosenberger, Stephan van Hulst for you suggetion
First of all i constrcut an array of int of length 100 and then i made a method that generate prime no. Now when the method is called is it would generate a prime no. series and this those prime series are stored in array then after the problem get very clear. I happy now

Hi friend..congrats for that code.I am trying from 2hrs but i cant getting a single error make me confusion.please mail your code to me once i will see and correct my code please...please....
my mail id is kirankiran123456.kk@gmail.com.

fred rosenberger
lowercase baba
Bartender
Posts: 12097
30
kiran kirankukmar wrote:
Hi friend..congrats for that code.I am trying from 2hrs but i cant getting a single error make me confusion.please mail your code to me once i will see and correct my code please...please....
my mail id is kirankiran123456.kk@gmail.com.

Kiran,

Please read my earlier posts. We don't hand out code here as 'ready-made solutions'. We try to help people learn how to write code for themselves.

If you need help, I would suggest you start your own thread/topic, post what code you have, and ask specific questions there. Folks here love to help, but nobody can or should do your work for you.

Aashu Mahajan
Ranch Hand
Posts: 114
Hello Guys,
127th twin-prime pair is : (5009, 5011)

@kiran kirankukmar
The problem is somewhat tricky and fred rosenberger, Stephan van Hulst explained well, read their each words carefully. you will get hint.

@fred rosenberger, Stephan van Hulst
I want you check my code weather my answer is correct or not. and what should be the answere according to you?

fred rosenberger
lowercase baba
Bartender
Posts: 12097
30
kiran kirankukmar,
Your post was moved to a new topic.

kiran kirankukmar
Greenhorn
Posts: 7
thanks

fred rosenberger
lowercase baba
Bartender
Posts: 12097
30
I can tell by looking at it your code won't compile. Also, PLEASE move this discussion to YOUR thread that I created for you (linked to above), rather than continuing to hijack this thread.

kiran kirankukmar
Greenhorn
Posts: 7
sir please include if statement of k<100 before that storing prime numbers in array..

Stephan van Hulst
Bartender
Posts: 5553
53

Bert Bates
author
Sheriff
Posts: 8898
5
• 1
Hi Guys,

For anyone reading this thread, don't worry, there's nothing like this on the real exam.

Greenhorn
Posts: 2
Prasad babu wrote:source - chetanasforum (a Job Portal site)
Find 127th twin-prime pair(For more information on what is twin prime, look at Wikipedia)

I just write the code for this yar if you want that source code contact me on spb.1986@gmail.com.

Greenhorn
Posts: 2
hey this is prasad. i just write the code for this. if you want that contact me on spb.1986@gmail.com k

Aashu Mahajan
Ranch Hand
Posts: 114
• 1