13042 Views 64 Replies Latest reply: Mar 22, 2011 2:34 AM by MikeArmstrong
335 posts since
Jan 22, 2007
Currently Being Moderated

## Twin Primes and Extracting.

Hi,

I have a question and need help. Assuming that we have a set of Prime Numbers (from 5 to 37

in this case) Let's extract Twin Primes like as the Output below. (IN .MCD12)

Regards,

Loi.

Attachments:
• 4,522 posts since
Dec 3, 2008
Currently Being Moderated
Nov 2, 2010 1:07 AM (in response to lvl107)
Re: Twin Primes and Extracting.

Something like the attached?

Mike

Attachments:
• 10,394 posts since
May 11, 2010
Currently Being Moderated
Nov 2, 2010 5:18 AM (in response to MikeArmstrong)
Re: Twin Primes and Extracting.

Sorry, but that's not correct. Twin primes are pairs of primes that differ by 2, so (23,29) is not a valid pair.

Attachments:
• 4,864 posts since
Jun 1, 2007
Currently Being Moderated
Nov 2, 2010 8:24 AM (in response to lvl107)
Re: Twin Primes and Extracting.

Loi Le wrote:

Thanks, Richard. Possibly I should have the update-question :

Just a top-of-the-head thing, but might be worth playing with.   Makes use of the fact that 'triple primes' don't exist (unless you count 1,2,3), to avoid re-testing numbers.

Stuart

Attachments:
• 10,394 posts since
May 11, 2010
Currently Being Moderated
Nov 2, 2010 8:47 AM (in response to StuartBruff)
Re: Twin Primes and Extracting.
Makes use of the fact that 'triple primes' don't exist (unless you count 1,2,3), to avoid re-testing numbers.

Another 10^-9 C saving in global warming

• 4,864 posts since
Jun 1, 2007
Currently Being Moderated
Nov 2, 2010 9:59 AM (in response to A.Non)
Re: Twin Primes and Extracting.

Richard Jackson wrote:

Makes use of the fact that 'triple primes' don't exist (unless you count 1,2,3), to avoid re-testing numbers.

Another 10^-9 C saving in global warming

A nanocoulomb?   Sorry, guv, I never really got past pounds, shillings and pence.  What's that in moles of electrons?

However, you neglected the effects of relativity on subjective time experience ... that' gotta be worth a couple of microseconds of anybody's time!

Stuart

• 4,522 posts since
Dec 3, 2008
Currently Being Moderated
Nov 2, 2010 11:24 AM (in response to StuartBruff)
Re: Twin Primes and Extracting.

Nice function...... Impressed

Was going to propose an alternative, but I don't think I could.

Mike

• 4,864 posts since
Jun 1, 2007
Currently Being Moderated
Nov 2, 2010 4:36 PM (in response to MikeArmstrong)
Re: Twin Primes and Extracting.

Mike Armstrong wrote:

Nice function...... Impressed

Was going to propose an alternative, but I don't think I could.

Mike

Oy vey iz mir! Are my lessons forgotten so soon, already?

There's *always* an alternative to anything I suggest

There are a couple of tricks to finding alternatives.

One is to just look at it sniffily and pick holes in it.  "Hah!  It doesn't handle the (special case) pairs (2,3) & (3,5)!" (having, naturally, extended the prime sequence) and then noting that it doesn't pick up the true prime triplets (2,3,5) & (3,5,7), extend the idea into Prime Constellations - ie, some sequence of primes that spans the minimum distance according to some criteria, eg prime triplets can be of the form (p,p+2,p+6), (p,p+4,p+6), (p,p+6,p+12), etc.   Having then sneered at my rather pathetic program, you can then produce the piece de resistance that knocks mine into a cocked hat.

An alternative viewpoint is to, as Jean would have said, draw inspiration from a worksheet by saying "Wow!  I wonder if it's possible to extend that program to cover other cases!"; both views can lead to the same result.   Depends whether competition is your forte or you have a temperament like a Labrador puppy newly promoted to 2nd Lt.

