Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!

# Find 127th twin-prime pair?

Aashu Mahajan
Ranch Hand

Joined: May 27, 2011
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

Joined: Sep 20, 2010
Posts: 4099

19

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.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Aashu Mahajan
Ranch Hand

Joined: May 27, 2011
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

Joined: Oct 02, 2003
Posts: 11882

18

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Aashu Mahajan
Ranch Hand

Joined: May 27, 2011
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

Joined: Sep 20, 2010
Posts: 4099

19

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

Joined: May 27, 2011
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

Joined: Oct 02, 2003
Posts: 11882

18

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

Joined: Oct 02, 2003
Posts: 11882

18

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

Joined: May 27, 2011
Posts: 114

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

Joined: Jun 09, 2011
Posts: 7
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

Joined: Oct 02, 2003
Posts: 11882

18

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

Joined: May 27, 2011
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

Joined: Oct 02, 2003
Posts: 11882

18

kiran kirankukmar,
Your post was moved to a new topic.
kiran kirankukmar
Greenhorn

Joined: Jun 09, 2011
Posts: 7
thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11882

18

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

Joined: Jun 09, 2011
Posts: 7
sir please include if statement of k<100 before that storing prime numbers in array..
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4099

19

Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8898

5
Hi Guys,

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

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Greenhorn

Joined: Jun 11, 2011
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

Joined: Jun 11, 2011
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

Joined: May 27, 2011
Posts: 114

Welcome to Java Ranch

I already solved this question 1day ago, read my posts again and please don't provide email address here but yes you can tell the logic(not full coding) you applied because Every problem has a no. of solutions(especially in Java Programming ) so i would appreciate you share the logic applied to make the code successful. Also you can not get the coding of this program even searching on google.
Thanks
Keep stay with Java Ranch

I agree. Here's the link: http://aspose.com/file-tools

subject: Find 127th twin-prime pair?