Or you could be like me, a cross between Scrooge and the Four Yorkshiremen.   I were raised when you could count each clock cycle of t' CPU and every bit counted, but you try and tell the young people of today that ..... they won't believe you.   So profligate waste, such as Richard's checking a number more than once, that may be acceptable if you've got gigs of RAM and Hz, just offends my sensibilities (well, that's a bit of an exaggeration, I've only got the one sensibility).

Which leads to another way of developing alternatives - take a trick out of Paul Dirac's book and look for the beauty or elegance in a method.

Stuart

• 4,522 posts since
Dec 3, 2008
Currently Being Moderated
Nov 3, 2010 2:13 AM (in response to StuartBruff)
Re: Twin Primes and Extracting.

Stuart,

After reading your post I thought "I'm not a Labrador puppy" and decided to write a function which would collect Triplet Primes, see below.

Then, I decided to do a little research into types of Prime numbers and realised that there are multiple types.

• Pairs
• Triplets
• Quintuplets

Which massivly deflated me and made me realsie I might be more of a Poodle than a Labrador

Mike

• 4,864 posts since
Jun 1, 2007
Currently Being Moderated
Nov 3, 2010 2:49 AM (in response to MikeArmstrong)
Re: Twin Primes and Extracting.

Mike Armstrong wrote:

Stuart,

After reading your post I thought "I'm not a Labrador puppy" and decided to write a function which would collect Triplet Primes, see below.

Then, I decided to do a little research into types of Prime numbers and realised that there are multiple types.

• Pairs
• Triplets
• Quintuplets

Which massivly deflated me and made me realsie I might be more of a Poodle than a Labrador

Mike

First, I should point (pun intended) that Poodles are No 2 in the dog intelligence stakes, whereas Labradors are No 7.  So well done on your promotion! (I, being of Welsh extraction, am, of course, a Border Collie, although my wife puts me about twenty places lower than an Afghan Hound. :-) )

Second, the term Labrador Puppy is not at all derdogatory!   They are enthusiastic, intelligent, full of boundless energy and totally optimistic, just like a Second Lieutenant ... well, apart from the intelligence bit - never give a 2nd Lt a map unless you want to get totally lost.

As to your problem, this is where Labrador Nature comes in handy, as you should view it as a Labrador would view a Brontosaurus(*) - a big collection of bones (unlike my Collie self, which does a quick 180 and applies full military power).

Stuart

(*) Yes, I know it's really an Apatosaurus, but I'm with Gould on this one.

• 4,864 posts since
Jun 1, 2007
Currently Being Moderated
Nov 3, 2010 3:06 AM (in response to StuartBruff)
Re: Twin Primes and Extracting.

Stuart Bruff wrote:

... First, I should point (pun intended) that Poodles are No 2 in the dog intelligence stakes, whereas Labradors are No 7.  So well done on your promotion! (I, being of Welsh extraction, am, of course, a Border Collie, although my wife puts me about twenty places lower than an Afghan Hound. :-) )

... As to your problem, this is where Labrador Nature comes in handy, as you should view it as a Labrador would view a Brontosaurus(*) - a big collection of bones (unlike my Collie self, which does a quick 180 and applies full military power).

Actually, thinking about it for a few seconds, I guess this proves my wife's point, as a real Collie would simply give a flock of Brontosauri 'The Eye' and start herding them.

• 10,394 posts since
May 11, 2010
Currently Being Moderated
Nov 3, 2010 8:13 AM (in response to StuartBruff)
Re: Twin Primes and Extracting.

Second, the term Labrador Puppy is not at all derdogatory!

Especially if it's a very determined Labrador puppy

• 10,394 posts since
May 11, 2010
Currently Being Moderated
Nov 3, 2010 8:02 AM (in response to StuartBruff)
Re: Twin Primes and Extracting.
noting that it doesn't pick up the true prime triplets (2,3,5) & (3,5,7),

You said the only prime triplet was 1,2,3. I took you at your word there, and never even thought about it. It just goes to show that one should always think about what they are reading!

What a wasted opportunity to come up with a smart-*** reply

Edit: Oh God! The nanny-ware kicked in! Perhaps I'm supposed to say smart-donkey reply?

• 4,864 posts since
Jun 1, 2007
Currently Being Moderated
Nov 3, 2010 8:27 AM (in response to A.Non)
Re: Twin Primes and Extracting.

Richard Jackson wrote:

noting that it doesn't pick up the true prime triplets (2,3,5) & (3,5,7),

You said the only prime triplet was 1,2,3. I took you at your word there, and never even thought about it. It just goes to show that one should always think about what they are reading!

What a wasted opportunity to come up with a smart-*** reply

Edit: Oh God! The nanny-ware kicked in! Perhaps I'm supposed to say smart-donkey reply?

I did say 'true' triplets, all the rest of them have are separated by more than 2.   There are also some variations that I regard as cheating.  For example, there are triplets that are consecutive primes but there are also other triplets that match a given pattern, but aren't consecutive (ie, there's one or more other primes in the minimum subsequence containing the triplet.

What a wasted opportunity to come up with a smart-*** reply

Edit: Oh God! The nanny-ware kicked in! Perhaps I'm supposed to say smart-donkey reply?

I'm shocked, Jackson, that you could impugn donkeys in such a way.  Is it not enough that we denigrate cats, by attributing feminine malice to them, that we must now resort to defaming equus africanus asinus?

A friend of mine insists on a Right to Reply

• 4,522 posts since
Dec 3, 2008
Currently Being Moderated
Nov 2, 2010 11:02 AM (in response to A.Non)
Re: Twin Primes and Extracting.

Sorry, but that's not correct. Twin primes are pairs of primes that differ by 2, so (23,29) is not a valid pair.

Ooops. Of course not.

Didn't even read the question properly. Just concentrated on nesting every two elements.

Mike

• 4,522 posts since
Dec 3, 2008
Currently Being Moderated
Nov 2, 2010 11:26 AM (in response to A.Non)
Re: Twin Primes and Extracting.

Richard,

Its such a shame that I don't agree with removing posts because this is a valid example of a post needing to be removed.

Mike

• 10,394 posts since
May 11, 2010
Currently Being Moderated
Nov 2, 2010 11:36 AM (in response to MikeArmstrong)
Re: Twin Primes and Extracting.

Unfortunately, unless they have fixed it so you can delete only your own post and not all the ones subsequent to it, that would delete most of the thread (including Staurt's answer!).

• 4,522 posts since
Dec 3, 2008
Currently Being Moderated
Nov 2, 2010 11:45 AM (in response to A.Non)
Re: Twin Primes and Extracting.

Unfortunately, unless they have fixed it so you can delete only your own post and not all the ones subsequent to it, that would delete most of the thread (including Staurt's answer!).

Think I'll pass on that then.

Mike

• 4,522 posts since
Dec 3, 2008
Currently Being Moderated
Nov 2, 2010 1:09 AM (in response to lvl107)
Re: Twin Primes and Extracting.

Here is a worksheet you might be intrested in. Found in the old collab posted by Jean.

Mike

Attachments:
• 4,522 posts since
Dec 3, 2008
Currently Being Moderated
Nov 2, 2010 1:49 PM (in response to lvl107)
Re: Twin Primes and Extracting.

Like the attached?

Just a word of advice. If somebody posts an answer to the original question don't ask another question and discard the answer. You have asked a question which is a variation to the original thread and created a new Mathcad document without the function supplied by Richard or Stuart.

If you keep all posted functions in the worksheet, all collabs can track the progress of the thread by downloading the latest file.

Mike

Attachments:
• 4,864 posts since
Jun 1, 2007
Currently Being Moderated
Nov 2, 2010 2:30 PM (in response to lvl107)
Re: Twin Primes and Extracting.

Loi Le wrote:

I have still one more extra question and wish to learn.

Loi.

Looks straightforward.  Refer to my previous example, add a new variable MTP (merged twin primes), every time you add an item to TP or NTP, add it to MTP.  At the end, add MTP to the augment.

Stuart

(I haven't got Mathcad open at the moment (installing something else), so I'm just guessing from what I remember of my function ... sounds OKish, though)

• 4,522 posts since
Dec 3, 2008
Currently Being Moderated
Nov 4, 2010 8:38 AM (in response to lvl107)
Re: Twin Primes and Extracting.

Like the attached?

Mike

Message was edited by: Mike Armstrong - Worksheet saved down. The pre-defined function used is in the collasped area.

Attachments:
• 4,864 posts since
Jun 1, 2007
Currently Being Moderated
Nov 5, 2010 6:06 AM (in response to MikeArmstrong)
Re: Twin Primes and Extracting.

Mike Armstrong wrote:

Like the attached?

Mike

Message was edited by: Mike Armstrong - Worksheet saved down. The pre-defined function used is in the collasped area.

Mike,

Just a quick note.  In my later worksheets, I've dropped the term 'ravel' and replaced it with 'vec'.

'ravel' is a term, that as far as I can determine, was first widely used in the programming language APL and has continued on into APL's 'successor', J, and some other vector languages.   However, 'vec' appears to be more widely used in matrix theory and linear algebra.   Given that the idea of Mathcad is to be an Engineer's, Scientist's, Mathematician's whiteboard, I thought I'd switch from ravel to vec.

Initially, I used vec as a general purpose function for integer sequences if the input was a scalar.   However, as time went on, I thought it was more consistent to get vec to change a scalar into a single-element array and introduce the function 'seq' to generate sequences.   Currently, the only difference between vec and seq is in the handling of scalars, but I might have a think about seq to see if I can make it more useful for sequence generation ... I've already created a few variants that handle different data types and more complex sequences.

Stuart

The attached worksheet has some behavioural differences between versions of Mathcad.   M13..M15 will handle an expression of the form v:=vec(rv) where rv is a range variable, M11 won't.  M11, M13..M15 will handle v:=vec(rd) where rd is a range definition (ie, expression of form a,b..c).

Attachments:
• 4,522 posts since
Dec 3, 2008
Currently Being Moderated
Nov 5, 2010 11:37 AM (in response to StuartBruff)
Re: Twin Primes and Extracting.

Very nice. Particulary like the seq(n) function.

I have been using the following function posted by Richard, but your seq function seems to have the advantage of taking a scalar.

Been having a think and I've got no idea how you could improve the seq function.

Mike

• 4,864 posts since
Jun 1, 2007
Currently Being Moderated
Nov 5, 2010 2:03 PM (in response to MikeArmstrong)
Re: Twin Primes and Extracting.

Mike Armstrong wrote:

Very nice. Particulary like the seq(n) function.

I have been using the following function posted by Richard, but your seq function seems to have the advantage of taking a scalar.

Been having a think and I've got no idea how you could improve the seq function.

Mike

OK.   What's one of the annoyances you've got with the limitations of range variables?  What data types can't you express as a range?

I'll see if I can dig up the worksheet I was working on a bit ago.

Stuart

• 4,522 posts since
Dec 3, 2008
Currently Being Moderated
Nov 5, 2010 2:14 PM (in response to StuartBruff)
Re: Twin Primes and Extracting.
OK.   What's one of the annoyances you've got with the limitations of range variables?  What data types can't you express as a range?

Is that a rhetorical question?

Mike

• 4,864 posts since
Jun 1, 2007
Currently Being Moderated
Nov 5, 2010 5:05 PM (in response to MikeArmstrong)
Re: Twin Primes and Extracting.

Mike Armstrong wrote:

OK.   What's one of the annoyances you've got with the limitations of range variables?  What data types can't you express as a range?

Is that a rhetorical question?

Mike

Do I look like a rhetorical kind of guy?   I'm much too simple to indulge in rhetoric.

Think about the kind of sequences that a range variable generates.   Are there any sequences you'd like to generate that you can't?  Or any that Mathcad should be able to generate but can't?  Eg, data types?

Stuart

#### More Like This

• Retrieving data ...